Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 11th, 2011, 10:44 PM
wakerunner wakerunner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 68 wakerunner User rank is Private First Class (20 - 50 Reputation Level)wakerunner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 49 m
Reputation Power: 5
SQL query help

Is it possible to get the following from a SQL string?

Code:
sample table
model        make            location
ford           f150               1
chevy        camaro              1
ford           f150               2
ford           f150               3
chevy        camaro               1
ford           ranger             3

Can you do a SQL string that would returns records like below?
model        make       Count1               Count2     Count3
ford           f150          1                   1           1
chevy        camaro          2                  0           0
ford           ranger        0                  0           1
Where the count1, 2, 3 are the count of the make at each location.

Didn't think this was possible with a SQL string but thought I would check. Thanks for any help.

Last edited by wakerunner : July 11th, 2011 at 10:46 PM.

Reply With Quote
  #2  
Old July 12th, 2011, 01:22 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Code:
SELECT model
     , make
     , COUNT(CASE WHEN location = 1
                  THEN location END ) AS Count1
     , COUNT(CASE WHEN location = 2
                  THEN location END ) AS Count2
     , COUNT(CASE WHEN location = 3
                  THEN location END ) AS Count3
  FROM daTable
GROUP
    BY model
     , make
Comments on this post
wakerunner agrees: Terrific and amazing stuff
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 12th, 2011, 08:30 AM
wakerunner wakerunner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 68 wakerunner User rank is Private First Class (20 - 50 Reputation Level)wakerunner User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 49 m
Reputation Power: 5
Incredible! The more I learn about SQL the more amazed I am. BTW your simply-sql.com site does not have any links on it to purchase your book (unless I missed something... I don't see any links at all just a static page), I do see it on Amazon though, I will definitely buy it on Amazon unless you have a direct way to buy. Thanks for your help!

Quote:
Originally Posted by r937
Code:
SELECT model
     , make
     , COUNT(CASE WHEN location = 1
                  THEN location END ) AS Count1
     , COUNT(CASE WHEN location = 2
                  THEN location END ) AS Count2
     , COUNT(CASE WHEN location = 3
                  THEN location END ) AS Count3
  FROM daTable
GROUP
    BY model
     , make

Reply With Quote
  #4  
Old July 12th, 2011, 08:57 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Quote:
Originally Posted by wakerunner
BTW your simply-sql.com site does not have any links on it to purchase your book (unless I missed something... I don't see any links at all just a static page),
yes, it's just a static page for the time being (long story)

amazon or your local bookstore is fine

feel free to contact me if there's anything in the book that isn't clear


Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > SQL query help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap