×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Can this be done?

Can this be done?

Can this be done?

(OP)
I have a table of CartItems, with the ProductID bought and then an Extra field which is true or false. I want to see a grouping of a count of each ProductID but within that count I want to know the count of ProductIDs with extras.

So given

CODE

create table test(CartItemID int, ProductID int, Extra int);

insert into test (CartItemID, ProductID, Extra) values (100, 1, 1);
insert into test (CartItemID, ProductID, Extra) values (200, 2, 0);
insert into test (CartItemID, ProductID, Extra) values (300, 2, 0);
insert into test (CartItemID, ProductID, Extra) values (400, 1, 1);

select ProductID, count(ProductID) as Total
from test
group by ProductID;

produces...

ProductID | Total
 1                  2
 2                  2

I'd like it to say ...

ProductID | Total | ExtraCount
 1                    2       2             <-- True As 2 instances of a ProductID 1 purchase also had Extra (CartItemID 100, 400)
 2                    2       0             <-- False As 0 instances of a ProductID 2 purchase had Extra

Can this be done?

Thanks for replies
 

RE: Can this be done?

CODE

SELECT ProductID
     , COUNT(*) AS Total
     , COUNT(CASE WHEN Extra = 1
                  THEN 'Science!!'
                  ELSE NULL END) AS ExtraCount
  FROM CartItems
GROUP
    BY ProductID
smile

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: Can this be done?

(OP)
Cheers! Now if only you could translate that into .NET Linq it'd make my day week!

RE: Can this be done?

perhaps you should ask that question in the .NET forum, eh

winky smile

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close