×
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!
  • Students Click Here

*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

Jobs

Joining Several results together in one table

Joining Several results together in one table

Joining Several results together in one table

(OP)
I have the following table.

  File Type    Date Opened
------------- ---------------
     jpg        01-03-2010
     gif        01-02-2010
     png        18-02-2010
     png        06-01-2010
     jpg        02-03-2010
     gif        25-02-2010
     gif        30-01-2010
     png        12-01-2010
     jpg        19-02-2010

and would like to count all the different file types that were opened in January, February and March to produce this result.

File Type   Opened in Jan  Opened in Feb  Opened in Mar
---------   -------------  -------------  -------------
   jpg           0               1              2
   gif           1               2              0
   png           2               1              0

I have the following sql

SELECT [File Type], COUNT([File Type]) As [Opened in January] from Files
Where [Date Opened] between '01-01-2010' and '31-01-2010'
Group BY [File Type]

How do I extend my query to add a further two columns for Feb and March?
Also, How do I bring back the File Type when the Count is 0.
The above query does not bring back the jpg filetype but I still want all file types returned even if the count is 0.

I have trying left joins, sub queries etc but can't get my head around it.

Hope someone can help.

RE: Joining Several results together in one table

this is typically called a pivot query

CODE

SELECT "File Type"
     , COUNT(CASE WHEN EXTRACT(MONTH FROM "Date Opened") = 1
                  THEN 'curly' ELSE NULL END) AS January
     , COUNT(CASE WHEN EXTRACT(MONTH FROM "Date Opened") = 2
                  THEN 'larry' ELSE NULL END) AS February
     , COUNT(CASE WHEN EXTRACT(MONTH FROM "Date Opened") = 3
                  THEN 'moe'   ELSE NULL END) AS March
  FROM Files
GROUP
    BY "File Type"
that's the ANSI SQL solution (because that's the forum you posted in)

if the syntax doesn't work properly in your database system, you might have to tell us what it is

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