December 29, 2015 at 5:13 pm
Yes, I tried with distinct as I mentioned earlier, but still duplicates shows.
Unless I am inserting distinct at wrong place. See below
Select females_only.*
From dbo.vw_csys_NurserySchool
Cross Apply (
SELECT distinct Parent1_id, Parent1_FullName, Parent1_Email, Parent1_STATUS, Parent1_Gender, YEAR, FULL_ADDRESS
WHERE Parent1_Gender = 'F'
UNION
SELECT distinct Parent2_id, Parent2_FullName, Parent2_Email, Parent2_STATUS, Parent2_Gender,YEAR, FULL_ADDRESS
WHERE Parent2_Gender = 'F'
) AS females_only
Regards,
SQLisAwe5oMe.
December 29, 2015 at 5:17 pm
Read my post a bit more closely 🙂
Jacob Wilkins (12/29/2015)
There would also be duplicates if the same parent occurred on different rows in vw_csys_NurserySchool. You could just do a DISTINCT in the outer SELECT (i.e., SELECT DISTINCT females_only.*) to remove them.Cheers!
December 29, 2015 at 5:23 pm
I am really sorry Jacob. Thank you!....that seems working now.
SELECT DISTINCT females_only.*
From dbo.vw_csys_NurserySchool
Cross Apply (
SELECT Parent1_id, Parent1_FullName, Parent1_Email, Parent1_STATUS, Parent1_Gender, YEAR, FULL_ADDRESS
WHERE Parent1_Gender = 'F'
UNION ALL
SELECT Parent2_id, Parent2_FullName, Parent2_Email, Parent2_STATUS, Parent2_Gender,YEAR, FULL_ADDRESS
WHERE Parent2_Gender = 'F'
) AS females_only
Regards,
SQLisAwe5oMe.
December 29, 2015 at 5:25 pm
No need to be too sorry; we all skip over things when reading sometimes. Goodness knows I have 🙂
I'm glad it's working for you!
Cheers!
December 29, 2015 at 6:41 pm
Jeff Moden (12/29/2015)
I'm thinking that all you need to do is change the AND to an OR in Alan's WHERE clause.
Never mind and apologies. I was in a hurry and didn't notice that this table view is denormalized.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 29, 2015 at 6:49 pm
Thnx again Jeff for trying. I am learning.
Regards,
SQLisAwe5oMe.
December 29, 2015 at 6:54 pm
SQLisAwE5OmE (12/29/2015)
Thnx again Jeff for trying. I am learning.
No problem. Looking back at this, I see that it's not actually a table. It's a view. What is the code for the view? Life could be a whole lot easier if the underlying tables were actually properly normalized. If you get the chance, could you post the CREATE VIEW code?
--Jeff Moden
Change is inevitable... Change for the better is not.
December 29, 2015 at 7:46 pm
SQLisAwE5OmE (12/29/2015)
Thnx again Jeff for trying. I am learning.
You've come to the best place for people that are learning SQL IMHO.
-- Itzik Ben-Gan 2001
December 29, 2015 at 7:55 pm
Jeff, See the attachment for the create view script. Thanks.
Regards,
SQLisAwe5oMe.
December 29, 2015 at 9:46 pm
SQLisAwE5OmE (12/29/2015)
Jeff, See the attachment for the create view script. Thanks.
Thanks for that. That view is a bit of a "Rosetta Stone" for what the table relationships are.
I don't know everything about your data but, from what I saw in the view, the following should do the trick without all that extra overhead from the view. Of course, I have no way of testing this so check carefully.
SELECT ParentID = n.ID
,FullName = n.FULL_NAME
,EMail = n.EMAIL
,[Status] = n.[STATUS]
,Gender = i.GENDER
,[YEAR] = ns.[YEAR]
,FullAddress = na.FULL_ADDRESS
FROM dbo.NAME AS n
JOIN dbo.Individual_JCCOTP AS i ON i.ID = n.ID
JOIN dbo.NURSERY_SCHOOL AS ns ON ns.ID = n.ID
JOIN dbo.Name_Address AS na ON na.ADDRESS_NUM = n.MAIL_ADDRESS_NUM
JOIN dbo.Gen_Tables AS gt ON gt.CODE = ns.NS_PROGRAM
WHERE i.GENDER = 'F'
AND ns.[YEAR] > ''
AND ns.NS_PROGRAM > ''
AND gt.TABLE_NAME = 'NS_PROGRAM'
;
--Jeff Moden
Change is inevitable... Change for the better is not.
December 30, 2015 at 7:58 am
Jeff Moden (12/29/2015)
SQLisAwE5OmE (12/29/2015)
Jeff, See the attachment for the create view script. Thanks.Thanks for that. That view is a bit of a "Rosetta Stone" for what the table relationships are.
I don't know everything about your data but, from what I saw in the view, the following should do the trick without all that extra overhead from the view. Of course, I have no way of testing this so check carefully.
SELECT ParentID = n.ID
,FullName = n.FULL_NAME
,EMail = n.EMAIL
,[Status] = n.[STATUS]
,Gender = i.GENDER
,[YEAR] = ns.[YEAR]
,FullAddress = na.FULL_ADDRESS
FROM dbo.NAME AS n
JOIN dbo.Individual_JCCOTP AS i ON i.ID = n.ID
JOIN dbo.NURSERY_SCHOOL AS ns ON ns.ID = n.ID
JOIN dbo.Name_Address AS na ON na.ADDRESS_NUM = n.MAIL_ADDRESS_NUM
JOIN dbo.Gen_Tables AS gt ON gt.CODE = ns.NS_PROGRAM
WHERE i.GENDER = 'F'
AND ns.[YEAR] > ''
AND ns.NS_PROGRAM > ''
AND gt.TABLE_NAME = 'NS_PROGRAM'
;
Hi Jeff,
This seems working but I am getting less rows(1448) Vs the previous results(2379)...so, I am not sure which is accurate now
Regards,
SQLisAwe5oMe.
December 30, 2015 at 8:02 am
SQLisAwE5OmE (12/30/2015)
Jeff Moden (12/29/2015)
SQLisAwE5OmE (12/29/2015)
Jeff, See the attachment for the create view script. Thanks.Thanks for that. That view is a bit of a "Rosetta Stone" for what the table relationships are.
I don't know everything about your data but, from what I saw in the view, the following should do the trick without all that extra overhead from the view. Of course, I have no way of testing this so check carefully.
SELECT ParentID = n.ID
,FullName = n.FULL_NAME
,EMail = n.EMAIL
,[Status] = n.[STATUS]
,Gender = i.GENDER
,[YEAR] = ns.[YEAR]
,FullAddress = na.FULL_ADDRESS
FROM dbo.NAME AS n
JOIN dbo.Individual_JCCOTP AS i ON i.ID = n.ID
JOIN dbo.NURSERY_SCHOOL AS ns ON ns.ID = n.ID
JOIN dbo.Name_Address AS na ON na.ADDRESS_NUM = n.MAIL_ADDRESS_NUM
JOIN dbo.Gen_Tables AS gt ON gt.CODE = ns.NS_PROGRAM
WHERE i.GENDER = 'F'
AND ns.[YEAR] > ''
AND ns.NS_PROGRAM > ''
AND gt.TABLE_NAME = 'NS_PROGRAM'
;
Hi Jeff,
This seems working but I am getting less rows(1448) Vs the previous results(2379)...so, I am not sure which is accurate now
Weren't the previous results showing duplicates?
December 30, 2015 at 8:05 am
Not after I modified the script as Alan mentioned as below.
SELECT DISTINCT females_only.*
From dbo.vw_csys_NurserySchool
Cross Apply (
SELECT Parent1_id, Parent1_FullName, Parent1_Email, Parent1_STATUS, Parent1_Gender, YEAR, FULL_ADDRESS
WHERE Parent1_Gender = 'F'
UNION ALL
SELECT Parent2_id, Parent2_FullName, Parent2_Email, Parent2_STATUS, Parent2_Gender,YEAR, FULL_ADDRESS
WHERE Parent2_Gender = 'F'
) AS females_only
Regards,
SQLisAwe5oMe.
December 30, 2015 at 9:03 am
SQLisAwE5OmE (12/30/2015)
Not after I modified the script as Alan mentioned as below.SELECT DISTINCT females_only.*
From dbo.vw_csys_NurserySchool
Cross Apply (
SELECT Parent1_id, Parent1_FullName, Parent1_Email, Parent1_STATUS, Parent1_Gender, YEAR, FULL_ADDRESS
WHERE Parent1_Gender = 'F'
UNION ALL
SELECT Parent2_id, Parent2_FullName, Parent2_Email, Parent2_STATUS, Parent2_Gender,YEAR, FULL_ADDRESS
WHERE Parent2_Gender = 'F'
) AS females_only
Another question.
The above query results include staff members as well and I want to exclude staff members.
There is a table called dbo.Name and the column CATEGORY = 'STF' identify the staff members.
How can I modify the script above to exclude n.CATEGORY <>'STF'.....so, my result will be excluding staff members. Thanks.
Regards,
SQLisAwe5oMe.
December 30, 2015 at 10:11 am
SELECT DISTINCT females_only.*
From dbo.vw_csys_NurserySchool
Cross Apply (
SELECT Parent1_id, Parent1_FullName, Parent1_Email, Parent1_STATUS, Parent1_Gender, YEAR, FULL_ADDRESS
WHERE Parent1_Gender = 'F' AND CATEGORY <> 'STF'
UNION
SELECT Parent2_id, Parent2_FullName, Parent2_Email, Parent2_STATUS, Parent2_Gender,YEAR, FULL_ADDRESS
WHERE Parent2_Gender = 'F' AND CATEGORY <> 'STF'
) AS females_only
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 15 posts - 16 through 30 (of 37 total)
You must be logged in to reply to this topic. Login to reply