2017-01-20 195 views
0
ItemName Price CreatedDateTime 
New Card 50.00 2014-05-26 19:17:09.987 
Recharge 110.00 2014-05-26 19:17:12.427 
Promo  90.00 2014-05-27 16:17:12.427 
Membership 70.00 2014-05-27 16:17:12.427 
New Card 50.00 2014-05-26 19:20:09.987 

輸出:需要一個查詢,其中銷售的當前小時和 銷售在該小時內銷售額最大的物品的銷售情況 列。任何人都可以幫助我解決這個問題,盡我所能,但沒有解決這個問題?

Hour SaleAmount BreakDownOfSale 
19  210   Recharge 
16  160   Promo 
+0

您可能需要看看[問] [旅遊],[格式](/幫助/格式)等 – fedorqui

+0

你如何期待與ITEMNAME – Chanukya

+0

導致這就是我想要實現。 –

回答

0

這應該這樣做

create table #t 
(
    ItemName varchar(50), 
    Price decimal(18,2), 
    CreatedDateTime datetime 
); 

set dateformat ymd; 

insert into #t values('New Card', 50.00, '2014-05-26 19:17:09.987'); 
insert into #t values('Recharge', 110.00, '2014-05-26 19:17:12.427'); 
insert into #t values('Promo', 90.00, '2014-05-27 16:17:12.427'); 
insert into #t values('Membership', 70.00, '2014-05-27 16:17:12.427'); 
insert into #t values('New Card', 50.00, '2014-05-26 19:20:09.987'); 

with cte as 
(
    select datepart(hh, CreatedDateTime) as [Hour], 
     ItemName, 
     Price, 
     sum(Price) over (partition by datepart(hh, CreatedDateTime)) SaleAmount, 
     ROW_NUMBER() over (partition by datepart(hh, CreatedDateTime) order by Price desc) rn 
    from #t 
) 
select Hour, 
    SaleAmount, 
    ItemName 
from cte 
where rn = 1 
+0

非常感謝@詹姆斯凱西 –

0

雖然我不是這個問題,根據您所需的輸出清晰,你可以使用下面的查詢。

SELECT DATEPART(HOUR,CreatedDateTime) AS Hour, sum(Price) AS Price, ItemName AS BreakDownOfSale from TableName WHERE BY ItemName,DATEPART(HOUR,CreatedDateTime) 

將表名和列名替換爲實際的名稱和列名。

希望這會有所幫助!

0

以下是示例查詢。

您可以使用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 
+0

非常感謝,它也有效。 –

0
 
Check this simple solution, Please convert it to SQL Server Query. 

This will give you perfect result even if you have multiple date data. 

SELECT HOUR(CreatedDateTime), SUM(Price), 
    (SELECT itemname FROM t it WHERE HOUR(ot.CreatedDateTime) = HOUR(it.CreatedDateTime) AND 
     DATE(ot.CreatedDateTime) = DATE(it.CreatedDateTime) 
     GROUP BY itemname 
     ORDER BY price DESC 
     LIMIT 1 
    ) g 
FROM t ot 
GROUP BY HOUR(CreatedDateTime); 

相關問題