Solved

valid SQL statement?

Posted on 2014-03-05
9
436 Views
Last Modified: 2014-03-05
Is this SQL statement valid?  refering to the "and"

Group BY item.inv_units_Pack and user_comp_permission.id_user = 37
0
Comment
Question by:tesla764
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Assisted Solution

by:ravikantninave
ravikantninave earned 66 total points
ID: 39907409
use
Group BY item.inv_units_Pack HAVIN user_comp_permission.id_user = 37
0
 
LVL 6

Assisted Solution

by:r3nder
r3nder earned 166 total points
ID: 39907433
no
Group BY item.inv_units_Pack,user_comp_permission.id_user = 37
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 184 total points
ID: 39907437
<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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Assisted Solution

by:r3nder
r3nder earned 166 total points
ID: 39907440
or like this
SELECT top 10 TelemetryUID from Telemetry Group by TelemetryTime, TelemetryUID,BitTripNumber  HAVIng BitTripNumber = 2
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 184 total points
ID: 39907454
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.
0
 
LVL 37

Assisted Solution

by:PatHartman
PatHartman earned 84 total points
ID: 39907461
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;
0
 

Author Comment

by:tesla764
ID: 39907462
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?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39907471
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.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39907533
Thanks for the split.  Good luck with your inherited code.  -Jim
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question