valid SQL statement?

tesla764
tesla764 used Ask the Experts™
on
Is this SQL statement valid?  refering to the "and"

Group BY item.inv_units_Pack and user_comp_permission.id_user = 37
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
use
Group BY item.inv_units_Pack HAVIN user_comp_permission.id_user = 37
Commented:
no
Group BY item.inv_units_Pack,user_comp_permission.id_user = 37
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
<guess you mean this>

SELECT item.inv_units_Pack, -- one or more aggregated column such as SUM(), COUNT()
FROM -- some table(s)
WHERE user_comp_permission.id_user = 37
GROUP BY item.inv_units_Pack
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
or like this
SELECT top 10 TelemetryUID from Telemetry Group by TelemetryTime, TelemetryUID,BitTripNumber  HAVIng BitTripNumber = 2
SQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Couple of things, somewhat redundant with above columns...
*  Columns in GROUP BY are separated by commas
*  You can't have an logic expression in a GROUP BY

btw if it helps I have an article out there on SQL Server GROUP BY Solutions that would be a good read.   If it helped you please hit the 'Yes' button at the end.
Distinguished Expert 2017
Commented:
Having and Where are applied in different stages of the query and so can have different effects.  They can both be used in the same query but if you build the query with the QBE, you need to be careful to specify WHERE when that is what you want since totals queries will always default to using Having.

WHERE is applied BEFORE the data is aggregated so you would use the Where clause if you were not including the field in the selection clause.  So the query selects all data prior to today but doesn't include the date in the aggregation.
Select fld1, fld2, Sum(fld3) as SumOfFld3
From yourtaable
Where fld4 < Date()
Group By fld1, fld2;

HAVING is applied AFTER the data is aggregated so it is most often used on some aggregate value.  Same as the first example except that after the data is aggregated, you apply a Having to select only rows where the sum of fld3 is greater than 333.
Select fld1, fld2, Sum(fld3) as SumOfFld3
From yourtaable
Where fld4 < Date()
Group By fld1, fld2
Having Sum(fld3) > 333;

Author

Commented:
Thank you for your respones everyone. This is not my code and I deal with many languages.
I think you are telling me that the "and" being used is not correct?
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Well, the error message that would be returned when you execute it would tell you that it is not correct.

All of us are guessing what you're trying to pull off, and the correct way of doing it.  So if that's your intent, please provide us some more details as to what you're trying to do.  Preferably with a data mockup of what your data looks like now, and what you're trying to accomplish.
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Thanks for the split.  Good luck with your inherited code.  -Jim

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial