需要在给定查询中添加与Row_Number的Distinct,我在asp.net中使用了此查询for gridview: -
SELECT DISTINCT ROW_NUMBER() OVER (order by tbpan) AS 'Sr. No',
case when tbprofile = '3' then 'Applicant TBI'
when tbprofile = '4' then 'Prayas Center'
end 'Applicant Type',
REPLACE(ISNULL(DATEPART(yyyy,b.govtimevalid), '-'),0,'-') as 'Year',
[tbpan] AS 'Applicant Id',
ISNULL(a.PCId, '-') as 'PCId',
UPPER(tbname) AS 'Name',
UPPER(isnull(formstatus,'IN PROGRESS')) AS 'Form Status',UPPER(isnull(pmuapproval,'-')) AS 'PMU Status',
case when pmuapproval = 'valid'
then isnull (convert(Varchar, pmutimevalid, 107),'-')
else isnull (convert(Varchar, pmutimeinvalid, 107),'-')
end 'PMUDateTime',
UPPER(ISNULL(govapproval,'-')) AS 'PMC Status',
case when govapproval = 'valid'
then isnull (convert(Varchar, govtimevalid, 107),'-')
else isnull( convert(Varchar, govtimeinvalid, 107),'-')
end 'PMCDateTime',
ISNULL(SanctionedAmount,'0') AS 'Sanctioned Amount',
ISNULL((SanctionedAmount-BalDisbursed),'0') AS 'Total Disbursed',ISNULL(BalDisbursed,'0') AS 'Total Balance'
FROM tb_User a
LEFT OUTER JOIN applied b ON a.tbpan=b.tbid
LEFT OUTER JOIN tb_SanctionInfo c ON a.PCId = c.PCId
LEFT OUTER JOIN tb_DisbursedInfo d ON c.PCId = d.PCId WHERE tbprofile !='1' AND tbprofile !='2'
答案 0 :(得分:2)
试试这样:
SELECT ROW_NUMBER() OVER(ORDER BY SomeColumn) AS RowNr
,tbl.*
FROM
(SELECT DISTINCT ....) AS tbl
ROW_NUMBER()
将为每一行生成一个数字。 DISTINCT
正在寻找相同的行。但是 - 因为它们各有一个运行数字,它们并不相同......
答案 1 :(得分:0)
首先在CTE中拍打它:
with CTE as
(
SELECT DISTINCT
case when tbprofile = '3' then 'Applicant TBI'
when tbprofile = '4' then 'Prayas Center'
end 'Applicant Type',
REPLACE(ISNULL(DATEPART(yyyy,b.govtimevalid), '-'),0,'-') as 'Year',
[tbpan] AS 'Applicant Id',
ISNULL(a.PCId, '-') as 'PCId',
UPPER(tbname) AS 'Name',
UPPER(isnull(formstatus,'IN PROGRESS')) AS 'Form Status',UPPER(isnull(pmuapproval,'-')) AS 'PMU Status',
case when pmuapproval = 'valid'
then isnull (convert(Varchar, pmutimevalid, 107),'-')
else isnull (convert(Varchar, pmutimeinvalid, 107),'-')
end 'PMUDateTime',
UPPER(ISNULL(govapproval,'-')) AS 'PMC Status',
case when govapproval = 'valid'
then isnull (convert(Varchar, govtimevalid, 107),'-')
else isnull( convert(Varchar, govtimeinvalid, 107),'-')
end 'PMCDateTime',
ISNULL(SanctionedAmount,'0') AS 'Sanctioned Amount',
ISNULL((SanctionedAmount-BalDisbursed),'0') AS 'Total Disbursed',ISNULL(BalDisbursed,'0') AS 'Total Balance'
FROM tb_User a
LEFT OUTER JOIN applied b ON a.tbpan=b.tbid
LEFT OUTER JOIN tb_SanctionInfo c ON a.PCId = c.PCId
LEFT OUTER JOIN tb_DisbursedInfo d ON c.PCId = d.PCId WHERE tbprofile !='1' AND tbprofile !='2'
)
select CTE.*, ROW_NUMBER() OVER (order by [Applicant ID]) AS 'Sr. No'
from CTE
此外,对于SQL Server,使用[]作为别名,而不是''
答案 2 :(得分:0)
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS 'Sr. No', 'Applicant Type','Applicant Id','PCId','Name', 'PMU Status','PMUDateTime','PMC Status',
'PMCDateTime','Sanctioned Amount','Total Balance'
FROM (
DISTINCT
case when tbprofile = '3' then 'Applicant TBI'
when tbprofile = '4' then 'Prayas Center'
end 'Applicant Type',
REPLACE(ISNULL(DATEPART(yyyy,b.govtimevalid), '-'),0,'-') as 'Year',
[tbpan] AS 'Applicant Id',
ISNULL(a.PCId, '-') as 'PCId',
UPPER(tbname) AS 'Name',
UPPER(isnull(formstatus,'IN PROGRESS')) AS 'Form Status',UPPER(isnull(pmuapproval,'-')) AS 'PMU Status',
case when pmuapproval = 'valid'
then isnull (convert(Varchar, pmutimevalid, 107),'-')
else isnull (convert(Varchar, pmutimeinvalid, 107),'-')
end 'PMUDateTime',
UPPER(ISNULL(govapproval,'-')) AS 'PMC Status',
case when govapproval = 'valid'
then isnull (convert(Varchar, govtimevalid, 107),'-')
else isnull( convert(Varchar, govtimeinvalid, 107),'-')
end 'PMCDateTime',
ISNULL(SanctionedAmount,'0') AS 'Sanctioned Amount',
ISNULL((SanctionedAmount-BalDisbursed),'0') AS 'Total Disbursed',ISNULL(BalDisbursed,'0') AS 'Total Balance'
FROM tb_User a
LEFT OUTER JOIN applied b ON a.tbpan=b.tbid
LEFT OUTER JOIN tb_SanctionInfo c ON a.PCId = c.PCId
LEFT OUTER JOIN tb_DisbursedInfo d ON c.PCId = d.PCId WHERE tbprofile !='1' AND tbprofile !='2'
) T