Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

help count that involves mulitple table joins with foreign keys Expand / Collapse
Author
Message
Posted Thursday, October 10, 2013 9:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 10:12 PM
Points: 8, Visits: 37
I am trying to get a count from two separate tables that are joined via foreign key, it only seems to be counting from the HIS table I need results from both the HIS and GRD


DECLARE @ID INT = 4043300;
SELECT
HISTORY, ELA, MATH, SCIENCE, FL, VA, Prep,
CASE WHEN HISTORY >= 0 AND ELA >= 1 AND MATH >= 1 AND SCIENCE >= 1 AND FL >= 1 AND VA >= 1 AND Prep >= 1
THEN 'Yes'
ELSE 'No' END AS [On Target?]
FROM

(SELECT
COUNT(CASE WHEN CRS.U1 = 'A' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS HISTORY,
COUNT(CASE WHEN CRS.U1 = 'B' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS ELA,
COUNT(CASE WHEN CRS.U1 = 'C' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS MATH,
COUNT(CASE WHEN CRS.U1 = 'D' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS SCIENCE,
COUNT(CASE WHEN CRS.U1 = 'E' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS FL,
COUNT(CASE WHEN CRS.U1 = 'F' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS VA,
COUNT(CASE WHEN CRS.U1 = 'G' AND (HIS.MK NOT LIKE '%FD%') OR (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS Prep
FROM CRS

INNER JOIN
HIS ON CRS.CN = HIS.CN
INNER JOIN
STU ON HIS.PID = STU.ID
LEFT OUTER JOIN
GRD ON CRS.CN = GRD.CN AND dbo.GRD.SN = dbo.STU.SN


WHERE
STU.ID = @ID) AS derived



Thanks in advance

Post #1503854
Posted Thursday, October 10, 2013 10:37 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 10:12 PM
Points: 8, Visits: 37
so if I union it separates the correct values in two rows....so how to sum the rows into one and display only one?


DECLARE @ID INT = 4043300

SELECT
COUNT(CASE WHEN CRS.U1 = 'A' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS HISTORY,
COUNT(CASE WHEN CRS.U1 = 'B' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS ELA,
COUNT(CASE WHEN CRS.U1 = 'C' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS MATH,
COUNT(CASE WHEN CRS.U1 = 'D' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS SCIENCE,
COUNT(CASE WHEN CRS.U1 = 'E' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS FL,
COUNT(CASE WHEN CRS.U1 = 'F' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS VA,
COUNT(CASE WHEN CRS.U1 = 'G' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS Prep
FROM CRS
CRS INNER JOIN
HIS ON CRS.CN = HIS.CN INNER JOIN
STU ON HIS.PID = STU.ID
WHERE
STU.ID = @ID


UNION ALL

SELECT
COUNT(CASE WHEN CRS.U1 = 'A' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS HISTORY,
COUNT(CASE WHEN CRS.U1 = 'B' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS ELA,
COUNT(CASE WHEN CRS.U1 = 'C' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS MATH,
COUNT(CASE WHEN CRS.U1 = 'D' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS SCIENCE,
COUNT(CASE WHEN CRS.U1 = 'E' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS FL,
COUNT(CASE WHEN CRS.U1 = 'F' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS VA,
COUNT(CASE WHEN CRS.U1 = 'G' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS Prep
FROM CRS


INNER JOIN
GRD ON dbo.GRD.CN = dbo.CRS.CN
INNER JOIN
STU ON GRD.SN = STU.SN


WHERE
STU.ID = @ID

Post #1503862
Posted Sunday, October 13, 2013 6:04 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:01 PM
Points: 6,922, Visits: 12,633
What exactly aore you trying to do?

The first query you posted limits the number of rows since you perform an inner join on CRS, HIS, and STU and use this result in an outer join on GRD(SN).
Since the column names are all but descriptive it's hard to figure the business logic behind it.
Just guessing I would "translate"
CN = course name
SN = student name
STU.ID = student id
HIS.PID = person id (aka student id).

If that's the case then the database design itself is inkonsistent: What would you do if there are two students with the name John Doe in one course? One received an 'A' and the other one an 'F'. How would your result set look like?

Therefore I'd like to ask you to provide the table definition together with some sample data and your expected result based on those data. For details onhow to do that please see the firstlink in my signature.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1504255
Posted Sunday, October 13, 2013 2:49 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 13, 2014 10:12 PM
Points: 8, Visits: 37
I didn't design the database, but unfortunately for whatever reason that is causing the headache here (at least for me) is that they use two identifiers! SN (student number) which is not unique and ID (studentID) which is unique but not present on all the tables sometimes only SN is present, and in this case the table I am trying to join only has SN so I have to join it to the STU (student) table which has both ID and SN...so the HIS (history table) has there final grades and uses ID...the GRD (grade) table uses SN which shows present or progress report grades they want them both added up categorically (A-G) which are college prereq courses and displaying only courses with a C- or better. So I am trying to count up all the A-G courses that they've taken and presently taking....I think I have it figured out....I am using this...I don't know if there is a better way or what... but seems ok?


DECLARE @ID INT = 4056102


SELECT
SUM(HISTORY) AS HISTORY,
SUM(ELA) AS ELA,
SUM(MATH) AS MATH,
SUM(SCIENCE) AS SCIENCE,
SUM(FL) AS FL,
SUM(VA) AS VA,
SUM(Prep) AS Prep
FROM
(

SELECT
COUNT(CASE WHEN CRS.U1 = 'A' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS HISTORY,
COUNT(CASE WHEN CRS.U1 = 'B' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS ELA,
COUNT(CASE WHEN CRS.U1 = 'C' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS MATH,
COUNT(CASE WHEN CRS.U1 = 'D' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS SCIENCE,
COUNT(CASE WHEN CRS.U1 = 'E' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS FL,
COUNT(CASE WHEN CRS.U1 = 'F' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS VA,
COUNT(CASE WHEN CRS.U1 = 'G' AND (HIS.MK NOT LIKE '%FD%') THEN STU.ID END) AS Prep
FROM CRS
CRS INNER JOIN
HIS ON CRS.CN = HIS.CN INNER JOIN
STU ON HIS.PID = STU.ID
WHERE
STU.ID = @ID


UNION ALL

SELECT
COUNT(CASE WHEN CRS.U1 = 'A' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS HISTORY,
COUNT(CASE WHEN CRS.U1 = 'B' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS ELA,
COUNT(CASE WHEN CRS.U1 = 'C' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS MATH,
COUNT(CASE WHEN CRS.U1 = 'D' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS SCIENCE,
COUNT(CASE WHEN CRS.U1 = 'E' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS FL,
COUNT(CASE WHEN CRS.U1 = 'F' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS VA,
COUNT(CASE WHEN CRS.U1 = 'G' AND (GRD.M1 NOT LIKE '%FD%') THEN STU.ID END) AS Prep
FROM CRS


INNER JOIN
GRD ON dbo.GRD.CN = dbo.CRS.CN
INNER JOIN
STU ON GRD.SN = STU.SN


WHERE
STU.ID = @ID
)r

Post #1504290
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse