从SQL结果中删除重复的行

时间:2015-09-22 08:29:52

标签: php sql oracle duplicate-removal

这是我从object(stdClass)#1 (4) { ["chartName"]=> string(7) "Chart#1" ["chartNumber"]=> int(1) ["sensorsArray"]=> object(stdClass)#2 (44) { ["B0"]=> object(stdClass)#3 (5) { ["sensorNumber"]=> int(0) ["name"]=> string(10) "SRBUS(mV)" ["m"]=> string(6) "1.2811" ["b"]=> string(8) "-4.1559" ["selected"]=> bool(false) } ["B1"]=> object(stdClass)#4 (5) { ["sensorNumber"]=> int(1) ["name"]=> string(10) "SRBUS(mA)" ["m"]=> string(6) "0.6137" ["b"]=>... DB

获取结果的SQL查询
ORACLE

它给出了重复SELECT U."UID", CONCAT(CONCAT(U.LNAME, ', '), U.FNAME) AS UNAME, M.NAME, (SELECT COUNT(LH."UID") FROM DRUPAL_SYS.LOGIN_HISTORY LH WHERE LH."UID"=U."UID" ) AS VIEWS_COUNT, L.LOGIN as "ACCESS" FROM DRUPAL_SYS.USERS U INNER JOIN DRUPAL_SYS.LOGIN_HISTORY L ON L."UID" = U."UID" INNER JOIN ABC_WEBAPP.MEMBER_USER_ROLE MUR ON MUR.USER_ID=U.MDES_ID AND MUR.MEMBER_ID LIKE '%' INNER JOIN ABC_WEBAPP.MEMBER M ON M.ID=MUR.MEMBER_ID WHERE U.LOGIN != 0 AND U."UID">1 ORDER BY L."LOGIN" ASC`

的结果

enter image description here

但我的预期结果应该是

enter image description here

3 个答案:

答案 0 :(得分:0)

SELECT U."UID", 
CONCAT(CONCAT(U.LNAME, ', '), U.FNAME) AS UNAME,
M.NAME,
(SELECT COUNT(LH."UID") FROM DRUPAL_SYS.LOGIN_HISTORY LH WHERE LH."UID"=U."UID" ) AS VIEWS_COUNT,
MAX(L.LOGIN) as "ACCESS" FROM DRUPAL_SYS.USERS U 
INNER JOIN DRUPAL_SYS.LOGIN_HISTORY L ON L."UID" = U."UID"
INNER JOIN ABC_WEBAPP.MEMBER_USER_ROLE MUR ON MUR.USER_ID=U.MDES_ID AND MUR.MEMBER_ID LIKE '%' 
INNER JOIN ABC_WEBAPP.MEMBER M ON M.ID=MUR.MEMBER_ID WHERE U.LOGIN != 0 AND U."UID">1
ORDER BY L."LOGIN" ASC`
GROUP BY UNAME, MNAME

L.LOGIN更改为MAX(L.LOGIN)并添加了GROUP BY子句

答案 1 :(得分:0)

SELECT U."UID", 
CONCAT(CONCAT(U.LNAME, ', '), U.FNAME) AS UNAME,
M.NAME,
(SELECT COUNT(LH."UID") FROM DRUPAL_SYS.LOGIN_HISTORY LH WHERE LH."UID"=U."UID" ) AS VIEWS_COUNT,
L.LOGIN as "ACCESS" FROM DRUPAL_SYS.USERS U INNER JOIN DRUPAL_SYS.LOGIN_HISTORY L ON L."UID" = U."UID"
INNER JOIN ABC_WEBAPP.MEMBER_USER_ROLE MUR ON MUR.USER_ID=U.MDES_ID AND MUR.MEMBER_ID LIKE '%' 
INNER JOIN ABC_WEBAPP.MEMBER M ON M.ID=MUR.MEMBER_ID WHERE U.LOGIN != 0 AND U."UID">1 GROUP BY UNAME ORDER BY L."LOGIN" ASC`

答案 2 :(得分:0)

为了按UIDUNAME获取一个结果行,您可以按这些字段进行分组。

GROUP BY UID, UNAME

然后,对于所有剩余的字段,决定显示哪个值。例如。你有“2689 kaila,piyush”的四条记录,你要显示哪个名字?首先?最后?最大值? ......观点数相同;最大值?总和?同样是访问列的问题。

示例:

select uid, uname, max(name), max(views_count), max(access)
from (<your query here>) query
group by uid, uname;

复制并粘贴(删除了ORDER BY子句):

select uid, uname, max(name), max(views_count), max(access)
from 
(
  SELECT U."UID", 
  CONCAT(CONCAT(U.LNAME, ', '), U.FNAME) AS UNAME,
  M.NAME,
  (SELECT COUNT(LH."UID") FROM DRUPAL_SYS.LOGIN_HISTORY LH WHERE LH."UID"=U."UID" ) AS VIEWS_COUNT,
  L.LOGIN as "ACCESS" FROM DRUPAL_SYS.USERS U INNER JOIN DRUPAL_SYS.LOGIN_HISTORY L ON L."UID" = U."UID"
  INNER JOIN ABC_WEBAPP.MEMBER_USER_ROLE MUR ON MUR.USER_ID=U.MDES_ID AND MUR.MEMBER_ID LIKE '%' 
  INNER JOIN ABC_WEBAPP.MEMBER M ON M.ID=MUR.MEMBER_ID WHERE U.LOGIN != 0 AND U."UID">1
) query
group by uid, uname;