SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Query SQL group by ?

    Hello everyone, I need your help.

    This is my table MYSQL:

    Code:
    id	DR	STATUS
    1	SAR	CLOSE_D
    2	SAR	CLOSE_D
    3	TOU	OPEN
    4	LAM	CLOSE_D
    5	CMP	OPEN
    6	CMP	OPEN
    7	CMP	PENDING
    8	CMP	PENDING
    9	TOU	OPEN
    10	CMP	OPEN
    11	LAM	CLOSE_C
    12	LAM	CLOSE_C
    I need this output, any help would be very much appreciated.
    Many thanks to any who can help me out with this:

    Code:
    dr	OPEN	PENDING	CLOSE_C	CLOSE_D	total
    CMP	3	2	0	0	5
    LAM	0	0	2	1	3
    SAR	0	0	0	2	2
    TOU	2	0	0	0	2
    Thanks in advance.
    Chevy

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    SELECT dr
         , COUNT(CASE WHEN status = 'open'
                      THEN status END) AS open
         , COUNT(CASE WHEN status = 'pending'
                      THEN status END) AS pending
         , COUNT(CASE WHEN status = 'close_c'
                      THEN status END) AS close_c
         , COUNT(CASE WHEN status = 'close_d'
                      THEN status END) AS close_d
         , COUNT(*) AS total
      FROM daTable
    GROUP
        BY dr
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


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
  •