SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to combine these three queries?

    I'm currently using three separate queries to get the data I need out of this table. I know there is a way to do a single query... I just can't seem to figure out what that way is... Anyone?

    Code:
    SELECT COUNT(tracking_number) as dase 
    FROM billing 
    WHERE user_id = $user_id
      AND charge_description_code = 'LDR'
      AND invoice_date BETWEEN '$start_date' AND '$end_date'
    
    SELECT COUNT(tracking_number) as total
    FROM billing 
    WHERE user_id = $user_id
      AND package_quantity>0
      AND invoice_date BETWEEN '$start_date' AND '$end_date'
    
    SELECT COUNT(tracking_number) as das
    FROM billing 
    WHERE user_id = $user_id
      AND charge_description_code = 'RDR'
      AND invoice_date BETWEEN '$start_date' AND '$end_date'

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT COUNT(CASE WHEN charge_description_code = 'LDR'
                      THEN tracking_number END) AS dase
         , COUNT(CASE WHEN package_quantity>0
                      THEN tracking_number END) AS total
         , COUNT(CASE WHEN charge_description_code = 'RDR'
                      THEN tracking_number END) AS das
      FROM billing 
     WHERE user_id = $user_id
       AND invoice_date BETWEEN '$start_date' AND '$end_date'
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •