2012年5月23日 星期三

[SQL] Subquery

Example:
select 
    convert(varchar(10), DATEADD(DAY, -1, GETDATE()), 120) AS [...],
    SUM(CASE WHEN [C1] = ... THEN 1 ELSE 0 END) AS [...],
    SUM(CASE WHEN [C1] = ... THEN 1 ELSE 0 END) AS [...],
    SUM(CASE WHEN [C1] = ... THEN 1 ELSE 0 END) AS [...],
from T where [D] in 
(
    select max([D]) from ...
    where
        [D] >= convert(varchar(10), DATEADD(DAY, 0, GETDATE()), 120)
        and [D] < convert(varchar(10), DATEADD(DAY, -1, GETDATE()), 120)
    group by [C2]
)

沒有留言:

張貼留言