SQL在SELECT中生成缺失记录

时间:2013-04-16 06:32:59

标签: sql sql-server-2000 temp-tables

使用MS SQL Server 8.0.760(2000)

我有一个像这样的表:

Table A
Day       | Hour | Value
2012-10-01| 12   | 780
2012-10-01| 14   | 678
2012-11-02| 08   | 123
2012-11-02| 09   | 473

预期结果应为:

Day       | Hour | Value
2012-10-01| 00   | 0
2012-10-01| 01   | 0
2012-10-01| 02   | 0
2012-10-01| ..   | ..
2012-10-01| 12   | 780
2012-10-01| 13   | 0
2012-10-01| 14   | 678
2012-10-01| ..   | ..
2012-10-01| 22   | 0
2012-10-01| 23   | 0
2012-10-01| 24   | 0
2012-11-02| 00   | 0
2012-11-02| 01   | 0
2012-11-02| 02   | 0
2012-11-02| ..   | ..
2012-11-02| 08   | 123
2012-11-02| 09   | 473
2012-11-02| ..   | ..
2012-11-02| 22   | 0
2012-11-02| 23   | 0
2012-11-02| 24   | 0

因此,零值会产生缺失的小时数。

任何想法?

编辑1

试过这个:

DECLARE @tmpHours TABLE
(
tmpHour varchar(2)
)

INSERT INTO @tmpHours VALUES ('00')
INSERT INTO @tmpHours VALUES ('01')
...
INSERT INTO @tmpHours VALUES ('23')
INSERT INTO @tmpHours VALUES ('24')

SELECT * FROM [A]
  FULL JOIN @tmpHours tmp ON tmp.[tmpHour] = [A].[Hour]
  ORDER BY [Day], [Hour], [tmpHour]

但这会产生这个:

Day Hour    Value   tmpHour
NULL    NULL    NULL    00
NULL    NULL    NULL    01
NULL    NULL    NULL    02
NULL    NULL    NULL    03
NULL    NULL    NULL    04
NULL    NULL    NULL    05
NULL    NULL    NULL    06
NULL    NULL    NULL    07
NULL    NULL    NULL    10
NULL    NULL    NULL    11
NULL    NULL    NULL    13
NULL    NULL    NULL    15
NULL    NULL    NULL    16
NULL    NULL    NULL    17
NULL    NULL    NULL    18
NULL    NULL    NULL    19
NULL    NULL    NULL    20
NULL    NULL    NULL    21
NULL    NULL    NULL    22
NULL    NULL    NULL    23
NULL    NULL    NULL    24
2012-10-01 00:00:00.000 12  780 12
2012-10-01 00:00:00.000 14  678 14
2012-11-02 00:00:00.000 08  123 08
2012-11-02 00:00:00.000 09  473 09

2 个答案:

答案 0 :(得分:1)

您可以创建一个名为容器的表(可能是临时的)您的小时(您的示例显示从00到24的25小时,但我想您需要24小时)。然后,您可以使用HOURS表在Table A之间进行外部联接。这将生成NULL值而不是0值。如果需要,可以使用函数将NULL值转换为0。

编辑重构了单个SQL查询的答案:

SELECT X.*, A.VALUE
FROM  A
RIGHT OUTER JOIN
(
SELECT *
FROM
       (SELECT DISTINCT A.DAY FROM A) DAYS,
       (SELECT 0 HOUR
        UNION ALL SELECT 1 HOUR
        UNION ALL SELECT 2 HOUR
        UNION ALL SELECT 3 HOUR
        UNION ALL SELECT 4 HOUR
        UNION ALL SELECT 5 HOUR
        UNION ALL SELECT 6 HOUR
        UNION ALL SELECT 7 HOUR
        UNION ALL SELECT 8 HOUR
        UNION ALL SELECT 9 HOUR
        UNION ALL SELECT 10 HOUR
        UNION ALL SELECT 11 HOUR
        UNION ALL SELECT 12 HOUR
        UNION ALL SELECT 13 HOUR
        UNION ALL SELECT 14 HOUR
        UNION ALL SELECT 15 HOUR
        UNION ALL SELECT 16 HOUR
        UNION ALL SELECT 17 HOUR
        UNION ALL SELECT 18 HOUR
        UNION ALL SELECT 19 HOUR
        UNION ALL SELECT 20 HOUR
        UNION ALL SELECT 21 HOUR
        UNION ALL SELECT 22 HOUR
        UNION ALL SELECT 23 HOUR
        ) HOURS
) X
ON X.DAY = A.DAY AND X.HOUR = A.HOUR

答案 1 :(得分:0)

尝试以下步骤。这将非常有活力。 (注意:相同日期00和24不会出现)

--Create Table :
Create Table #Table
(
    Day Date,
    Hour Int,
    Value Int
)
Go

-- Insert Values :
Insert into #Table Values('2012-10-01','12','780')
Insert into #Table Values('2012-10-01','14','678')
Insert into #Table Values('2012-10-02','08','123')
Insert into #Table Values('2012-10-02','09','473')
Go

--View Data :
Select * from #Table

Declare @TempTable as Table
(Day Date,Hour Int,Value Int)

Declare @Date Date
Declare @i Int
Set @i = 0

--Using Cursor :
Declare cur  Cursor
for
Select Distinct Day from #Table 

Open cur
Fetch Next From cur Into  @Date

While @@Fetch_status = 0
BEGIN
      While (@i <=23)
      Begin
            If not exists (Select 1 from #Table Where Day =@Date and Hour = @i )
            Begin
                    Insert into @TempTable Values (@Date,@i,0)      
            End 
            Else
            Begin
                Insert into @TempTable 
                Select Day,Hour,Value from #Table Where Day =@Date and Hour = @i                    
            End         

            Set @i = @i + 1         
        End
        Fetch Next From cur Into @Date
End      
Close cur
Deallocate cur

--Results :
Select * from @TempTable

--Clean Up :
Drop Table #Table