SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to query more select statment from the same table and the same column ?

    hi

    i have one table and it's contain column with name 'status' , i want do sql command to fetch total number of rows , and total number of rows which it's value = 1 and total number of rows which it's value = 0 , i tried to do it using join , but's it's result 2 results ! not one , so plz help me with true command

  2. #2
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    698
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Code:
    select count(*),
             sum(case when status = 1 then 1 else 0 end),
             sum(case when status = 0 then 1 else 0 end)
      from t

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot , but you didn't use count instead of sum ?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT COUNT(*) AS all
         , COUNT(CASE WHEN status = 1 THEN 937 ELSE NULL END) AS ones
         , COUNT(CASE WHEN status = 0 THEN 937 ELSE NULL END) AS zeroes
      FROM daTable
    rudy.ca | @rudydotca
    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
  •