简体   繁体   中英

MySQL split a single column data into two columns

may you find my question as duplicate, but I didn't get my answer from any of them or I could not create my query.

I have a table like below.

---------------------------------------------------------------------------------
|    Customer Name    |    Date    |  Product Name  |  Qty  |  Rate  |  Amount  |
---------------------------------------------------------------------------------
|  Customer1          | 10/10/2014 |  Product1      |   10  | 100.00 | 1000.00  |
|  Customer1          | 12/10/2014 |  Product1      |    1  |   0.00 |    0.00  |
|  Customer2          | 14/10/2014 |  Product1      |   10  | 100.00 | 1000.00  |
|  Customer1          | 16/10/2014 |  Product1      |   10  | 100.00 | 1000.00  |
|  Customer2          | 16/10/2014 |  Product2      |   10  | 100.00 | 1000.00  |
|  Customer2          | 16/10/2014 |  Product2      |    1  |   0.00 |    0.00  |
|  Customer1          | 20/10/2014 |  Product2      |   20  | 150.00 | 1500.00  |
---------------------------------------------------------------------------------

I want to show the result like below...

---------------------------------------------------------
|    Customer Name    |  Product Name  |  Qty  |  Free  |
---------------------------------------------------------
|  Customer1          |  Product1      |   20  |      1 |
|  Customer1          |  Product2      |   20  |      0 |
|  Customer2          |  Product1      |   10  |      0 |
|  Customer2          |  Product2      |   10  |      1 |
---------------------------------------------------------

It means the splitting the Qty column into two based on Rate / Amount in a date range.

In my program I use to save the free quantity as 0.00 rate.

I have seen and tried many processes but failed.

My SQL query is like below.

SELECT CustomerName,ProductName,SUM(Qty), CASE WHEN Rate > 0 AND Amount > 0, CASE WHEN Rate < 1 AND Amount < 1 FROM salesdetails WHERE InvoiceDate BETWEEN '02/11/2014' AND '13/11/2014' GROUP BY ProductId, SoldTo ORDER BY CustomerName ASC

But it is showing error.

When I tried...

SELECT CustomerName,ProductName,SUM(Qty) FROM salesdetails WHERE InvoiceDate BETWEEN '" & dtt & "' AND '" & dtf & "' AND Rate > 0 AND Amount > 0 GROUP BY ProductId, CustomerName ORDER BY CustomerName ASC

It is showing the records in single column ( SUM(Qty) ) skipping the free quantity (Rate = 0) because I have written the query to do this.

What should be the query ?

Please help me to solve this problem.

Regards

Tested and works:

SELECT Customer, 
       Product, 
       SUM(IF(Rate>0,Qty,0)) AS Qty, 
       SUM(IF(Rate=0,Qty,0)) AS Free
FROM salesdetails 
WHERE InvoiceDate BETWEEN '02/10/2014' AND '13/11/2014'
GROUP BY Customer, Product;

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