填补表格中缺少的日子

时间:2019-05-02 07:49:20

标签: sql sql-server stored-procedures

您能在sql中帮我吗,我有从中获取日常数据的表。 假设:

Select * from #tempTable

结果:-

Day      Amount
-----------------   
Saturday 16128.00
Monday  527008.00
Tuesday 407608.00

在上述查询中,您会看到缺少结果“ SUNDAY”,因此如何填写这几天。

1 个答案:

答案 0 :(得分:1)

尝试一下

SELECT  DayNames, [Amount]
FROM
(
SELECT 'Saturday' AS [Day], 16128.00  AS [Amount] UNION ALL
SELECT 'Monday'  ,527008.00 UNION ALL
SELECT 'Tuesday' ,407608.00
)DT
RIGHT JOIN 
(
SELECT DATENAME(dw,DATEADD(Dw,number,GETDATE())) As DayNames
FROM
    (
        SELECT DISTINCT number 
        FROM master.dbo.spt_values
        WHERE number BETWEEN 1 AND 7
        )dt2
    )Final
ON Final.DayNames = [Day]

结果

DayNames    Amount
-----------------------
Friday      NULL
Saturday    16128.00
Sunday      NULL
Monday      527008.00
Tuesday     407608.00
Wednesday   NULL
Thursday    NULL