将simiar记录分组为一个

时间:2014-05-14 08:07:47

标签: sql sql-server

我正在开展一个社交项目,并且有一些表格会有相似的记录,我希望将它们合并为一个。但是我的查询会带来所有记录,而不是将它们分组为一个。

SELECT 
    users_pics.email
    , pic = MIN(pic)
    , count(pic) AS total
    ,fname
    ,lname
    ,profile_pix
    ,RTRIM(wardrobe) as wardrobe
    ,gender
    , resp_email

FROM 
    users_pics  
INNER JOIN 
    profile 
ON 
    users_pics.email = profile.email, dbo.viva_friends

WHERE 
    users_pics.stat = 'new'  
AND  
    resp_email = MMColParam2  
OR   
    req_email= MMColParam 
AND 
    Status = 'Yes '
GROUP BY 
    users_pics.email
    ,fname,lname
    ,profile_pix
    ,wardrobe
    ,gender
    ,req_email
    ,resp_email

ORDER BY 
    MAX(users_pics.u_pic_id) DESC

请有出路。当我点击运行按钮时,我得到了这个。

enter image description here

1 个答案:

答案 0 :(得分:0)

看起来你只想要一个简单的GROUP BY;

SELECT users_pics.email, MIN(pic) AS pic, COUNT(pic) AS total, MAX(fname) AS fname,
       MAX(lname) AS lname, MAX(profile_pix) AS profile_pic, 
       RTRIM(MAX(wardrobe)) AS wardrobe, MAX(gender) AS gender, MAX(resp_email) resp_email
FROM users_pics  
INNER JOIN profile 
ON users_pics.email = profile.email, 
     dbo.viva_friends
WHERE (users_pics.stat = 'new' AND resp_email = MMColParam2)
   OR (req_email= MMColParam AND Status = 'Yes ')
GROUP BY 
    users_pics.email

由于我无法看到您的表格结构,因此我无法说明dbo.viva_friends的用途。看起来可能只是删除。