SQL Server 2008 R2即使没有结果也返回一行

时间:2018-08-21 16:34:10

标签: sql sql-server

我有一个包含以下某些数据的表:

+------+--------+-----+------+
| Team | Status | Bye | Wins |
+------+--------+-----+------+
| Cle  | New    |   1 |    5 |
| Tam  | New    |   3 |    0 |
| Bal  | New    |   3 |    2 |
| Ind  | Cur    |   4 |    0 |
| LAC  | New    |   4 |    2 |
| NYG  | Cur    |   5 |    0 |
| Chi  | Cur    |   5 |    0 |
+------+--------+-----+------+

有5个再见周,查询可能会返回没有特定再见周的结果,在这种情况下为2个。我想从表中返回一个记录集,但还要返回一行,在“再见”周中缺少数字,而该行中的其他所有内容都为空(其中没有“再见”)。我想我可以使用UNION做到这一点,但似乎需要做很多工作,并希望有一种更简单,更有效的方法。

我看过其他帖子返回一个空行,但这些帖子不包括返回其他有效行。感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您可以将values构造与join一起使用:

select t.bye, tt.*
from ( values (2) 
     ) t (bye) left join
     table tt
     on t.bye = tt.bye;

编辑:再次查看问题后,您似乎希望将unionnot exists一起使用:

select t.team, t.status, t.bye, t.wins
from table t
where bye = 2
union all
select t.team, t.status, t.bye, t.wins
from table t
where not exists (select 1 from table where bye = 2);

编辑:使用递归cte查找丢失的bye

with t as (
     select min(bye) mn_bye, max(bye) mx_bye
     from table
     union all
     select mn_bye + 1, mx_bye
     from t
     where mn_bye < mx_bye
)

select tt.*, coalesce(cast(t.mn_bye as varchar(255)), 'missing') 
from t left join
     table tt
     on tt.bye = t.mn_bye;

答案 1 :(得分:2)

这里还有其他一些很好的答案,但是我将分享在类似情况下所做的事情。基本上,我有一个“默认值”表,可以将其UNION合并到该表中,然后仅返回该表中所有缺少的行。

根据上面的示例数据,我创建了以下示例,可以在SSMS中运行。

-- replicate your environment --

DECLARE @data TABLE ( [team] VARCHAR(3), [status] VARCHAR(3), [bye] INT, [wins] INT );
INSERT INTO @data ( [team], [status], [bye], [wins] )
VALUES
  ( 'Cle', 'New', 1, 5 )
, ( 'Tam', 'New', 3, 0 )
, ( 'Bal', 'New', 3, 2 )
, ( 'Ind', 'Cur', 4, 0 )
, ( 'LAC', 'New', 4, 2 )
, ( 'NYG', 'Cur', 5, 0 )
, ( 'Chi', 'Cur', 5, 0 );

-- declare a "defaults" table variable --

DECLARE @defaults TABLE ( [team] VARCHAR(2) DEFAULT '--', [status] VARCHAR(2) DEFAULT '--', [bye] INT, [wins] INT );

-- insert default data based on what I surmised from your question --

INSERT INTO @defaults ( [bye], [wins] ) VALUES ( 1, 0 ), ( 2, 0 ), ( 3, 0 ), ( 4, 0 ), ( 5, 0 );

-- select data using the "defaults" table to "fill in the wholes" --

SELECT [team], [status], [bye], [wins] FROM @data
UNION
SELECT [team], [status], [bye], [wins] FROM @defaults
WHERE [bye] NOT IN ( SELECT [bye] FROM @data )
ORDER BY
    [bye], [team];

返回哪个

+------+--------+-----+------+
| team | status | bye | wins |
+------+--------+-----+------+
| Cle  | New    |   1 |    5 |
| --   | --     |   2 |    0 |
| Bal  | New    |   3 |    2 |
| Tam  | New    |   3 |    0 |
| Ind  | Cur    |   4 |    0 |
| LAC  | New    |   4 |    2 |
| Chi  | Cur    |   5 |    0 |
| NYG  | Cur    |   5 |    0 |
+------+--------+-----+------+

另一种替代方法是扩展@Yogesh对FROM ( VALUES )的使用:

SELECT [team], [status], [bye], [wins] FROM @data
UNION
SELECT [team], [status], [bye], [wins] 
FROM ( 
    VALUES ( '--', '--', 1, 0 ), ( '--', '--', 2, 0 ), ( '--', '--', 3, 0 ), ( '--', '--', 4, 0 ), ( '--', '--', 5, 0 )
 ) AS MyTable( [team], [status], [bye], [wins] )
WHERE [bye] NOT IN ( SELECT [bye] FROM @data )
ORDER BY
    [bye], [team];

不需要表变量/不需要表变量就能返回与上面相同的结果。

答案 2 :(得分:1)

我故意删除了Bye个,其中有4个,以便可以更好地看到结果。

declare @t table
(
    Team     char(3) null,
    [Status] char(3) null,
    Bye      tinyint null,
    Wins     tinyint null
);

insert into @t values
('Cle', 'New', 1, 5),
('Tam', 'New', 3, 0),
('Bal', 'New', 3, 2),
--('Ind', 'Cur', 4, 0),
--('LAC', 'New', 4, 2),
('NYG', 'Cur', 5, 0),
('Chi', 'Cur', 5, 0);

with info as
(
    select x.*, x.next_row_bye - Bye diff
    from
    (
        select *,
               LEAD(Bye) OVER (ORDER BY Bye) next_row_bye,
               Bye + 1 next_bye
        from @t
    ) x
)
select null Team, null [Status], next_bye Bye, 0 Wins
from info
where diff > 0
union
select *
from @t
order by Bye;

结果:

IMG1