日期:2014-05-19  浏览次数:20787 次

请教各位一个SQL语句,帮忙看看。谢谢
现有一个表:
tbl:
内容:

code     name     qty       other   qtys
A         AAA         10         001       1
A         AAA         10         002       5
A         AAA         10         003       4
如何显示:
code     name     qty       other   qtys
A           AAA         10         001       1
                                      002       5
                                      003       4



------解决方案--------------------
1、用临时表、临时表变量 可以很方便的实现
需求有些少见 :)

现查出
A AAA 10 第一条记录

To Temp

再查出前三列为A AAA 10的记录 insert 后两条记录 :)

直接联接查询不太可行
------解决方案--------------------
--如果other這列是不重復,且有序的, 可以實現,但是效率不夠好,還是建議在前台去實現
Select
(Case When B.code Is Null Then ' ' Else A.code End) As code,
(Case When B.code Is Null Then ' ' Else A.name End) As name,
(Case When B.code Is Null Then Null Else A.qty End) As qty,
A.other,
A.qtys
From
tbl A
Left Join
(Select code, name, qty, Min(other) As other From tbl Group By code, name, qty)B
On A.code = B.code And A.name = B.name And A.qty = B.qty And A.other = B.other