每月报告的TSQL按组排序

时间:2016-02-14 05:34:46

标签: sql sql-server tsql

我有法庭清关统计数据,但我的每月都有一些轻微的问题..它按顺序排列,并希望有人可以帮我修复它...是否可以在我的表中添加总数?< / p>

DECLARE @StartDate As date = '03-28-2015',
@EndDate As date = '03-28-2015'



DECLARE @TEMP_DATES AS TABLE (FROM_DATE DATE, TO_DATE DATE)
INSERT INTO @TEMP_DATES VALUES(@StartDate, @EndDate)

DECLARE @TENP_MONTH_YEAR AS TABLE(MONTH_YEAR VARCHAR(20), [YEAR] INT, [MONTH] INT)
INSERT INTO @TENP_MONTH_YEAR
select FORMAT(D.Dates, 'MMMM-yy', 'en-US' ) AS MonthYear, YEAR(D.Dates), MONTH(D.Dates)
from @TEMP_DATES as T
  inner join master..spt_values as N
    on N.number between 0 and datediff(DAY, T.FROM_DATE, T.TO_DATE)
    cross apply (select dateadd(DAY, N.number, T.FROM_DATE)) as D(Dates)
where N.type ='P'
GROUP BY FORMAT(D.Dates, 'MMMM-yy', 'en-US' ), YEAR(D.Dates), MONTH(D.Dates)
ORDER BY YEAR(D.Dates), MONTH(D.Dates)

DECLARE @NEWID AS UNIQUEIDENTIFIER = NEWID()

SELECT CT.RPT_CASE_CODE_GROUP, SUM(ISNULL(INCOMING_CASES, 0)) AS INCOMING_CASES, SUM(ISNULL(OUTGOING_CASES, 0)) AS OUTGOING_CASES,
ISNULL(CAST(SUM(NULLIF(CAST(ISNULL(OUTGOING_CASES, 0) AS DECIMAL),0.00))/SUM(NULLIF(CAST(ISNULL(INCOMING_CASES, 0) AS DECIMAL),0.00)) * 100 AS DECIMAL(18,2)),0) AS [CLEARANCE_RATE], 
MONTH_YEAR
FROM @tempClearanceListCases tempCLC
RIGHT OUTER JOIN (SELECT CASE_TYPE_ID, MONTH_YEAR, [YEAR], [MONTH]
    FROM (SELECT DISTINCT CASE_TYPE_ID FROM @tempClearanceListCases tempCLC ) A, @TENP_MONTH_YEAR tempMonthYear) B
    ON B.CASE_TYPE_ID = tempCLC.CASE_TYPE_ID AND tempCLC.MONTHLY = B.MONTH_YEAR
INNER JOIN CaseType CT WITH (NOLOCK)
    ON B.CASE_TYPE_ID = CT.CASE_TYPE_ID 
WHERE ISNULL(COURT_LOCATION_ID, @NEWID) = ISNULL(@COURT_LOCATION_ID, ISNULL(COURT_LOCATION_ID, @NEWID))
GROUP BY CT.RPT_CASE_CODE_GROUP, [INTERVAL_MONTH], MONTH_YEAR
ORDER BY CT.RPT_CASE_CODE_GROUP 

每月的结果不正确的顺序:

RPT_CASE_CODE_GROUP | INCOMEING CASES | OUTGOING CASES | CLEARANCERATE |  MONTHYEAR

BCY/CP                    15               4                 26.67         March-15
BCY/CP                    15               0                 0.00          February-15
BCY/CP                    33               0                 0.00          January-15
BCY/DP                     0               0                 0.00          February-15
BCY/DP                     2               0                 0.00          March-15
BCY/DP                     1               0                 0.00          January-15

结果至少我希望它是:

RPT_CASE_CODE_GROUP | INCOMEING CASES | OUTGOING CASES | CLEARANCERATE |  MONTHYEAR
BCY/CP                    33               0                 0.00          January-15
BCY/CP                    15               0                 0.00          February-15
BCY/CP                    15               4                 26.67         March-15
BCY/DP                     1               0                 0.00          January-15
BCY/DP                     0               0                 0.00          February-15
BCY/DP                     2               0                 0.00          March-15

我想要的结果:

RPT_CASE_CODE_GROUP | INCOMEING CASES | OUTGOING CASES | CLEARANCERATE |  MONTHYEAR
BCY/CP                    33               0                 0.00          January-15
BCY/CP                    15               0                 0.00          February-15
BCY/CP                    15               4                 26.67         March-15
                          63               4                 6.34          Overall
BCY/DP                     1               0                 0.00          January-15
BCY/DP                     0               0                 0.00          February-15
BCY/DP                     2               0                 0.00          March-15
                           3               0                 0.00          Overall

我必须坚持查询或创建分组查询吗?我已经花了很多时间在这上面,我觉得很难回头,我很新鲜的毕业生:(任何大师能指导我吗?

2 个答案:

答案 0 :(得分:1)

如果您的所有MONTHYEAR都遵循相同的模式monthname-xx,您可以在订单上使用以下声明:

 ORDER BY 
    cast('20' + substring(MONTHYEAR, Charindex('-', MONTHYEAR) + 1, 2) + '-' + substring(MONTHYEAR, 1, 3) + '-01' AS date)

答案 1 :(得分:0)

这看起来像是UNION子句的工作。您似乎应该为所需的结果执行此执行顺序:

SELECT -- data you want from tables w/ all joins
WHERE RPT_CASE_CODE_GROUP = 'BCY/CP'
UNION ALL
SELECT '', SUM([INCOMING CASES]), SUM([OUTGOING CASES]), AVG([CLEARANCE RATE], 'Overall'
FROM -- source data
WHERE RPT_CASE_CODE_GROUP = 'BCY/CP'
UNION ALL
SELECT -- data you want from tables w/ all joins
WHERE RPT_CASE_CODE_GROUP = 'BCY/DP'
UNION ALL
SELECT '', SUM([INCOMING CASES]), SUM([OUTGOING CASES]), AVG([CLEARANCE RATE], 'Overall'
FROM -- source data
WHERE RPT_CASE_CODE_GROUP = 'BCY/CP'

或者,如果有多个RPT_CASE_CODE_GROUP值而不是显示的值,您可以使用循环来捕获有关所有这些值的信息,如下所示:

CREATE TABLE #rccg(ID INT IDENTITY(1,1), RPT_CASE_CODE_GROUP NVARCHAR(15))
INSERT INTO #rccg(RPT_CASE_CODE_GROUP)
SELECT DISTINCT RPT_CASE_CODE_GROUP FROM --data source

DECLARE @i INT = 1, @j INT = (SELECT MAX(ID) FROM #rccg), @rccg NVARCHAR(15)
DECLARE @results TABLE(rccg NVARCHAR(15), ic INT, oc INT, cr INT, my NVARCHAR(20))
WHILE @i < @j
BEGIN
    SET @rccg = (SELECT RPT_CASE_CODE_GROUP FROM #rccg WHERE ID = @i)
    INSERT INTO @results
    SELECT -- data you want from tables w/ all joins
    WHERE RPT_CASE_CODE_GROUP = @rccg
    UNION ALL
    SELECT '', SUM([INCOMING CASES]), SUM([OUTGOING CASES]), AVG([CLEARANCE RATE], 'Overall'
    FROM -- source data
    WHERE RPT_CASE_CODE_GROUP = @rccg
END
SELECT * FROM @rccg
DROP TABLE #rccg

我知道我没有使用您的特定代码,因为这会占用大量空间,但我认为您会得到一般的想法。对于每个字段,您需要时间段的总计,使用SUM(column_name)聚合函数。你可以在这里得到一个很好的解释和例子:http://www.techonthenet.com/sql/sum.php

我希望这至少可以帮助你指明正确的方向。