我有一張這樣的桌子..
| 稅種 | 應稅 | 免稅 |
|---|---|---|
| 混合 | 10 | 20 |
我的問題是,如何在沒有Union條款的情況下得到這樣的結果?
| 稅種 | 應稅 | 免稅 |
|---|---|---|
| 應稅 | 10 | 0 |
| 免稅 | 0 | 20 |
這意味著,這不是我要找的
select taxtype,taxable,taxfree into #tb
from (values('mixed',10,20)) tb(taxtype,taxable,taxfree)
select 'taxable' taxtype,
taxable,
0 taxfree
from #tb
union all
select 'taxfree',
0,
taxfree
from #tb
drop table if exists #tb
uj5u.com熱心網友回復:
使用表值建構式
select v.*
from #tb t
cross apply
(
values
('taxable', taxable, 0),
('taxfree', 0, taxfree)
) v (taxtype, taxable, taxfree)
uj5u.com熱心網友回復:
不幸的是,盡管有 Squirrel 的聰明支持,我還是接受了Union作為我的最終解決方案。只是添加一些修訂..
select taxtype,taxable,taxfree into #src
from (
values
('taxable',5,0),
('taxfree',0,10),
('mixed',15,20)
) src(taxtype,taxable,taxfree)
select res.*
from #src s
cross apply
(select *
from (
values
('taxable', s.taxable, 0, s.taxtype),
('taxfree', 0, s.taxfree, s.taxtype)
) v (taxtype, taxable, taxfree, origin)
where not (v.taxable = 0 and v.taxfree = 0)) res
drop table if exists #src
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/492721.html
上一篇:我需要將日歷應用程式的行轉換為列
