SQL查询帮助请

时间:2014-09-15 17:26:56

标签: sql sql-server

我正在整理一个复杂的查询来创建一种报告,并且遇到一些麻烦。我需要让报告只显示帐户9000-9200。该报告将显示每个帐户[帐单帐户],然后每小时为该帐户的总计,然后以该帐户当天的当前总计结束。我目前的查询如下:

Select [Bill Acct], 
    (select [Total Fee] from Trans where DATEPART(hh, [Time Out]) = 0 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0000],
    (select [Total Fee] from Trans where DATEPART(hh, [Time Out]) = 1 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0100],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 2 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0200],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 3 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0300],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 4 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0400],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 5 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0500],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 6 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0600],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 7 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0700],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 8 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0800],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 9 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [0900],
    (select Sum([Total Fee]) from Trans where DATEPART(hh, [Time Out]) = 10 ANd Cast([Date Out] as date) = Cast(getdate() as date)) as [1000],
    (select Sum([Total Fee]) from Trans where Cast([Date Out] as date) = Cast(getdate() as date)) as Total
From Trans
Where [Bill Acct] between 9000 and 9200
ANd Cast([Date Out] as date) = Cast(getdate() as date)
And Void = 0
Group By  [Bill Acct], [Site Code]
Order by [Site Code]

2 个答案:

答案 0 :(得分:2)

有更好的方法来编写查询:PIVOT语句或一组CASE语句(如下所示)会快得多。正如Greenspark所提到的,有一些缺失的聚合函数,并且您的SELECT语句中没有“站点代码”字段。

假设其中一个,全部或部分是个问题,请尝试以下方法:

SELECT  
    [Bill Acct], 
    [Site Code],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 0 THEN [Total Fee] ELSE 0 END) AS [0000],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 1 THEN [Total Fee] ELSE 0 END) AS [0100],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 2 THEN [Total Fee] ELSE 0 END) AS [0200],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 3 THEN [Total Fee] ELSE 0 END) AS [0300],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 4 THEN [Total Fee] ELSE 0 END) AS [0400],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 5 THEN [Total Fee] ELSE 0 END) AS [0500],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 6 THEN [Total Fee] ELSE 0 END) AS [0600],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 7 THEN [Total Fee] ELSE 0 END) AS [0700],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 8 THEN [Total Fee] ELSE 0 END) AS [0800],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 9 THEN [Total Fee] ELSE 0 END) AS [0900],
    SUM(CASE WHEN DATEPART(hh, [Time Out]) = 10 THEN [Total Fee] ELSE 0 END) AS [1000],
    SUM([Total Fee]) AS Total
FROM Trans
WHERE 
    ([Bill Acct] BETWEEN 9000 AND 9200) AND
    CAST([Date Out] AS DATE) = CAST(GETDATE() AS DATE) AND
    Void = 0
GROUP BY [Bill Acct], [Site Code]
ORDER BY [Site Code]

答案 1 :(得分:1)

如果我理解正确,您的上述查询可以简化为以下内容

Select [Bill Acct], 
    [Total Fee],
    Sum([Total Fee])
 from Trans 
where DATEPART(hh, [Time Out]) in (0,1,2,3,4,5,6,7,8,9,10)
 ANd Cast([Date Out] as date) = Cast(getdate() as date)) 
 AND [Bill Acct] between 9000 and 9200
And Void = 0
Group By  [Bill Acct], [Total Fee]
Order by [Site Code]