星期四, 9月 24, 2009

MS SQL 重覆性資料表處理 Repeat rows ,Group by

第一篇:
--出現多筆時抓取最大值
Select 欄位一,max(欄位二) 欄位二
From Table
Group By 欄位一
--出現多筆時抓取最小值
Select 欄位一,min(欄位二) 欄位二
From Table
Group By 欄位一




第二篇:
sql: why does query repeat values when using ‘GROUP CONCAT’ + ‘GROUP BY’?
(目前沒用到,留著日後參考)

The Query:
SELECT MemberId, a.MemberName, GROUP_CONCAT(FruitName) FROM a LEFT JOIN b ON
a.MemberName = b.MemberName GROUP BY a.MemberName


Table a
MemberID MemberName
-------------- ----------
1 Al
1 Al
3 A2

Table b
MemberName FruitName
--------------- --------------
Al Apple
Al Mango
A2 Cherry

Resulting Output from above query:
MemberId MemberName GROUP_CONCAT(FruitName)
3 A2 Cherry
1 A1 Apple,Apple,Mango,Mango

The actual tables I am using have 10 columns apiece so just storing everything in one table is not a workaround. That said, how can I change the query to only return 'Apple,Mango' for MemberName?

Reference:
如何篩選掉重覆的值
sql: why does query repeat values when using ‘GROUP CONCAT’ + ‘GROUP BY’?(目前沒用到,留著日後參考)

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails