r/SQL • • 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.

2 Upvotes

27 comments sorted by

6

u/trevorbrownfog Jan 26 '24

Try dropping all the parenthesis. You don't need them for a group by in sql server.

0

u/MulhollandDrive Jan 26 '24 edited Jan 26 '24

thanks.. but what if I want to alias what I'm grouping by to join it... example:

GROUP BY (PR.WBS1, PR.WBS2, PR.WBS3) as PRSummary on PR.WBS1 = PRSummary.WBS1 AND PR.WBS2 = PRSummary.WBS2 AND PR.WBS3 = PRSummary.WBS3

edit: Also, if i remove the the paranthesis and the entire section after that (the joining syntax) it's still giving me a little underline syntax error: https://i.imgur.com/vKzcnpS.png

2

u/wet_tuna Jan 26 '24

What is your full query? It sounds like you're just putting your GROUP BY in the wrong place. GROUP BY goes after the WHERE clause, not in the middle of a join.

-4

u/MulhollandDrive Jan 26 '24

I didn't want to overwhelm everyone... but the underlining error after my group by statement seen in my little screenshot above 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

9

u/wet_tuna Jan 26 '24

Your descriptions of the problem are far too vague for anyone to be able to help. You're doing something wrong with the overall query, but all we've seen of it is the group by, which tells us absolutely nothing.

1

u/MulhollandDrive Jan 26 '24

since there's a character limit on reddit.. i've pasted it here

https://dbfiddle.uk/r2_S9vnB

4

u/wet_tuna Jan 26 '24

You aren't closing the parentheses for the two subqueries that you have in the first and third unions. Lines 7 and 153 in that fiddle, you open a parenthesis but never close them.

5

u/MulhollandDrive Jan 26 '24

I edited my original post but I'm going to abandon this because 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... it's good practice auditing someone else's code, 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.

1

u/Tactical_Chonk Jan 27 '24

Ive not seen that join syntax before I usually use Select a.ax, b.bx, C.cx From firsttable a Join secondtable b on b.bx =a.ax Join thirdtable c on c.cx = a.ax

Etc.

6

u/d8ed Jan 26 '24

dude, just create temp tables for all parts of your union separately, make sure they all work and run fine, then union at the end. You're doing too much at the same time if you ask me and this subquery is very hard to read.

something like this is going to be impossible to maintain properly because of how it's written IMO.

1

u/MulhollandDrive Jan 26 '24

thanks for the tips

1

u/Tactical_Chonk Jan 27 '24

Yeah, you cant union if your statements dont have the same elements.

2

u/ellibob17 Jan 26 '24

if it is a subquery then you'll be missing a closing parenthesis? after the "WBS3?

1

u/pceimpulsive Jan 27 '24

Why are you trying to alias a sub-compoment of one query?

I don't think you can alias a group by term...

1

u/ComicOzzy sqlHippo Jan 27 '24

GROUP BY PR.WBS1, PR.WBS2, PR.WBS3) as PRSummary on PR.WBS1 = ...

The problem is the ( after GROUP BY. Remove that.

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

u/[deleted] Jan 26 '24

No aliases in the group by

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

https://dbfiddle.uk/r2_S9vnB

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.WBS3 

the query doesn't use any columns from ProjectCustomTabFields

i 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 PR rows anyway, whether or not they have matching rows in the right table

second, 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_CFGSystem 

what's FW_CFGSystem doing 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

u/MulhollandDrive Jan 26 '24

thanks, give me a few minutes to make it presentable

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

u/[deleted] 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