SQL order by 找不到解决方法
SQL order by can't find a solution
我正在努力在查询中进行排序。
想象一下当前查询的以下结果。
TableA.Id ---- TableB.TagName ---- TableB.TagValue
1----------------A---------------------------Customer A
1----------------B---------------------------Contract B
1----------------C---------------------------Product Z
2----------------A---------------------------Customer B
2----------------B---------------------------Contract C
2----------------C---------------------------Product Y
3----------------A---------------------------Customer C
3----------------B---------------------------Contract D
3----------------C---------------------------Product X
因此排序是动态的,可以是 A、B、C 升序或降序 (TableB.TagName)
如果用户选择 C ASC 作为排序,结果应该是:
TableA.Id ---- TableB.TagName ---- TableB.TagValue
3----------------A---------------------------Customer C
3----------------B---------------------------Contract D
3----------------C---------------------------Product X
2----------------A---------------------------Customer B
2----------------B---------------------------Contract C
2----------------C---------------------------Product Y
1----------------A---------------------------Customer A
1----------------B---------------------------Contract B
1----------------C---------------------------Product Z
非常感谢您的帮助
哦,我明白了。
您可以在 order by
:
中使用子查询
select a.id, b.tagname, b.tagvalue,
max(case when b.tagname = @usertagname then b.tagvalue end) over (partition by a.id) as tagtagvalue
from tablea a join
tableb b
on a.tagid = b.tagid
order by tagtagvalue, a.id, b.tagname;
要获得降序排序,您需要在 `order by.
中使用 case
表达式
我正在努力在查询中进行排序。 想象一下当前查询的以下结果。
TableA.Id ---- TableB.TagName ---- TableB.TagValue
1----------------A---------------------------Customer A
1----------------B---------------------------Contract B
1----------------C---------------------------Product Z
2----------------A---------------------------Customer B
2----------------B---------------------------Contract C
2----------------C---------------------------Product Y
3----------------A---------------------------Customer C
3----------------B---------------------------Contract D
3----------------C---------------------------Product X
因此排序是动态的,可以是 A、B、C 升序或降序 (TableB.TagName) 如果用户选择 C ASC 作为排序,结果应该是:
TableA.Id ---- TableB.TagName ---- TableB.TagValue
3----------------A---------------------------Customer C
3----------------B---------------------------Contract D
3----------------C---------------------------Product X
2----------------A---------------------------Customer B
2----------------B---------------------------Contract C
2----------------C---------------------------Product Y
1----------------A---------------------------Customer A
1----------------B---------------------------Contract B
1----------------C---------------------------Product Z
非常感谢您的帮助
哦,我明白了。
您可以在 order by
:
select a.id, b.tagname, b.tagvalue,
max(case when b.tagname = @usertagname then b.tagvalue end) over (partition by a.id) as tagtagvalue
from tablea a join
tableb b
on a.tagid = b.tagid
order by tagtagvalue, a.id, b.tagname;
要获得降序排序,您需要在 `order by.
中使用case
表达式