简体   繁体   中英

how do i pull mysql data with case statements into separate columns

I have table as follows:

sales: customerid, invoice, saledate, amount
journal: reference, date, type, amount
customers: customerid, customername

I need to pull out data from this table and show in following format:

customer name  | jan sales| feb sales| march sales| april sales|

and so on

I am trying the following query:-

     SELECT
    customers.customername AS customer_name,
    CASE WHEN EXTRACT(MONTH FROM journal.date) = 1 THEN SUM(journal.amount) AS january_sales ELSE 0 END,
    CASE WHEN EXTRACT(MONTH FROM journal.date) = 1 THEN SUM(journal.amount) AS feb_sales ELSE 0 END,
    CASE WHEN EXTRACT(MONTH FROM journal.date) = 1 THEN SUM(journal.amount) AS march_sales ELSE 0 END
        FROM customers
        JOIN sales ON sales.customerid = customers.customerid
        JOIN journal ON journal.reference = sales.invoice
        WHERE
        journal.type = 'sale' AND
        journal.date BETWEEN '2013-01-01' AND '2013-03-31' GROUP BY
        customers.customername

i am not able to get this query to work. any help is appreciated.

Use CASE in side the SUM

SELECT 
  customers.customername AS customer_name,
  SUM(
    CASE
      WHEN EXTRACT(MONTH FROM journal.date) = 1 
      THEN journal.amount 
      ELSE 0 
    END
  ) AS january_sales,
  SUM(
    CASE
      WHEN EXTRACT(MONTH FROM journal.date) = 1 
      THEN journal.amount 
      ELSE 0 
    END
  ) AS feb_sales,
  SUM(
    CASE
      WHEN EXTRACT(MONTH FROM journal.date) = 1 
      THEN journal.amount 
      ELSE 0 
    END
  ) AS march_sales 
FROM
  customers 
  JOIN sales 
    ON sales.customerid = customers.customerid 
  JOIN journal 
    ON journal.reference = sales.invoice 
WHERE journal.type = 'sale' 
  AND journal.date BETWEEN '2013-01-01' 
  AND '2013-03-31' 
GROUP BY customers.customername 

I think this is what you want:

 SELECT c.customername AS customer_name,
        sum(CASE WHEN month(j.date) = 1 THEN j.amount end) AS january_sales,
        sum(CASE WHEN month(j.date) = 2 THEN j.amount end) AS feb_sales,
        sum(CASE WHEN month(j.date) = 3 THEN j.amount end) AS march_sales
 FROM customers c JOIN
      sales s
      ON s.customerid = c.customerid JOIN
      journal j
      ON j.reference = s.invoice
 WHERE
    j.type = 'sale' AND
    j.date BETWEEN '2013-01-01' AND '2013-03-31'
  GROUP BY c.customername;

Your case statement was all messed up. For instance, it had the column alias inside the statement rather than after it. I made a couple other changes. I think month() is easier than extract(month) (a matter of opinion because the latter is ANSI standard). I also put in table aliases to make the query easier to read.

Note that you should probably be looking at the year value as well as the month, in case the range for the query extends over 12 months.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM