0

I have these tables in which I need to use Row_number() in where clause like this

where spectrumcms.staffchilddiscountpolicy.childno =
(select  ROW_NUMBER() Over (Partition by std.GuardianCNIC  order by std.ID ASC))

I have tables like these

enter image description here

Now i have this query

select
  std.ID as ID,
  std.FirstName,
  std.LastName,
  emp.ID as EmpID,
  emp.FirstName + '' + emp.LastName as EmpName,
  emp.CNIC,
  (
    select count(*) as NoOfChilds
      from StudentManagement.SpectrumCMS.Student stdd
      join HRManagement.SpectrumCMS.Employee empp on stdd.GuardianCNIC = empp.CNIC
      where empp.ID = emp.ID and stdd.CompanyID = std.CompanyID and empp.CompanyID = emp.CompanyID
  ) as NoOfchilds,
  ROW_NUMBER() Over (Partition by std.GuardianCNIC order by std.ID ASC) as ChildNo 

from StudentManagement.SpectrumCMS.Student std
join HRManagement.SpectrumCMS.Employee emp on std.GuardianCNIC = emp.cnic
where std.CompanyID = 20145
  and emp.CompanyID = 20145
  and std.IsActive = 1
  and emp.IsActive = 1
order by emp.CNIC

Now coming to main question why I need where=row_number()over(partition by col order by col):

Because i need to get percentage in last column of above mentioned query from the table of which i have posted the picture of.

This is what currently i am getting

enter image description here

Now in last percentage column, I need percentage from the staffchilddiscountpolicy table of which I have added picture

3
  • Gievn that the row_number is partitioned by GuardianCNIC, how do you propose to join staffchilddiscountpolicy.childno to it, you're going to need another join column. Also you can replace that subquery with count(*) over (partition by... Jul 5, 2021 at 10:14
  • 1
    Hi, and welcome to dba.se! Please avoid using images for the reasons outlined in this post.
    – Vérace
    Jul 5, 2021 at 10:43
  • 1
    Please construct your questions based on the details in minimal reproducible example. Adding table DDL statements, sample DML statements, and providing desired output via a markup table as shown in the example makes it easier for us to provide great answers.
    – Hannah Vernon
    Jul 5, 2021 at 16:36

1 Answer 1

0

As mentioned in the comments, you can't use a window function in that capacity. Rather you should take your first query and put it in a subquery or CTE such that you can then join on ChildNo between it and the SpectrumCMS.StaffChildsDiscountPolicy table like so:

WITH CTE_StudentChildNumbers AS
(
    select
        std.ID as ID,
        std.FirstName,
        std.LastName,
        emp.ID as EmpID,
        emp.FirstName + '' + emp.LastName as EmpName,
        emp.CNIC,
        (
            select count(*) as NoOfChilds
            from StudentManagement.SpectrumCMS.Student stdd
            join HRManagement.SpectrumCMS.Employee empp on stdd.GuardianCNIC = empp.CNIC
            where empp.ID = emp.ID 
                and stdd.CompanyID = std.CompanyID 
                and empp.CompanyID = emp.CompanyID
        ) as NoOfchilds,
       ROW_NUMBER() Over (Partition by std.GuardianCNIC order by std.ID ASC) as ChildNo 
   from StudentManagement.SpectrumCMS.Student std
   join HRManagement.SpectrumCMS.Employee emp on  std.GuardianCNIC = emp.cnic
   where std.CompanyID = 20145
       and emp.CompanyID = 20145
       and std.IsActive = 1
       and emp.IsActive = 1
)

SELECT 
    SCN.ID, 
    SCN.FirstName, 
    SCN.LastName, 
    SCN.EmpID, 
    SCN.EmpName, 
    SCN.CNIC, 
    SCN.NoOfchilds, 
    SCN.ChildNo,
    SCDP.Percentage
FROM CTE_StudentChildNumbers AS SCN
INNER JOIN SpectrumCMS.StaffChildsDiscountPolicy SCDP
    ON SCN.ChildNo = SCDP.ChildNo
WHERE SCDP.CompanyID = 20145 -- Not sure if you wanted the same filters from your screenshot, feel free to remove if not needed
    AND SCDP.IsActive = 1 
ORDER BY SCN.CNIC
4
  • i did this already but it returns 0 rows
    – learning17
    Jul 6, 2021 at 4:27
  • @learning17 Then the way you're generating ChildNo with the ROW_NUMBER() window function is possibly incorrect and there are no relating values to it in your SpectrumCMS.StaffChildsDiscountPolicy table for the filters you're currently applying to each. Please start by verifying you even get any results for the inner query of the CTE in my example, and go from there by adding or removing the remaining joins and filters to see where the fallout is coming from.
    – J.D.
    Jul 6, 2021 at 4:31
  • 1
    you are right actually i wanted a way where i could achieve this without using cte but i think thats not possible your solution works btw so thanks
    – learning17
    Jul 6, 2021 at 4:35
  • @learning17 No problem! I believe you'd either need to use a CTE or a subquery, but I find CTEs more clean / readable, personally.
    – J.D.
    Jul 6, 2021 at 4:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.