Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thank you. It's already helped me greatly, and I enjoy just reading the inputs from the other members..."

Geography

Where in the world do Tek-Tips members come from?
Laeg (Programmer)
29 Aug 09 10:16
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
 
Helpful Member!  r937 (TechnicalUser)
29 Aug 09 10:39

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

Laeg (Programmer)
29 Aug 09 11:21
Cheers! Now if only you could translate that into .NET Linq it'd make my day week!
r937 (TechnicalUser)
29 Aug 09 11:32
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

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!

Back To Forum

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