SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I use group by on one column which is having many entries?


How do I use group by on one column which is having many entries?

Author
Message
ashuthinks
ashuthinks
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 199
Hi,

I want to use group by on one column which is having many entries

table_a

Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0

Now I want to find out `Name` which is having `Price` as `0`

but as I'm having entries `AAA` 3 times I can't directly write simple sql with condition
`NOT Equal to 0`

Please help me I want to print result for above table_a should be

only `D` as it is having `0` as `price`.
ChrisM@home
ChrisM@home
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2040 Visits: 10366
ashuthinks (4/17/2013)
Hi,

I want to use group by on one column which is having many entries

table_a

Name price
AAA 12
BBB 13
AAA 0
CCC 24
AAA 0
DDD 0

Now I want to find out `Name` which is having `Price` as `0`

but as I'm having entries `AAA` 3 times I can't directly write simple sql with condition
`NOT Equal to 0`

Please help me I want to print result for above table_a should be

only `D` as it is having `0` as `price`.


SELECT 1 FROM table_a a WHERE NOT EXISTS (SELECT 1 FROM table_a b WHERE b.name = a.name AND b.Price > 0)


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
ashuthinks
ashuthinks
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 199
I have done by using having clause

SELECT name,SUM(price) as price
FROM table
GROUP BY name
HAVING SUM(price) = 0

Thanks Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search