Returning values from a temp table if another temp table returns no rows
如果另一个临时表没有返回行,我需要编写一个从一个临时表返回值的查询。
编辑:整个表具有以下结构
1 2 3 4 5 6 7 8 9 10 | NAME TYPE DATE VALUE Washington 1 NULL 1000 <--default value Washington 2 NULL 750 <--default value Washington 3 NULL 500 <--default value Washington 1 04.11.2015 500 Washington 2 04.11.2015 250 Washington 3 04.11.2015 100 Washington 1 07.11.2015 600 Washington 2 07.11.2015 300 Washington 3 07.11.2015 200 |
临时表:
临时表:
当日期列没有条目时,这意味着值应该是"默认",它们存储在同一个表中日期为空的行下
我也有 8 个不同的参数来确定值,但我只对 '1' 和 '2' 感兴趣,因此是 'case when'
所以我最后想要得到的是:
1 2 3 | Select * from temp1 where date =GETDATE() |
如果它不存在,我希望它返回:
1 | select * from temp2. |
我只对今天、明天和后天的结果感兴趣
这是我到目前为止所得到的,但它显然不起作用。
如果能得到任何帮助,我将不胜感激
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | WITH defaultvalues AS ( SELECT DISTINCT n.Name , Type , Value FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id AND Date IS NULL ), changedvalue AS ( SELECT DISTINCT n.Name , type , date , CASE WHEN d.Date = GETDATE() AND Parameter = '1' THEN Value WHEN Parameter = '2' THEN '0' END AS CAP FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id WHERE Parameter IN ( '1', '2' ) ) SELECT CASE WHEN NOT EXISTS ( SELECT * FROM changedvalue WHERE changedvalue.Date = GETDATE() ) THEN ( SELECT * FROM defaultvalues ) END |
您没有提供示例数据,只是基于您的 SQL 认为您只是在最后一部分遇到了问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | WITH defaultvalues AS ( SELECT DISTINCT n.Name, [type], [date], [Value] AS cap FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id AND [Date] IS NULL ), changedvalue AS ( SELECT DISTINCT n.Name, [type], [date], CASE WHEN d.Date = GETDATE() AND Parameter = '1' THEN [Value] WHEN Parameter = '2' THEN '0' END AS CAP FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id WHERE Parameter IN ( '1', '2' ) ) SELECT * FROM [changedvalue] UNION SELECT * FROM [defaultvalues] WHERE NOT EXISTS ( SELECT * FROM [changedvalue] ); |
使用 Union 来组合两个答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | WITH defaultvalues AS ( SELECT DISTINCT n.Name , Type , Value FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id AND Date IS NULL ), changedvalue AS ( SELECT DISTINCT n.Name , type , date , CASE WHEN d.Date = GETDATE() AND Parameter = '1' THEN Value WHEN Parameter = '2' THEN '0' END AS CAP FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id WHERE Parameter IN ( '1', '2' ) ) ( SELECT * FROM changedvalue WHERE changedvalue.Date = GETDATE() ) UNION ( SELECT * FROM defaultvalues WHERE NOT EXISTS (SELECT 1 FROM changedvalue )) END |
这是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | WITH defaultvalues AS ( SELECT DISTINCT n.Name , Type , Value FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id AND Date IS NULL ), changedvalue AS ( SELECT DISTINCT n.Name , type , date , CASE WHEN d.Date = GETDATE() AND Parameter = '1' THEN Value WHEN Parameter = '2' THEN '0' END AS CAP FROM mytable LEFT JOIN nametable AS n ON mytable.x = r.Id WHERE Parameter IN ( '1', '2' ) ), final AS ( SELECT * , RANK() OVER ( ORDER BY c ) r FROM ( SELECT * , 1 AS c FROM changedvalue UNION ALL SELECT * , 2 AS c FROM defaultvalues ) t ) SELECT * FROM final WHERE r = 1 |
您只需将结果与不同的
如果没有样本数据,很难阅读和完全理解您的问题,但您似乎希望根据条件从两个表之一返回值。
看到两个临时表的架构相同,实现此目的的一种方法是简单地
我尝试创建一个简化的示例来展示如何执行此操作,如果您将以下代码复制并粘贴到 Management Studio 中,则该代码可以运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | CREATE TABLE #temp1 (Name VARCHAR(10), [type] VARCHAR(10), [date] DATE, value int) CREATE TABLE #temp2 (Name VARCHAR(10), [type] VARCHAR(10), [date] DATE, value int) INSERT INTO #temp1 ( Name, type, date, value ) VALUES ( 'bob', 'male', '2015-03-12', 123) INSERT INTO #temp2 ( Name, type, date, value ) VALUES ( 'jane', 'female', GETDATE(), 456) DECLARE @switch BIT = 1 -- with @switch set to 1, #temp1 will be returned SELECT t1.* FROM ( SELECT * FROM #temp1 WHERE @switch = 1 UNION SELECT * FROM #temp2 WHERE @switch = 0 ) t1 SET @switch = 0 --with @switch set to 0, #temp2 will be returned SELECT t1.* FROM ( SELECT * FROM #temp1 WHERE @switch = 1 UNION SELECT * FROM #temp2 WHERE @switch = 0 ) t1 DROP TABLE #temp1 DROP TABLE #temp2 |
我在