提供されたデータのフィールドを解釈する方法を完全には知らなかったため、テーブル フィールドの名前を少し変更しました。そのため、クエリ ソリューションの例は意図したとおりではないかもしれませんが、近いはずです。実際の RefNumber とおそらく CartonNumber も表示したいのではないかと思いますが、それは単なる推測です。
tbl_pack:
CalendarDateTime RefNumber CartonNumber SequenceNumber SerialNumber Quantity
3/9/2012 13:12:53:837 I2280097 34 1 T3BSB 500
3/9/2012 13:12:53:867 I2280097 34 2 T3BSA 500
3/9/2012 13:12:53:893 I2280097 34 3 T3BS9 500
3/9/2012 13:12:53:923 I2280097 34 4 T1NJR 500
3/9/2012 13:13:37:453 I2280097 35 1 T29KU 2000
3/9/2012 13:13:37:483 I2280097 35 2 T29KT 2000
3/9/2012 13:13:37:510 I2280097 35 3 T29KS 2000
3/9/2012 13:25:11:907 I2280102 1 1 T491Q 400
3/9/2012 13:25:11:953 I2280102 1 2 T491S 400
3/9/2012 13:25:11:983 I2280102 1 3 T491U 400
次に、このクエリはあなたが求めていたことを行います:
SELECT
CONVERT(varchar(10), CalendarDateTime, 111),
(
CASE
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) <= 8*60 THEN '0:00am To 8:00am'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 8*60 + 1 and 12*60 THEN '8:00am To 12:00pm'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 12*60 + 1 and 18*60 THEN '12:00pm To 18:00pm'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 18*60 + 1 and 24 * 60 - 1 THEN '6:00pm To 23:59pm'
END
) as TimePeriod,
COUNT(RefNumber) as CountRefNumber,
COUNT(CartonNumber) as CountCartonNumber,
SUM(Quantity) as SumQuantity
FROM tbl_pack
GROUP BY
CONVERT(varchar(10), CalendarDateTime, 111),
(
CASE
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) <= 8*60 THEN '0:00am To 8:00am'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 8*60 + 1 and 12*60 THEN '8:00am To 12:00pm'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 12*60 + 1 and 18*60 THEN '12:00pm To 18:00pm'
WHEN datepart(hh,CalendarDateTime) * 60 + datepart(mi,CalendarDateTime) between 18*60 + 1 and 24 * 60 - 1 THEN '6:00pm To 23:59pm'
END
)