简体   繁体   English

需要以以下格式设置SQL结果

[英]Need SQL Result set in below format

I have table in below format 我有以下格式的表格

ID  TotalAmount AdjStatus
--------------------------
1   0           Approved
2   0           Closed
3   0           Disputed

I need the result set in multiple rows as below based on the column value AdjStatus. 我需要基于列值AdjStatus的多行结果集,如下所示。

ID  TotalAmount AdjStatus
---------------------------------
1   0           Approved
1   null        Closed
1   null        Disputed
2   0           Closed
2   null        Approved
2   null        Disputed
3   0           Disputed
3   null        Approved
3   null        Closed

Try to use CROSS JOIN in the following: 尝试在以下情况下使用CROSS JOIN

select 
    t.Id, 
    case when t.AdjStatus <> t2.AdjStatus then null else t.TotalAmount end as TotalAmount,
    t2.AdjStatus
from #test as t
cross join #test t2
order by t.Id, case when t.AdjStatus <> t2.AdjStatus then null else t.TotalAmount end desc

You could test it at SQL Fiddle 您可以在SQL Fiddle上对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM