如果为空表,则将日期提取为null

时间:2014-06-30 23:25:17

标签: sql sql-server

我有一张地点表

SELECT 
    "location_id",
    "location_code",
    "location_line_1",
    "location_line_2",
    "location_line_3",
    "location_line_4"
FROM
    "location"

带来

1    AM00758    line1    line2    line3    line4
2    PF00517    line1    line2    line3    line4
3    RFTA967    line1    line2    line3    line4

第二个表格,用于存档当天所在位置的排除数量

SELECT 
    "daily_exclusion_id",
    "location_id", --From Location
    "exclusion_date",
    "exclution_absence",
    "exclution_no_info",
    "exclusion_foregin_info"
FROM
    "daily_exclusion"

带来

1    1    2014-06-01    15    32    45
2    2    2014-06-01    23    10    81
3    3    2014-06-01    18    30    70
4    1    2014-06-02    63    34    12
5    2    2014-06-02    34    16    2
6    3    2014-06-02    51    18    9
7    1    2014-06-04    13    18    7
8    2    2014-06-04    18    36    19
9    3    2014-06-04    5     7     29

所以......要在屏幕上填写此内容,它会打印所选日期的每个位置,并使用排除的人数(缺席,没有信息和外包信息)填写3个排除项

问题: 有些日子,信息是空的,如果用户注册第1,2天,并且错过3,但继续进行4,5等...当我做选择时,它不会将那些丢失的日期作为空

SELECT 
    l.*, de.*
FROM
    location l
LEFT OUTER JOIN
    daily_exclusion de
ON (
    de.location_id = l.location_id
AND
    de.exclusion_date between '20140601' and '20140604' --HERE'S THE DIFFERENCE
)
WHERE
vl.location_id IN (1,2,3)

但它只会带来注册日期

1    AM00758    line1    line2    line3    line4    1    1    2014-06-01    15    32    45
1    AM00758    line1    line2    line3    line4    4    1    2014-06-02    63    34    12
1    AM00758    line1    line2    line3    line4    7    1    2014-06-04    13    18    7
2    PF00517    line1    line2    line3    line4    2    2    2014-06-01    23    10    81
2    PF00517    line1    line2    line3    line4    5    2    2014-06-02    34    16    2
2    PF00517    line1    line2    line3    line4    8    2    2014-06-04    18    36    19
3    RFTA967    line1    line2    line3    line4    3    3    2014-06-01    18    30    70
3    RFTA967    line1    line2    line3    line4    6    3    2014-06-02    51    18    9
3    RFTA967    line1    line2    line3    line4    9    3    2014-06-04    5     7     29

我需要第3天的空值

1    AM00758    line1    line2    line3    line4    1    1    2014-06-01    15    32    45
1    AM00758    line1    line2    line3    line4    4    1    2014-06-02    63    34    12
1    AM00758    line1    line2    line3    line4    null null 2014-06-03    null  null null
1    AM00758    line1    line2    line3    line4    7    1    2014-06-04    13    18    7
2    PF00517    line1    line2    line3    line4    2    2    2014-06-01    23    10    81
2    PF00517    line1    line2    line3    line4    5    2    2014-06-02    34    16    2
2    PF00517    line1    line2    line3    line4    null null 2014-06-03    null  null null
2    PF00517    line1    line2    line3    line4    8    2    2014-06-04    18    36    19
3    RFTA967    line1    line2    line3    line4    3    3    2014-06-01    18    30    70
3    RFTA967    line1    line2    line3    line4    6    3    2014-06-02    51    18    9
3    RFTA967    line1    line2    line3    line4    null null 2014-06-03    null  null  null
3    RFTA967    line1    line2    line3    line4    9    3    2014-06-04    5     7     29

为了做到这一点,我找到了一些工作......但我无法负担这个动态查询

SELECT * FROM (
SELECT
    convert(date, '20140601', 120) as the_date
    l.*, de.*
FROM
    location l
LEFT OUTER JOIN
    daily_exclusion de
ON (
    de.location_id = l.location_id
AND
    de.exclusion_date = '20140601' --HERE'S THE DIFFERENCE
)
WHERE
vl.location_id IN (1,2,3)
UNION ALL --CAN'T AFFORD TO DO THIS
SELECT
    convert(date, '20140602', 120) as the_date
    l.*, de.*
FROM
    location l
LEFT OUTER JOIN
    daily_exclusion de
ON (
    de.location_id = l.location_id
AND
    de.exclusion_date = '20140602' --HERE'S THE DIFFERENCE
)
WHERE
vl.location_id IN (1,2,3)
UNION ALL --CAN'T AFFORD TO DO THIS
SELECT
    convert(date, '20140603', 120) as the_date
    l.*, de.*
FROM
    location l
LEFT OUTER JOIN
    daily_exclusion de
ON (
    de.location_id = l.location_id
AND
    de.exclusion_date = '20140603' --HERE'S THE DIFFERENCE
)
WHERE
vl.location_id IN (1,2,3)
)
as t
ORDER BY the_date asc

依旧...... 它就像Returning NULLs in SQL if joined table is missing records for that Date但我没有注册日期的表格

1 个答案:

答案 0 :(得分:1)

您可以考虑使用recursive Common Table Expression (CTE)生成条件日期之间所有日期的列表。

E.g:

WITH cteDates (ADate) AS
(
SELECT CAST('20140601' as date) as ADate

UNION ALL

SELECT DATEADD(day, 1, ADate)
FROM cteDates
WHERE DATEADD(day, 1, ADate) <= CAST('20140604' as date)
)

将其插入查询的开头,并使用cteDates作为另一个表格引用,可能在CROSS JOINLocation