任何人都帮助我解决这个问题,我尽我所能,但没有解决这个问题?

时间:2017-01-20 10:26:56

标签: sql sql-server

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  

Out Put:需要查询当前小时和的销售额 在销毁故障的当时最大限度销售的商品的销售 列。

Hour    SaleAmount  BreakDownOfSale  
19      210         Recharge  
16      160         Promo  

4 个答案:

答案 0 :(得分:0)

这应该这样做

[INFO] Executing Karma Test Suite ...
cmd /C karma start C:\.....\karma.conf.js --browsers Chrome --single-run --no-auto-watch --colors true
'karma' is not recognized as an internal or external command, 
operable program or batch file.

答案 1 :(得分:0)

虽然我不清楚这个问题,但根据您所需的输出,您可以使用以下查询。

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

将表名和列名替换为实际名称。

希望这有帮助!

答案 2 :(得分: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

答案 3 :(得分: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);