Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Jannette
Starting Member
26 Posts |
Posted - 2011-09-07 : 05:13:36
|
When I run this query I get a record list of 20 records.select distinct reference_week from paye_wtr where paye_id = 12033 and reference_year=2011 (There are 2 records for week 1)=====================================================================When I run the following query I get a record list of 19 records.select distinct reference_week from paye_wtr where paye_id = 12033 and reference_year=2011 =====================================================================However when I run the next query, I get a count of 20 recordsselect distinct count(reference_week) from paye_wtr where paye_id = 12033 and reference_year=2011 =====================================================================I was expecting the final query to give me a count of 19, why is this not happening ?Thanks in advance. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-07 : 05:19:38
|
or getting distinct count useselect count(distinct reference_week) from paye_wtr where paye_id = 12033 and reference_year=2011and not the one you used. you need to first take distinct and then count hence it should be inside------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Jannette
Starting Member
26 Posts |
Posted - 2011-09-07 : 05:45:31
|
Thanks for that.However I have the full query here, and the fields PWQ and PWA don't seem to be giving unique counts.=====================================================================Select a.Employer, '"'+a.CANDa+'"', c.PWQ, c.PWA, a.DaysAccd, b.DaysPaid, '"Y"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Case When a.CandType = 'P' Then '"N"' Else '"Y"' End, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0From (Select Distinct Case When (CANDIDATE.Branch_Id=3) Then '"OWWEEK"' When (CANDIDATE.Branch_Id=2 and CANDIDATE.Division_Id=25) Then '"OMMTH"' Else '"OMWEEK"' End as Employer, CANDIDATE.Candidate_Code as Canda, Sum(Holiday_Days) as DaysAccd, Case When CANDIDATE.Candidate_Type = 'C' Then 'U' When CANDIDATE.Candidate_Type = 'S' Then 'C' Else 'P' End as CandType From PAYE_WTR Inner Join PAYE on PAYE_WTR.Paye_Id = PAYE.Paye_Id Inner Join PERSON on PAYE.Person_Id = Person.Person_Id Inner Join CANDIDATE on PERSON.Person_Id = CANDIDATE.Person_Id Where PAYE_WTR.Reference_Year=2011 and CANDIDATE.Active_YN = 'Y' Group By CANDIDATE.Candidate_Code, CANDIDATE.Candidate_Type, CANDIDATE.Branch_Id, CANDIDATE.Division_Id) aLeft Join (Select Distinct CANDIDATE.Candidate_Code as Candb, SUM(Case Code_Id When 3 then Item_Quantity Else 0 End) as DaysPaid From PAYSLIP Inner Join PAYSLIP_ITEM on PAYSLIP.Payslip_Id = Payslip_Item.Payslip_Id Inner Join PAYE ON PAYSLIP.PAYE_Id = PAYE.PAYE_Id Inner Join PERSON ON PAYE.Person_Id = PERSON.Person_Id Inner Join CANDIDATE ON PERSON.Person_Id = CANDIDATE.Person_Id Where PAYSLIP_ITEM.Reference_Year=2011 and CANDIDATE.Active_YN = 'Y' Group By CANDIDATE.Candidate_Code) bOn a.CANDa = b.CANDbLeft Join (Select Distinct CANDIDATE.Candidate_Code as Candc, Count(Distinct WTR_Id)as PWQ, Count(Distinct WTR_Id) as PWA From PAYE_WTR Inner Join PAYE on PAYE_WTR.Paye_Id = PAYE.Paye_Id Inner Join PERSON on PAYE.Person_Id = Person.Person_Id Inner Join CANDIDATE on PERSON.Person_Id = CANDIDATE.Person_Id Where PAYE_WTR.Reference_Year=2011 and CANDIDATE.Active_YN = 'Y' Group By CANDIDATE.Candidate_Code) cOn a.CANDa=c.CANDc |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-07 : 07:07:07
|
we wont be able to suggest you why unless we see how data is present in your table. wold you mind giving some sample data from table and explain why its not giving distinct count?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Jannette
Starting Member
26 Posts |
Posted - 2011-09-07 : 08:15:05
|
Data from file for one client as follows:Holiday_Days WTR_Id1.000000 10.472651 10.472651 20.472651 30.472651 40.354526 50.354526 60.590927 70.472651 80.472651 90.354526 100.472651 110.472651 140.472651 150.472651 160.472651 17There are 16 records, however I would like the count to be 15 as there are 2 week 1 records. |
 |
|
|
|
|
|
|