以下是示例查詢。
您可以使用SQL Server Windows函數來獲取所需的結果。
DECLARE @Table TABLE
(
ItemName NVARCHAR(40),
Price DECIMAL(10,2),
CreatedDatetime DATETIME
)
-- Fill table.
INSERT INTO @Table
(ItemName, Price, CreatedDatetime)
VALUES
(N'New Card' , 50.00 , '2014-05-26 19:17:09.987' ),
(N'Recharge' , 110.00 , '2014-05-26 19:17:12.427') ,
(N'Promo' , 90.00 , '2014-05-27 16:17:12.427') ,
(N'Membership' , 70.00 , '2014-05-27 16:17:12.427') ,
(N'New Card' , 50.00 , '2014-05-26 19:20:09.987')
-- Check record(s).
SELECT * FROM @Table
-- Get record(s) in required way.
;WITH T1 AS
(
SELECT
DATEPART(HOUR, T.CreatedDatetime) AS Hour,
CONVERT(DATE, T.CreatedDatetime) AS Date,
T.ItemName AS BreakDownOfSales,
-- Date and hour both will give unique record(s)
SUM(Price) OVER (PARTITION BY CONVERT(DATE, T.CreatedDatetime), DATEPART(HOUR, CreatedDateTime)) AS SaleAmount,
ROW_NUMBER() OVER(PARTITION BY CONVERT(DATE, T.CreatedDatetime), DATEPART(HOUR, T.CreatedDatetime) ORDER BY T.Price DESC) AS RN
FROM
@Table T
)
SELECT
T1.Date ,
T1.Hour ,
T1.SaleAmount,
T1.BreakDownOfSales
FROM
T1
WHERE T1. RN = 1
ORDER BY
T1.Hour
您可能需要看看[問] [旅遊],[格式](/幫助/格式)等 – fedorqui
你如何期待與ITEMNAME – Chanukya
導致這就是我想要實現。 –