分组/订购混淆

时间:2014-03-31 10:26:08

标签: sql

希望我在这里有一个简单的问题,并以正确的方式向您解释。

我有以下查询:

--DECLARE DATES
DECLARE @Date datetime
DECLARE @DaysInMonth INT
DECLARE @i INT

--GIVE VALUES
SET @Date = Getdate()
SELECT @DaysInMonth = datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(@Date) as varchar)+'-'+cast(month(@Date) as varchar)+'-01' as datetime))))
SET @i = 1

--MAKE TEMP TABLE
CREATE TABLE #TempDays
(
    [days] VARCHAR(50)
)

WHILE @i <= @DaysInMonth
BEGIN
    INSERT INTO #TempDays
    VALUES(@i)
    SET @i = @i + 1
END



    SELECT #TempDays.days, DATEPART(dd, a.ActualDate) ActualDate, a.ActualAmount, (SELECT SUM(b.ActualAmount)
                           FROM UnpaidManagement..Actual b
                           WHERE b.ID <= a.ID) RunningTotal
    FROM   UnpaidManagement..Actual a
    RIGHT JOIN #TempDays on a.ID = #TempDays.days



DROP TABLE #TempDays

产生以下输出:

 +------+------------+--------------+--------------+
| days | ActualDate | ActualAmount | RunningTotal |
+------+------------+--------------+--------------+
|    1 | 1          | 438706       | R 438 706    |
|    2 | 2          | 16239        | R 454 945    |
|    3 | 3          | 1611264      | R 2 066 209  |
|    4 | 4          | 1157777      | R 3 223 986  |
|    5 | 5          | 470662       | R 3 694 648  |
|    6 | 6          | 288628       | 3983276      |
|    7 | 7          | 245897       | 4229173      |
|    8 | 8          | 5235         | 4234408      |
|    9 | 10         | 375630       | 4610038      |
|   10 | 11         | 95610        | 4705648      |
|   11 | 12         | 87285        | 4792933      |
|   12 | 13         | 73399        | 4866332      |
|   13 | 14         | 59516        | 4925848      |
|   14 | 15         | 918915       | 5844763      |
|   15 | 17         | 1957285      | 7802048      |
|   16 | 18         | 489964       | 8292012      |
|   17 | 19         | 272304       | 8564316      |
|   18 | 20         | 378601       | 8942917      |
|   19 | 22         | 92374        | 9035291      |
|   20 | 23         | 198          | 9035489      |
|   21 | 24         | 1500820      | 10536309     |
|   22 | 25         | 2631057      | 13167366     |
|   23 | 26         | 6466505      | 19633871     |
|   24 | 27         | 3757350      | 23391221     |
|   25 | 28         | 3487466      | 26878687     |
|   26 | 29         | 160197       | 27038884     |
|   27 | 30         | 14000        | 27052884     |
|   28 | NULL       | NULL         | NULL         |
|   29 | NULL       | NULL         | NULL         |
|   30 | NULL       | NULL         | NULL         |
|   31 | NULL       | NULL         | NULL         |
+------+------------+--------------+--------------+

如果仔细查看上表,“ActualDate”列缺少一些值,EG:9,16等。

正因为如此,行被推高而不是按正确的数字分组?我如何通过/任何方式完成一组以使它们保持正确的行?

期望的输出:

+------+------------+--------------+--------------+
| days | ActualDate | ActualAmount | RunningTotal |
+------+------------+--------------+--------------+
|    1 | 1          | 438706       | R 438 706    |
|    2 | 2          | 16239        | R 454 945    |
|    3 | 3          | 1611264      | R 2 066 209  |
|    4 | 4          | 1157777      | R 3 223 986  |
|    5 | 5          | 470662       | R 3 694 648  |
|    6 | 6          | 288628       | 3983276      |
|    7 | 7          | 245897       | 4229173      |
|    8 | 8          | 5235         | 4234408      |
|    9 | NULL       | NULL         | NULL         |
|   10 | 10         | 375630       | 4610038      |
|   11 | 11         | 95610        | 4705648      |
|   12 | 12         | 87285        | 4792933      |
|   13 | 13         | 73399        | 4866332      |
|   14 | 14         | 59516        | 4925848      |
|   15 | 15         | 918915       | 5844763      |
|   16 | NULL       | NULL         | NULL         |
|   17 | 17         | 1957285      | 7802048      |
|   18 | 18         | 489964       | 8292012      |
|   19 | 19         | 272304       | 8564316      |
|   20 | 20         | 378601       | 8942917      |
|   21 | NULL       | NULL         | NULL         |
|   22 | 22         | 92374        | 9035291      |
|   23 | 23         | 198          | 9035489      |
|   24 | 24         | 1500820      | 10536309     |
|   25 | 25         | 2631057      | 13167366     |
|   26 | 26         | 6466505      | 19633871     |
|   27 | 27         | 3757350      | 23391221     |
|   28 | 28         | 3487466      | 26878687     |
|   29 | 29         | 160197       | 27038884     |
|   30 | 30         | 14000        | 27052884     |
|   31 | NULL       | NULL         | NULL         |
+------+------------+--------------+--------------+

我知道这是一个很长的阅读,但如果我已经清楚地解释了这一点,请告诉我。我整个上午一直试图分组,但我一直都有错误。

2 个答案:

答案 0 :(得分:1)

SELECT #TempDays.days, DATEPART(dd, a.ActualDate) ActualDate, a.ActualAmount, (SELECT SUM(b.ActualAmount)
                       FROM UnpaidManagement..Actual b
                       WHERE b.ID <= a.ID) RunningTotal
FROM   UnpaidManagement..Actual a
RIGHT JOIN #TempDays on  DATEPART(dd, a.ActualDate) = #TempDays.days

答案 1 :(得分:0)

如果您选择临时表作为选择中的第一个表并加入UnpaidManagement..Actual,那么您的日期将按正确的行和顺序排列:

SELECT t.days
    ,DATEPART(dd, a.ActualDate) ActualDate
    ,a.ActualAmount
    ,(
        SELECT SUM(b.ActualAmount)
        FROM UnpaidManagement..Actual b
        WHERE b.ID <= a.ID
    ) RunningTotal
FROM #TempDays AS t
INNER JOIN UnpaidManagement..Actual AS a ON a.IDENTITYCOL = t.days
ORDER BY t.days

完成此操作后,cou可以添加CASE WHEN以生成NULL单元格的内容。