0
我有一個查詢這給了我非常好的結果:SQL - 作爲子查詢的結果添加一列? SQL Server 2008的
select
A.ID_acc, A.ID_us, A.st, table3.KFL,
'100' as myattribute,
'101' as my attribute2
from
SOURCE1 as A
left join
(select
table2.ID_us, table2.ID_acc,
CASE WHEN table2.KFL_type = 'KFL' THEN P.index_num ELSE table2.KFL_type END as KFL
from
(select
table1.ID_us, table1.ID_acc,
CASE WHEN sum(table1.count_kfl) > 1 THEN '9999' WHEN sum(table1.count_kfl) = 1 THEN 'KFL' END as KFL_type
from
(SELECT
ID_us, ID_acc, count(*) as count_kfl
FROM
payments
WHERE
index_num IN (200, 201, 203)
AND (date >= XXXX or date2 >= 'XXXXX')
GROUP BY
1, 2) as table1
group by
1, 2) as table2
join
SOURCE2 as P on table2.ID_us = P.ID_us
and table2.ID_acc = P.ID_acc
where
(P.date>= XXXX or P.date2 >= 'XXXXX')
and index_num in (201,201,203)
group by
1, 2
order by
1, 2) as table3 on table3.ID_us = A.ID_us
and table3.ID_acc = A.ID_acc
where
A.not_deleted >= XXXXXX
這個查詢是不是我的主要問題,所以我只有它複製只是短暫短暫,但我不知道我怎麼能現在就加一個更多的附加列(計數操作的結果)作爲我的第一個查詢結束?只是不要單獨製作2,然後混合結果。當然,我不想影響我早期的領域結果。
我有一個看起來像這樣第二個查詢:
select A.ID_us, count(*)/2 as number
from
SOURCE1 as A
left join
SOURCE3 as B
on A.ID_acc = B.ID_acc
where A.date >= XXXX
group by 1
的2個查詢之間的聯繫是其中出現在第一和第二中查詢源的屬性ID_acc。
但是不知道它是如何做到的?
select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute, '101' as my attribute2, NEWSOURCE.MYNEW_attribute
from SOURCE1 as A
left join
(
...
)
as table3 on table3.ID_us = A.ID_us and table3.ID_acc = A.ID_acc
where A.not_deleted >= XXXXXX
left join
(
.
.
.
)
as NEWSOURCE
像這樣的事情,當然,不工作:///