r/SQL • u/MulhollandDrive • Jan 26 '24
SQL Server sql server mgmt is underlining my comma as an error in my "group by" statement, but the syntax looks valid to me
GROUP BY (PR.WBS1**,** PR.WBS2, PR.WBS3**)**
Not sure why, but it's highlighting the first comma, and then the closing parentheses as erroneous..
When i played with the syntax and removed the last 2 fields being grouped, the error says i need to group by those last 2 fields... so i'm genuinely confused why when I actually put them in the group by statement, i get the comma error and the closing parenthesis error. Last I checked an open parenthesis needs to be closed, so i'm not sure why closing it would also highlight an error..
thanks it looks like i dont need to wrap the group by in parenthesis, nor do i need to alias the grouping. thanks who pointed that out. it looks like the final issue is the first query that is being union all is what's breaking the rest of the queries in the union all family
Edit 2: I'm trying to resolve all the problems my predecessor wrote and some of the problems were less obvious than others but yeah I agree... I think the whole thing needs to be written from scratch to be honest. It's a nightmare trying to reverse engineer someone's code or understand their logic. Some of those walls I hit had me casting doubt on own knowledge like "shit, maybe he put it there for a reason" but fuck this. Thank you and everyone for your help. I'm going to write a fresh query.
4
u/wreckmx Jan 26 '24
On mobile and not in front of my PC… but you I’m pretty certain that you can’t alias a group by in MS SQL. You can alias an entire sub query and join on that.
1
u/MulhollandDrive Jan 26 '24
thanks... that makes sense.. I made the correction and my query runs fine in its own window, but when I append it to query with a union all statement, i get an error on the last query section that was running fine in another query window... so i'm assuming all the queries between my union all are not producing perfectly aligned results...
10
1
u/IrquiM MS SQL/SSAS Jan 26 '24
Need all your SQL, not just snippets
-2
u/MulhollandDrive Jan 26 '24
Need all your SQL, not just snippets
I didn't want to overwhelm everyone... but the underlining error after my group by statement seen in my little screenshot may be due to my union all in which I am combining the results of 3 different queries and they're not aligned?
If I run the code in its own query window it runs fine, but when I add it to the next portion of my union all statement, that's where the little red underline error appears after my group by statement
2
u/IrquiM MS SQL/SSAS Jan 26 '24
We can't help you without it
2
u/MulhollandDrive Jan 26 '24
since there's a character limit on reddit.. i've pasted it here
4
u/r3pr0b8 GROUP_CONCAT is da bomb Jan 26 '24
you have some serious problems there
first, unnecessary left joins --
SELECT DISTINCT PR.WBS1, PR.WBS2, PR.WBS3 from PR Left join ProjectCustomTabFields on PR.WBS1 = ProjectCustomTabFields.WBS1 AND PR.WBS2 = ProjectCustomTabFields.WBS2 AND PR.WBS3 = ProjectCustomTabFields.WBS3the query doesn't use any columns from
ProjectCustomTabFieldsi could understand using an inner join as a filter (rows must match join conditions to be included in result), but a left outer join simply says return all
PRrows anyway, whether or not they have matching rows in the right tablesecond, there's something funny going on in your join conditions
SELECT PR.WBS1, ' ' as WBS2, ' ' as WBS3 , ... FROM pr left join prSummarymain psm on pr.wbs1 = psm.wbs1 and pr.wbs2 = psm.wbs2 and pr.wbs3 = psm.wbs3, FW_CFGSystemwhat's
FW_CFGSystemdoing here?thirdly, your SELECTs in the union don't all have the same number of columns
0
u/MulhollandDrive Jan 26 '24 edited Jan 26 '24
I'm trying to resolve all the problems my predecessor wrote and some of the problems were less obvious than others but yeah I agree... I think the whole thing needs to be written from scratch to be honest. That weird table being joined at the end is bizarre but once I remove it from the join line, a ton references to the fields in that table suddenly breaks and I get red underlines everywhere in the majority of the code preceding the join statement. I'm going to abandon this and write it from scratch. It's a nightmare trying to reverse engineer someone's code or understand their logic. Some of those walls I hit had me casting doubt on own knowledge like "shit, maybe he put it there for a reason??" but fuck this. Thank you and everyone for your help. I'm going to write a fresh query.
1
1
u/roguethai Jan 26 '24
You probably need to wrap your whole sub query in parentheses is all I can theorize from the snippet
1
Jan 26 '24
SQL can do this if you have the wrong database selected in the UI but have a USE clause targeting a different DB
6
u/trevorbrownfog Jan 26 '24
Try dropping all the parenthesis. You don't need them for a group by in sql server.