1

Need to add Distinct with Row_Number in given query, I have used this query for gridview in asp.net:-

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' 

Added image for reference

  • 6
    Sample data and desired results would help explain what you want to do. In addition, your question suggests that you could ask about a much simpler query. – Gordon Linoff Feb 28 '17 at 11:50

3 Answers3

3

Try it like this:

SELECT ROW_NUMBER() OVER(ORDER BY SomeColumn) AS RowNr
      ,tbl.*
FROM
(SELECT DISTINCT ....) AS tbl

ROW_NUMBER() will generate a number for each row. DISTINCT is looking for identical rows. But - as they have a running number each, they are not identical...

Shnugo
  • 66,100
  • 9
  • 53
  • 114
0

Slap it in a CTE first:

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

Also, For SQL server, use [] for alias, not ''

JohnHC
  • 10,935
  • 1
  • 24
  • 40
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
Singh Kailash
  • 621
  • 5
  • 16