0

I want to pull an SQL statement so that one column (not present in the second table) is conditionally filled based on table source.

Table1
joinid
flag1
cond1
cond2
date

Table2
joinid
flag2
cond2
date

I would like the output to be:

flag1,flag2,cond1,cond2

where cond1 is always 600 for results from table2.

there is a condition that the date must be in a specific range.

Psuedo Ex:

SELECT flag1,flag2,IF tablesource=table1 then cond1 else 600 AS cond1,cond2 
FROM table1 
WHERE date IN (date1,date2,date3, etc) 
LEFT JOIN table2 on table1.joinid=table2.joinid
4

2 回答 2

1

如果我正确阅读了您的问题,则您正在从不同行的两个表中查找结果。这是用 aunion而不是 a完成的left join

select  flag1
,       flag2
,       cond1
,       cond2
from    (
        select  flag1
        ,       null as flag2
        ,       cond1
        ,       cond2
        ,       date
        from    Table1
        union all
        select  null
        ,       flag2
        ,       600
        ,       cond2
        ,       date
        from    Table2
        ) SubQueryAlias
where   '2010-01-01' <= date and date < '2011-01-01'
于 2013-05-13T14:05:20.023 回答
1

是否需要将两个 cond2 字段放入一个目标字段中?
过滤器需要使用哪个日期字段?

假设没有和 table1 ...

SELECT
t1.joinid,
t1.flag1,
t2.flag2,
case when t1.cond1 is not null then cond1 else 600 end AS cond1
t1.cond2 as t1cond2,
t2.cond2 as t2cond2
FROM table1 t1
LEFT JOIN table2 t2 on t1.joinid =t2.joinid
WHERE t1.date IN(date1、date2、date3 等)

于 2013-05-13T14:13:58.523 回答