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 |
mikebird
Aged Yak Warrior
529 Posts |
Posted - 2009-09-16 : 06:09:03
|
Once I have a functional statement grouping nicely, how do I just add another column(s) in there afterwards, using the objects and joins, not included in the grouping? Do I seperate the group work in parenthesis, or what? I really hate this point in SQL, and it's where I quit and use Crystal Reports but I can't today. How do I step back and add an ungrouped column?In fact, what I need to do is add the de.PatientNumber as a column to the select list - part of the joins... but don't want to group on that!SELECTvwKC60.Code AS BaseCode,vwKC60.Description,SUM(patindex('M',de.Sex)) AS TotalMale,SUM(PATINDEX(@sSexOrnBiSex + 'F',di.Acquired + de.Sex)) + SUM(PATINDEX(@sSexOrnHoSex + 'M', di.Acquired + de.Sex)) AS HoBiMaleOnly,SUM(patindex('F',de.Sex)) AS TotalFemaleFROM vwSHKC60BaseCode vwKC60LEFT JOIN Diagnosis di ON vwKC60.Code = di.DiagnosisLEFT JOIN Demographic de ON di.PatientNumber = de.PatientNumberLEFT JOIN Episode ep ON ep.ID = di.ParentIDLEFT JOIN Locations AS lo ON ep.Location = lo.CodedWHERE (di.DiagnosisDate BETWEEN '01-06-2009' AND '31-08-2009')AND (di.Provisional = 0)AND (di.RecordDeleted = 0)AND ep.Location = 'GU'AND ep.Location = 'GU'OR (lo.BelongsTo LIKE '%GU%')GROUP BYALL vwKC60.Code, vwKC60.DescriptionORDER BYvwKC60.Code |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-16 : 06:43:03
|
If there are many different PatientNumbers in the grouped data you cannot display them all because in a group all values have to be equal if they are not in aggregate functions.See this example:AcctNo Name payed1 Miller 52 Miller 13 Smith 44 Smith 3 Now you can group by Name and sum(payed)result would beMiller 6Smith 7So there is ONE line per grouping result!In this ONE line you cannot display AcctNo 1 and 2 or 3 and 4The only thing you can do is min(AcctNo) or max(AcctNo) to get only ONE value per group. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
mikebird
Aged Yak Warrior
529 Posts |
Posted - 2009-09-16 : 07:20:27
|
The grouping is not a problem. It's doing exactly what I want.I want to add a column to the select list, without grouping on it, which I think requires a setup something like this, but I cannot get the syntax right...select PatientNumberfrom Demographicinner join(SELECTvwKC60.Code AS BaseCode,vwKC60.Description,SUM(patindex('M',de.Sex)) AS TotalMale,SUM(PATINDEX(@sSexOrnBiSex + 'F',di.Acquired + de.Sex)) + SUM(PATINDEX(@sSexOrnHoSex + 'M', di.Acquired + de.Sex)) AS HoBiMaleOnly,SUM(patindex('F',de.Sex)) AS TotalFemaleFROM vwSHKC60BaseCode vwKC60LEFT JOIN Diagnosis di ON vwKC60.Code = di.DiagnosisLEFT JOIN Demographic de ON di.PatientNumber = de.PatientNumberLEFT JOIN Episode ep ON ep.ID = di.ParentIDLEFT JOIN Locations AS lo ON ep.Location = lo.CodedWHERE (di.DiagnosisDate BETWEEN '01-06-2009' AND '31-08-2009')AND (di.Provisional = 0)AND (di.RecordDeleted = 0)AND ep.Location = 'GU'AND ep.Location = 'GU'OR (lo.BelongsTo LIKE '%GU%')GROUP BYALL vwKC60.Code, vwKC60.Description) as X on Demographic.PatientNumber = X.PatientNumberORDER BYvwKC60.Codebut I think the problem is that PatientNumber was not selected in the first case anyway, to make the link to...Is maybe creating a derived table of the original grouping statement, and then attach extra columns to that? Sounds simplePlease? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-16 : 07:47:09
|
Your derived table is grouping many records into one record.I think there are many different PatientNumbers within the underlaying records.Hence it is not possible to have one PatientNumber per grouped record.Maybe you want to retrieve all PatientNumbers which are matching the WHERE clause of your derived table.So you have to do the select without grouping and aggregating. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
mikebird
Aged Yak Warrior
529 Posts |
Posted - 2009-09-16 : 08:16:47
|
Very apt advice. Need to check with boss what's wanted. The statement is totalling summaries. Cannot show PatientNumber for even two cases. Review |
|
|
|
|
|
|
|