-3

我正在处理sql查询,请检查并给我解决方案

请在下面找到表格的任务简要详细信息

表 1 'tbl_SuperAdmin'已归档

数据样例

s_id name age gtc.......
1     abc  23 .........
2     cda  42 ..........

另一个表2有'tbl_Renewal'

renewalid  renname date supid   Payed etc......
1          first   -     1        1    ........
2          first   -     2        1    ........
3          second -      1        0     ........
4          second -      1        1     ........
5          third-        1        1     ........

查询是 select *from tbl_superadmin as a inner join tbl_Renewal as b on a.s_id=b.supid

结果是

 s_id name age gtc....... renewalid  renname date supid   Payed etc.......
1     abc  23 .............1        first     -      1         1    ........
2     cda  42 .............2       first      -      2         1    ........
1     abc  23 .............3       second     -      1        0     ........
1     abc  23 .............4       second     -      1        1     ........
1     abc  23 .............5       third      -      1        1     ........ 

在这里我想要如何支付=1 和更新id desc 并且不需要 s_id 不再重复 1 次

我想展示我的结果

  s_id name age gtc....... renewalid  renname date supid   Payed etc.......
    1     abc  23 .............5       third      -      1        1     ........ 
    2     cda  42 .............2       first      -      2         1    ........

像这个结果我想要请任何人帮助它

谢谢 pradeep

4

2 回答 2

1

要删除重复项,您需要使用Windows Function

With cte as 
(
 select *,row_number() over(partition by s_id order by renewalid desc )rn
 from tbl_superadmin as a 
 inner join tbl_Renewal as b 
 on a.s_id=b.supid
)
Select * from cte where rn=1

SQL FIDDLE中的演示

更新

;With cte as 
( 
 select *,row_number() over(partition by s_id order by renewalid desc )rn 
 from alz_SuperAdmin as a inner join alz_Renewal as b on a.s_id=b.supid
)
select top 2 CONVERT(varchar(6),a.lastdate,6) as lastdate,
(select name from alz_states where ID=a.joblocation ) as locat,
from alz_jobpost as a
inner join 
(Select col1,col2,col3.... from cte where rn=1 )ct
on ct.commonColumn=a.CommonColumn  and 
ct.Product in 
(Select query)
于 2013-03-22T11:10:49.750 回答
0

您应该在查询中使用 GROUP BY 和 ORDER。像这样的东西:

SELECT 
    * 
FROM 
    tbl_SuperAdmin AS a
INNER JOIN 
    tbl_Renewal AS b 
ON 
    a.s_id = b.supid
GROUP BY
    a.s_id
ORDER BY
    b.renewalid
DESC
于 2013-03-22T10:54:25.103 回答