MySQL Merge Two SELECT Queries With CASE On One Table

ToddN

I want to merge two queries into one full query. I would need the two queries to group on one aggregate field (MFG). I would assume you use a JOIN but it is on the same table and with a CASE so I'm not sure how that could even work.

1ST QUERY (2015)

SELECT 
    CASE
        WHEN ITEM LIKE '%1%' THEN 'MFG1'
        WHEN ITEM LIKE '%2%' THEN 'MFG2'
        WHEN ITEM LIKE '%3%' THEN 'MFG3'
        ELSE ''
    END AS MFG,
    SUM(COST) AS Cost2015
FROM
    table
WHERE
    TRANS_DATE BETWEEN '2015-12-01' AND '2015-12-31'
GROUP BY MFG

2ND QUERY (2014)

SELECT 
    CASE
        WHEN ITEM LIKE '%1%' THEN 'MFG1'
        WHEN ITEM LIKE '%2%' THEN 'MFG2'
        WHEN ITEM LIKE '%3%' THEN 'MFG3'
        ELSE ''
    END AS MFG,
    SUM(COST) AS Cost2014
FROM
    table
WHERE
    TRANS_DATE BETWEEN '2014-12-01' AND '2014-12-31'
GROUP BY MFG

DESIRED RESULT

MFG  |  Cost2015  |  Cost2014
MFG1 |   1500     |     0
MFG2 |   1000     |    1200
MFG3 |     0      |    3600
     |    100     |     15
M Khalid Junaid

You can use CASE in SUM()

SELECT 
    CASE
        WHEN ITEM LIKE '%1%' THEN 'MFG1'
        WHEN ITEM LIKE '%2%' THEN 'MFG2'
        WHEN ITEM LIKE '%3%' THEN 'MFG3'
        ELSE ''
    END AS MFG,
    SUM(CASE WHEN TRANS_DATE BETWEEN '2014-12-01' AND '2014-12-31' THEN COST ELSE 0 END) AS Cost2014,
    SUM(CASE WHEN TRANS_DATE BETWEEN '2015-12-01' AND '2015-12-31' THEN COST ELSE 0 END) AS Cost2015
FROM
    TABLE
WHERE
    TRANS_DATE BETWEEN '2014-12-01' AND '2015-12-31'
GROUP BY MFG

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Merge two queries in one, GROUP BY and SUM one attribute

분류에서Dev

Merge queries in MySQL

분류에서Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

분류에서Dev

MySQL Best select query from two table

분류에서Dev

MySQL database with two foreign keys in one table

분류에서Dev

combine two different queries into one

분류에서Dev

data.table: merge() one-key table with two-keys table

분류에서Dev

MySQL Case select query

분류에서Dev

How do I insert two values in mysql with just one select?

분류에서Dev

MySql multiple select from two tables and join their results to a third table

분류에서Dev

mysql: select from two tables through a lookup table

분류에서Dev

Select and update same table and row in one connection in mysql

분류에서Dev

using a limit on one table when joining two tables in mysql

분류에서Dev

Given two SELECT statements, how to merge them to get one result minus the other?

분류에서Dev

two selects queries instead of one before insert

분류에서Dev

Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

분류에서Dev

MySQL select records from one table if their id AND username do not appear in a second table

분류에서Dev

MySql select all rows in one table based on MAX value in another table

분류에서Dev

MySQL select data from table and inner join the union all selection from another two identical tables

분류에서Dev

MYSQL Select two distinct values

분류에서Dev

Merge two columns into one and add one extra column

분류에서Dev

CASE 내부의 MySQL SELECT 문

분류에서Dev

combining two mysql table by row

분류에서Dev

MySQL SELECT queries of same limits getting slower and slower

분류에서Dev

How can merge two images to be one image with the two images combined one transparent over the second one?

분류에서Dev

Select when two tables reference the same table

분류에서Dev

DataSet, SqlDataAdapter, Multiple select returns one table

분류에서Dev

Show two models (Images and Treatments) in one table

분류에서Dev

Compare two columns on one table row by row

Related 관련 기사

  1. 1

    Merge two queries in one, GROUP BY and SUM one attribute

  2. 2

    Merge queries in MySQL

  3. 3

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  4. 4

    MySQL Best select query from two table

  5. 5

    MySQL database with two foreign keys in one table

  6. 6

    combine two different queries into one

  7. 7

    data.table: merge() one-key table with two-keys table

  8. 8

    MySQL Case select query

  9. 9

    How do I insert two values in mysql with just one select?

  10. 10

    MySql multiple select from two tables and join their results to a third table

  11. 11

    mysql: select from two tables through a lookup table

  12. 12

    Select and update same table and row in one connection in mysql

  13. 13

    using a limit on one table when joining two tables in mysql

  14. 14

    Given two SELECT statements, how to merge them to get one result minus the other?

  15. 15

    two selects queries instead of one before insert

  16. 16

    Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

  17. 17

    MySQL select records from one table if their id AND username do not appear in a second table

  18. 18

    MySql select all rows in one table based on MAX value in another table

  19. 19

    MySQL select data from table and inner join the union all selection from another two identical tables

  20. 20

    MYSQL Select two distinct values

  21. 21

    Merge two columns into one and add one extra column

  22. 22

    CASE 내부의 MySQL SELECT 문

  23. 23

    combining two mysql table by row

  24. 24

    MySQL SELECT queries of same limits getting slower and slower

  25. 25

    How can merge two images to be one image with the two images combined one transparent over the second one?

  26. 26

    Select when two tables reference the same table

  27. 27

    DataSet, SqlDataAdapter, Multiple select returns one table

  28. 28

    Show two models (Images and Treatments) in one table

  29. 29

    Compare two columns on one table row by row

뜨겁다태그

보관