Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query enhancement

SQL Query enhancement

Scheduled Pinned Locked Moved Database
databasequestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wonder FOOL
    wrote on last edited by
    #1

    I wrote a query that gets the counts of which has different statuses. The query goes like

    SELECT COUNT(rec.rmaNum) as Created
    FROM RMARecords rec
    LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
    WHERE rec.rmaStatus = 0
    UNION
    SELECT COUNT(rec.rmaNum) as Received
    FROM RMARecords rec
    LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
    WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
    UNION
    SELECT COUNT(rec.rmaNum) as Closed
    FROM RMARecords rec
    LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
    WHERE rec.rmaStatus = 3
    UNION
    SELECT COUNT(rec.rmaNum) as Voided
    FROM RMARecords rec
    LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
    WHERE rec.rmaStatus = 4

    The output of this query is; Created ------- 0 1 3 6 I think because of the union the result set comes like this. What I want as output is different Created Received Closed Voided --------/----------/-------/-------- --0-----/-----1----/--3--/----6--- How can I get as a result set like the upper result? Thanks in advance.

    C C L 3 Replies Last reply
    0
    • W wonder FOOL

      I wrote a query that gets the counts of which has different statuses. The query goes like

      SELECT COUNT(rec.rmaNum) as Created
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
      WHERE rec.rmaStatus = 0
      UNION
      SELECT COUNT(rec.rmaNum) as Received
      FROM RMARecords rec
      LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
      WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
      UNION
      SELECT COUNT(rec.rmaNum) as Closed
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
      WHERE rec.rmaStatus = 3
      UNION
      SELECT COUNT(rec.rmaNum) as Voided
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
      WHERE rec.rmaStatus = 4

      The output of this query is; Created ------- 0 1 3 6 I think because of the union the result set comes like this. What I want as output is different Created Received Closed Voided --------/----------/-------/-------- --0-----/-----1----/--3--/----6--- How can I get as a result set like the upper result? Thanks in advance.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      I'd do it this way.

      select sum(created_count), sum(received_count), sum(closed_count), sum(voided_count) from (
      SELECT COUNT(rec.rmaNum) created_count
      ,0 received_count
      ,0 closed_count
      ,0 voided_count
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
      WHERE rec.rmaStatus = 0
      UNION
      SELECT 0 created_count
      ,COUNT(rec.rmaNum) received_count
      ,0 closed_count
      ,0 voided_count
      FROM RMARecords rec
      LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
      WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
      UNION
      SELECT 0 created_count
      ,0 received_count
      ,COUNT(rec.rmaNum) closed_count
      ,0 voided_count
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
      WHERE rec.rmaStatus = 3
      UNION
      SELECT 0 created_count
      ,0 received_count
      ,0 closed_count
      ,COUNT(rec.rmaNum) voided_count
      FROM RMARecords rec
      LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
      WHERE rec.rmaStatus = 4
      );

      You could also look into creating a view for those joins. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      W 1 Reply Last reply
      0
      • C Chris Meech

        I'd do it this way.

        select sum(created_count), sum(received_count), sum(closed_count), sum(voided_count) from (
        SELECT COUNT(rec.rmaNum) created_count
        ,0 received_count
        ,0 closed_count
        ,0 voided_count
        FROM RMARecords rec
        LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
        WHERE rec.rmaStatus = 0
        UNION
        SELECT 0 created_count
        ,COUNT(rec.rmaNum) received_count
        ,0 closed_count
        ,0 voided_count
        FROM RMARecords rec
        LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
        WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
        UNION
        SELECT 0 created_count
        ,0 received_count
        ,COUNT(rec.rmaNum) closed_count
        ,0 voided_count
        FROM RMARecords rec
        LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
        WHERE rec.rmaStatus = 3
        UNION
        SELECT 0 created_count
        ,0 received_count
        ,0 closed_count
        ,COUNT(rec.rmaNum) voided_count
        FROM RMARecords rec
        LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
        WHERE rec.rmaStatus = 4
        );

        You could also look into creating a view for those joins. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        W Offline
        W Offline
        wonder FOOL
        wrote on last edited by
        #3

        Hello Chris, In your first line

        select sum(created_count), sum(received_count), sum(closed_count), sum(voided_count) from (

        I am having errors in this line for the column names which you placed inside the brackets. For; created_count, received_count, closed_count, voided_count. The error is; invalid column name. In my sql statement I am only looking to the column name rmaNum.

        C 1 Reply Last reply
        0
        • W wonder FOOL

          Hello Chris, In your first line

          select sum(created_count), sum(received_count), sum(closed_count), sum(voided_count) from (

          I am having errors in this line for the column names which you placed inside the brackets. For; created_count, received_count, closed_count, voided_count. The error is; invalid column name. In my sql statement I am only looking to the column name rmaNum.

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          Your original post included the 'as' keyword for aliasing. Perhaps your DB requires that. Try the following

          ...
          SELECT COUNT(rec.rmaNum) as created_count
          ,0 as received_count
          ,0 as closed_count
          ,0 as voided_count
          FROM RMARecords rec
          ...

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          1 Reply Last reply
          0
          • W wonder FOOL

            I wrote a query that gets the counts of which has different statuses. The query goes like

            SELECT COUNT(rec.rmaNum) as Created
            FROM RMARecords rec
            LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
            WHERE rec.rmaStatus = 0
            UNION
            SELECT COUNT(rec.rmaNum) as Received
            FROM RMARecords rec
            LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
            WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
            UNION
            SELECT COUNT(rec.rmaNum) as Closed
            FROM RMARecords rec
            LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
            WHERE rec.rmaStatus = 3
            UNION
            SELECT COUNT(rec.rmaNum) as Voided
            FROM RMARecords rec
            LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
            WHERE rec.rmaStatus = 4

            The output of this query is; Created ------- 0 1 3 6 I think because of the union the result set comes like this. What I want as output is different Created Received Closed Voided --------/----------/-------/-------- --0-----/-----1----/--3--/----6--- How can I get as a result set like the upper result? Thanks in advance.

            C Offline
            C Offline
            Corporal Agarn
            wrote on last edited by
            #5

            You might try:

            SELECT
            SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created
            SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received
            SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed
            SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided
            FROM RMARecords rec
            LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

            L 1 Reply Last reply
            0
            • C Corporal Agarn

              You might try:

              SELECT
              SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created
              SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received
              SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed
              SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided
              FROM RMARecords rec
              LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              TRY THIS

              djj55 wrote:

              SELECT SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided FROM RMARecords rec LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

              comma missing..

              SELECT
              SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created,
              SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received ,
              SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed,
              SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided
              FROM RMARecords rec
              LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

              Maulik Dusara Sr. Sofware Engineer I love it when a plan comes together

              C 1 Reply Last reply
              0
              • L Lost User

                TRY THIS

                djj55 wrote:

                SELECT SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided FROM RMARecords rec LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

                comma missing..

                SELECT
                SUM(CASE WHEN rec.rmaStatus = 0 THEN 1 ELSE 0 END) as Created,
                SUM(CASE WHEN (rec.rmaStatus = 1 OR rec.rmaStatus = 2) THEN 1 ELSE 0 END) AS Received ,
                SUM(CASE WHEN rec.rmaStatus = 3 THEN 1 ELSE 0 END) AS Closed,
                SUM(CASE WHEN rec.rmaStatus = 4 THEN 1 ELSE 0 END) AS Voided
                FROM RMARecords rec
                LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated

                Maulik Dusara Sr. Sofware Engineer I love it when a plan comes together

                C Offline
                C Offline
                Corporal Agarn
                wrote on last edited by
                #7

                Thanks! I just did a quick non-tested coding. :-O

                1 Reply Last reply
                0
                • W wonder FOOL

                  I wrote a query that gets the counts of which has different statuses. The query goes like

                  SELECT COUNT(rec.rmaNum) as Created
                  FROM RMARecords rec
                  LEFT JOIN RMAUsers ucr on ucr.id = rec.userCreated
                  WHERE rec.rmaStatus = 0
                  UNION
                  SELECT COUNT(rec.rmaNum) as Received
                  FROM RMARecords rec
                  LEFT JOIN RMAUsers ure on ure.id = rec.userCreated
                  WHERE (rec.rmaStatus = 1 OR rec.rmaStatus = 2)
                  UNION
                  SELECT COUNT(rec.rmaNum) as Closed
                  FROM RMARecords rec
                  LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
                  WHERE rec.rmaStatus = 3
                  UNION
                  SELECT COUNT(rec.rmaNum) as Voided
                  FROM RMARecords rec
                  LEFT JOIN RMAUsers ucl on ucl.id = rec.userCreated
                  WHERE rec.rmaStatus = 4

                  The output of this query is; Created ------- 0 1 3 6 I think because of the union the result set comes like this. What I want as output is different Created Received Closed Voided --------/----------/-------/-------- --0-----/-----1----/--3--/----6--- How can I get as a result set like the upper result? Thanks in advance.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hi, It's clear that you have met some trouble in this problem, why not try esProc to solve it? esProc is a tool for mass data computation, especially fit for the complex data computation like yours. Check below for more details: A Query Language Over-perform SQL[^]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups