Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
chresrond
Starting Member
2 Posts |
Posted - 2012-12-13 : 23:17:31
|
Dear Master,I'm newbie , I have table like bellow then I need retrieve data like as below result . could you advise how to get the result??Please....i want result like below ;date count(ref_no) count(ctn_no) sum(quantity)00:00am to 08:00 4 5 1000008:01am to 12:00pm12:01pm to 18:00pm18:01 to 23:59pm 00 00 000tbl_packinsert_date ref_no ctn_no serial_no quantity03/09/2012 13:12:53:837 I2280097 34 1 T3BSB 50003/09/2012 13:12:53:867 I2280097 34 2 T3BSA 50003/09/2012 13:12:53:893 I2280097 34 3 T3BS9 50003/09/2012 13:12:53:923 I2280097 34 4 T1NJR 50003/09/2012 13:13:37:453 I2280097 35 1 T29KU 200003/09/2012 13:13:37:483 I2280097 35 2 T29KT 200003/09/2012 13:13:37:51 I2280097 35 3 T29KS 200003/09/2012 13:25:11:907 I2280102 1 1 T491Q 40003/09/2012 13:25:11:953 I2280102 1 2 T491S 40003/09/2012 13:25:11:983 I2280102 1 3 T491U 400ChrisR |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-14 : 00:20:23
|
[code]declare @tbl_pack table (insert_date datetime, ref_no varchar(15), ctn_no int, serial_no int, field varchar(20), quantity inT)insert into @tbl_pack SELECT '03/09/2012 00:12:53:837', 'I2280097', 34, 1, 'T3BSB', 500 union allSELECT '03/09/2012 08:00:53:867', 'I2280097', 34, 2, 'T3BSA', 500 union allSELECT '03/09/2012 08:01:53:893', 'I2280097', 34, 3, 'T3BS9', 500 union allSELECT '03/09/2012 13:12:53:923', 'I2280097', 34, 4, 'T1NJR', 500 union allSELECT '03/09/2012 13:13:37:453', 'I2280097', 35, 1, 'T29KU', 2000 union allSELECT '03/09/2012 13:13:37:483', 'I2280097', 35, 2, 'T29KT', 2000 union allSELECT '03/09/2012 13:13:37:51', 'I2280097', 35, 3, 'T29KS', 2000 union allSELECT '03/09/2012 13:25:11:907', 'I2280102', 1, 1, 'T491Q', 400 union allSELECT '03/09/2012 13:25:11:953', 'I2280102', 1, 2, 'T491S', 400 union allSELECT '03/09/2012 23:25:11:983', 'I2280102', 1, 3, 'T491U', 400SELECT CASE WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '00:00' AND '08:00' THEN '00:00 to 08:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '08:01' AND '12:00' THEN '08:01 to 12:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '12:01' AND '18:00' THEN '12:01 to 18:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '18:01' AND '23:59' THEN '18:01 to 23:59' END TimeRanges, COUNT(ref_no) NoOfRef, COUNT(ctn_no) NoOfCtn, SUM(quantity) TotalQtyFROM @tbl_packGROUP BY CASE WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '00:00' AND '08:00' THEN '00:00 to 08:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '08:01' AND '12:00' THEN '08:01 to 12:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '12:01' AND '18:00' THEN '12:01 to 18:00' WHEN CONVERT(VARCHAR(5), insert_date, 24) BETWEEN '18:01' AND '23:59' THEN '18:01 to 23:59' END [/code]--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
chresrond
Starting Member
2 Posts |
Posted - 2012-12-14 : 02:58:24
|
Dear Candu,Thank you for your answer, but I need per day and not count total row. I need total refno and total ctn_no .Sample I given total only 2 ref no and 3 ctns only.ChrisR |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-12-14 : 03:05:59
|
quote: Originally posted by chresrond Dear Candu,Thank you for your answer, but I need per day and not count total row. I need total refno and total ctn_no .Sample I given total only 2 ref no and 3 ctns only.ChrisR
Add date field also onto GROUP BY in earlier suggestionor use logic in link i posted------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-14 : 04:08:14
|
quote: Originally posted by chresrond Dear Candu,Thank you for your answer, but I need per day and not count total row. I need total refno and total ctn_no .Sample I given total only 2 ref no and 3 ctns only.ChrisR
Hi,Add GROUP BY CAST(insert_date AS DATE) and also put DISTINCT keyword in COUNT() --Chandu |
|
|
|
|
|
|
|