如何创建SQL条件显示查询?

时间:2015-01-12 01:19:04

标签: sql

我需要以下查询的帮助。我知道这与Case When语句有关,但我无法完成查询而没有错误。这就是我所拥有的。

select * 
from tbl_mstr left outer join tbl_1
on tbl_mstr.dt = tbl_1.dt
left outer join tbl_2
on tbl_mstr.dt = tbl_2.dt
left outer join tbl_3
on tbl_mstr.dt = tbl_3.dt

代码:

根据条件提取数据并显示警告 您有一个包含日期敏感记录的主表。

您有3个其他包含相关记录和卷的子表。

三个子表包含相同的信息,但T1被认为比T2更准确,并且被认为比T3更准确。 这些表可能有也可能没有给定日期的信息。

你的任务:

1. To pull all information from the master table
2.  Pull the most accurate volume in as a column (vol)
3.  Create a warning flag for all three child tables if the volume is over 50 or below -50
4.  Do this with the minimal amount of code
5.  No loops
6.  No sub queries
7.  No temp tables

* /

设置NOCOUNT ON;

DECLARE @tbl_mstr TABLE (id int, nm varchar(50), dt datetime)
DECLARE @tbl_1 TABLE (id int, dt datetime, vol float)
DECLARE @tbl_2 TABLE (id int, dt datetime, vol float)
DECLARE @tbl_3 TABLE (id int, dt datetime, vol float)

INSERT @tbl_mstr VALUES(1,'Helga','10/1/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/2/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/3/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/4/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/5/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/6/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/7/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/8/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/9/2009')
INSERT @tbl_mstr VALUES(1,'Helga','10/10/2009')

INSERT INTO @tbl_1 VALUES(1,'10/2/2009',25)
INSERT INTO @tbl_1 VALUES(1,'10/8/2009',42)
INSERT INTO @tbl_1 VALUES(1,'10/9/2009',38)

INSERT INTO @tbl_2 VALUES(1,'10/1/2009',-55)
INSERT INTO @tbl_2 VALUES(1,'10/3/2009',69)
INSERT INTO @tbl_2 VALUES(1,'10/8/2009',74)
INSERT INTO @tbl_2 VALUES(1,'10/10/2009',16)

INSERT INTO @tbl_3 VALUES(1,'10/1/2009',08)
INSERT INTO @tbl_3 VALUES(1,'10/4/2009',37)
INSERT INTO @tbl_3 VALUES(1,'10/5/2009',75)
INSERT INTO @tbl_3 VALUES(1,'10/6/2009',-22)
INSERT INTO @tbl_3 VALUES(1,'10/7/2009',-64)

结果应如下所示:

id   nm       dt                           vol      T1_Flag     T2_Flag       T3_Flag
1    Helga    2009-10-01 00:00:00:00.000   -55      OK          T2 Wrng       Ok
1    Helga    2009-10-02 00:00:00:00.000    25      OK          Ok            Ok
1    Helga    2009-10-03 00:00:00:00.000    69      Ok          T2 Wrng       Ok
1    Helga    2009-10-04 00:00:00:00.000    37      Ok          Ok            Ok
1    Helga    2009-10-05 00:00:00:00.000    75      Ok          Ok            T3 Wrng
1    Helga    2009-10-06 00:00:00:00.000   -22      Ok          Ok            Ok    
1    Helga    2009-10-07 00:00:00:00.000   -64      Ok          Ok            T3 Wrng
1    Helga    2009-10-08 00:00:00:00.000    42      Ok          T2 Wrng       Ok
1    Helga    2009-10-09 00:00:00:00.000    38      Ok          Ok            Ok
1    Helga    2009-10-10 00:00:00:00.000    16      Ok          Ok            Ok

1 个答案:

答案 0 :(得分:0)

这个使用COALESCE作为表格标志的vol和CASE语句。

SELECT
    m.id,
    m.nm,
    m.dt,
    vol = COALESCE(t1.vol, t2.vol, t3.vol),
    T1_Flag = CASE WHEN t1.vol > 50 OR t1.vol < -50 THEN 'T1 Wrng' ELSE 'Ok' END,
    T2_Flag = CASE WHEN t2.vol > 50 OR t2.vol < -50 THEN 'T2 Wrng' ELSE 'Ok' END,
    T3_Flag = CASE WHEN t3.vol > 50 OR t3.vol < -50 THEN 'T3 Wrng' ELSE 'Ok' END
FROM @tbl_mstr m
LEFT JOIN @tbl_1 t1 ON t1.id = m.id AND t1.dt = m.dt
LEFT JOIN @tbl_2 t2 ON t2.id = m.id AND t2.dt = m.dt
LEFT JOIN @tbl_3 t3 ON t3.id = m.id AND t3.dt = m.dt