How do I remove not needed information from table? - mysql

im trying to make a column for month 1, 2 and 3 and put the invoice's total in that column. I have made that successfully, but then below there are more columns with zeroes for every column, aka it shows me the column for every invoice, but I want to get ONLY the invoices that have been made in month 1,2 and 3 and don't show the rest. How do I do that?
what I get:
invoice_id | total | firstMonth | secondMonth | thirdMonth |
1 33.4 0 0 33.4
2 3434.5 0 0 0 <=====
what I want:
invoice_id | total | firstMonth | secondMonth | thirdMonth |
1 33.4 0 0 33.4

Please learn about types. Single quotes should be used for string and date constants -- and not for column identifiers or numbers.
Then, I think you just want to filter the results correctly:
SELECT i.id AS invoice_ID , i.total AS TOTAL,
(CASE WHEN month(i.datetime) = 1 THEN ABS(i.total) ELSE 0 END) AS firstMonth,
(CASE WHEN month(i.datetime) = 2 THEN ABS(i.total) ELSE 0 END) AS secondMonth,
(CASE WHEN month(i.datetime) = 3 THEN ABS(i.total) ELSE 0 END) AS thirdMonth
FROM invoice i JOIN
store s
ON i.store_id = s.id JOIN
company c
ON s.company_id = c.id
WHERE c.name = 'RubberDuck' AND
i.datetime >= '2019-01-01' AND
i.date_time < '2019-04-01'

Related

MySQL duplicate entries search with selective date criteria

Having trouble wrapping my head around having an efficient "duplicate entries" select in a single query.
In the below example, duplicate StockNo can exist spanning multiple Date. I want to search StockNo for duplicate entries, and if at least 1 StockNo record is found within the Date current YEAR-MONTH, then I also need to select its partner that could exist in any other YEAR-MONTH. Is this possible?
Example Query:
SELECT * FROM `sales`
WHERE `StockNo` IN
(SELECT `StockNo` FROM `sales` GROUP BY `StockNo` HAVING COUNT(*) > 1)
AND `Date` LIKE '2016-11-%'
ORDER BY `StockNo`, `TransactionID`;
Example Data:
ID | StockNo | Date
1 | 1 | 2016-11-01
2 | 1 | 2016-11-10
3 | 2 | 2016-11-05
4 | 2 | 2016-10-29
5 | 3 | 2016-10-25
6 | 3 | 2016-10-15
With my example query and data, I have 3 pairs of duplicate entries. It's pretty obvious that I will only return 3 records (ID's 1, 2 & 3) due to AND Date LIKE '2016-11-%', however I need to return ID's 1, 2, 3, 4. I want to ignore ID's 5 & 6 because neither of them fall within the current month.
Hope that makes sense. Thanks for any help you can provide.
SELECT StockNo
FROM sales
GROUP BY StockNo
HAVING SUM(CASE WHEN DATE_FORMAT(Date, '%Y-%m') = '2016-11' THEN 1 ELSE 0 END) > 0
If you also want to retrieve the full records for those matching stock numbers in the above query, you can just add a join:
SELECT s1.*
FROM sales s1
INNER JOIN
(
SELECT StockNo
FROM sales
GROUP BY StockNo
HAVING SUM(CASE WHEN DATE_FORMAT(Date, '%Y-%m') = '2016-11' THEN 1 ELSE 0 END) > 0
) s2
ON s1.StockNo = s2.StockNo
Demo here:
SQLFiddle
Thank you very much Tim for pointing me in the right direction. Your answer was close but it still only returned records from the current month and in the end I used the following query:
SELECT s1.* FROM `sales` s1
INNER JOIN
(
SELECT * FROM `sales` GROUP BY `StockNo` HAVING COUNT(`StockNo`) > 1
AND SUM(CASE WHEN DATE_FORMAT(`Date`, '%Y-%m')='2016-11' THEN 1 ELSE 0 END) > 0
) s2
ON s1.StockNo=s2.StockNo
This one had been eluding me for some time.

MySQL - get COUNT depends on the value

How can I get the COUNT() of the specific field depends on the value of the field? For example I have the field typeOfAssistance , in the query below I got the total numbers of the typeOfAssistance but I have different values in it which is financial medical and burial, How can I add custom column that will divide the total value depends on the value?
SELECT date,COUNT(*) AS num
FROM requests
WHERE date BETWEEN DATE_ADD(CURDATE(),INTERVAL -20 DAY) AND CURDATE()
GROUP BY date
desired output:
date | financial | burial | medical | total
2014-04-25 | 1 | 2 | 3 | 6
Thanks. Sorry for the explanation. :)
Typically for something like that I would use SUM rather than COUNT for the item breakdowns.
Something like
SELECT date,
SUM(CASE WHEN typeOfAssistance = 'financial' THEN 1 ELSE 0 END) AS financial,
SUM(CASE WHEN typeOfAssistance = 'burial' THEN 1 ELSE 0 END) AS burial,
SUM(CASE WHEN typeOfAssistance = 'medical' THEN 1 ELSE 0 END) AS medical,
COUNT(1) AS Total
FROM requests
GROUP BY date

MySQL select using row counts and grouping

I have 2 tables, orders and order_lineitems.
orders contains the order status info (sold date, invoice no, type of sale, etc)
order_lineitems contains the item(s) for each order in a one to many relationship.
Since we provide shipping info by line item, ship_date is in the order_lineitems table, null if not shipped, a date if it is shipped.
I am trying to pull the orders where all items have shipped by comparing the number of line item rows against the line item rows that have a ship date. While I have successfully pulled all that info, I am unable to make the last step, limiting the result set to include only the completely shipped orders (number of rows = number of rows where ship_date is not null).
I know I am missing something simple, but just don't see it..
select sum(custom.lineitems) as totalitems, sum(custom.shipped) as totalshipped,
custom.invoice, z.shipregion
from (
select a.invoice, count(a.invoice) as lineitems, 0 as shipped
from order_lineitem a
group by a.invoice
UNION ALL
select b.invoice, 0 as notshipped, count(b.ship_date) as shipped
from order_lineitem b
where b.ship_date is not null
group by b.invoice
) as custom
left join orders z on custom.invoice = z.invoice
where z.result = 0
and z.respmsg like 'Approved%'
and z.shipregion <> 'PENDING'
and z.cancelorder = 0
group by custom.invoice;
This returns a result set like so (one row for each invoice in the DB)
totalitems totalshipped invoice shipregion
4 2 1000 REGION08
1 1 10001 REGION07
1 1 10004 REGION05
3 1 10006 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
What I am looking for is a result set like this - only where totalitems = totalshipped
totalitems totalshipped invoice shipregion
1 1 10001 REGION07
1 1 10004 REGION05
2 2 10007 REGION04
1 1 10008 REGION08
7 7 10009 REGION01
1 1 1001 REGION08
Use HAVING clause
SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice HAVING lineitems = shipped
OR
SELECT a.invoice, a.shipregion, a.lineitems, a.shipped
FROM (SELECT a.invoice, z.shipregion, COUNT(a.invoice) AS lineitems,
SUM(CASE WHEN a.ship_date IS NOT NULL THEN 1 ELSE 0 END) AS shipped
FROM order_lineitem a
LEFT JOIN orders z ON a.invoice = z.invoice AND z.result = 0 AND z.cancelorder = 0 AND
z.respmsg LIKE 'Approved%' AND z.shipregion <> 'PENDING'
GROUP BY a.invoice
) AS a WHERE a.lineitems = a.shipped
One more outer query needed.
select * from
(
\\Your whole query here
) as Result
where Result.totalitems = Result.totalshipped

SELECT without grouping

How get results without grouping it?
My table
id user_id amount currency_id type status
5 2 2.00 1 0 0
6 3 3.00 1 0 0
4 1 1.00 1 0 0
7 4 4.00 1 0 0
8 5 3.00 1 1 0
I do the following select
SELECT id, user_id, amount, currency_id, SUM( amount )
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
Result:
id user_id amount currency_id SUM( amount )
5 2 2.00 1 6.00
How get this result:
id user_id amount currency_id SUM( amount )
5 2 2.00 1 0 6.00
6 3 3.00 1 0 6.00
4 1 1.00 1 0 6.00
If your intent is to return individual records that meet this criteria AND sum them up and you don't actually need the SUM value as a field on every row (not sure why you would), then I would suggest looking at the GROUP BY ... WITH ROLLUP modifier. It works like this:
SELECT id, user_id, SUM(amount) AS `amounts`, currency_id
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
GROUP BY id WITH ROLLUP
Here I am grouping by id because this will keep the individual records intact as this value is unique
You output would look like this:
id user_id amounts currency_id
5 2 2.00 1
6 3 3.00 1
4 1 1.00 1
NULL 3 6.00 1
Note the last record provides the rollup to the SUM() function. Also note that values for user_id and currency_id in the rollup row are indeterminate as they were not part of the GROUP BY or aggregation. As such, they have no meaning.
You can do join
SELECT id,
user_id,
amount,
currency_id,
a.totalAmount
FROM market
CROSS JOIN
(
SELECT SUM(amount) totalAmount
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
) a
WHERE amount <=3
AND type = 0
AND status = 0
or using inline subquery,
SELECT id,
user_id,
amount,
currency_id,
(
SELECT SUM(amount) totalAmount
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
) totalAmount
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
Here you go:
SELECT id, user_id, amount, currency_id, t2.total
FROM market, (
SELECT SUM(amount) AS total
FROM market
WHERE amount <=3
AND type = 0
AND status = 0
) AS t2
WHERE amount <=3
AND type = 0
AND status = 0

SQL select ordered product quantities based on delivery date sorted by week

I am not sure if my question is clear but what I need is quite complex query. I am unable to put it together myself.
We have tables orders, order_items, products, deliveries and delivery_items. Orders is the main order table. Order_items table holds the list of ordered products in certain order. Products is the main products table and deliveries/delivery_items tables hold the list of delivered order_items (we can deliver entire order or only partially).
This is the 'stripped down' table structure:
ORDERS:
ID ORDER_NUMBER DELIVERY_DATE STATUS
-------------------------------------------------
1 2013-00001 Unixtimestamp Closed
2 2013-00002 Unixtimestamp Open
...
PRODUCTS:
ID CODE
-----------------------
1 Product 1
2 Product 2
3 Product 3
...
ORDER ITEMS:
ID ORDER_ID PRODUCT_ID QTY
-----------------------------------------
1 1 1 2
2 1 2 5
3 1 3 1
4 2 3 10
DELIVERIES:
ID ORDER_ID DELIVERY_NUMBER TYPE
---------------------------------------------
1 1 2013-00001 Full
2 2 2013-00002 Partial
...
DELIVERY_ITEMS:
ID DELIVERY_ID ORDER_ITEM_ID QTY
------------------------------------------
1 1 1 2
2 1 2 5
3 1 3 1
4 2 4 5
...
Our production demands view where all non-delivered order_items(products) are listed by quantities in the upcoming week schedule. What I need is something like this (quantities here are random):
Product Overdue W0 W1 W2 W3 W4 W5 Later Total
-------------------------------------------------------------------------
Product 1 1 2 0 0 0 0 0 0 3
Product 2 0 3 5 1 0 0 0 4 13
Product 3 2 4 0 7 5 0 0 0 18
...
The entire view is based on current time and order's delivery_date field. The query would need to get ordered product quantities from all open orders, check if some of there products were maybe already delivered and subtract delivered quantities, and on the end sort the result quantities as shown above.
UPDATE: here is SQL Fiddle with above structure with some data http://sqlfiddle.com/#!2/88891/4
---Using Fiddle provided (http://sqlfiddle.com/#!2/88891/34/0)
SELECT P.Name,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=0 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) AS Overdue,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=7 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>0 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W0,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=14 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>7 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W1,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=21 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>14 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W2,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=28 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>21 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W3,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=35 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>28 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W4,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())<=42 and DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>35 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as W5,
sum(case when DATEDIFF(FROM_UNIXTIME(Delivery_date),curDate())>42 then coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0) else 0 END) as Later,
sum(coalesce(OI.Quantity,0)-coalesce(DI.Quantity,0)) as Total
FROM ORDERS O
INNER JOIN ORDER_ITEMS OI
ON OI.Order_ID = O.ID
INNER JOIN PRODUCTS P on
P.ID = OI.Product_ID
LEFT JOIN DELIVERIES D
ON D.Order_ID = O.ID
LEFT JOIN DELIVERY_ITEMS DI
ON DI.Delivery_ID = D.ID
AND OI.ID = DI.Order_Item_ID
WHERE coalesce(DI.Quantity,0) < OI.Quantity
GROUP BY P.Name
Thanks for the fiddle. This takes care of all the remaining syntax errors. and formats to include 0 in the results.
Coalesce, takes first non-null value in an unlimited series.
FROM_UNIXTIME converts into to valid date time for comparison using datediff.
else statments handle situations when no data matches critiera thus 0 is
evaluated.
where clause eliminates orders with all items delivered or more than all items ordered delivered. (Thus items closed will still be included since they are not fully shipped!)
To get you started, here are a couple of joins:
SELECT o.ORDER_NUMBER, p.CODE, oi.QTY
FROM orders o
JOIN order_items oi ON oi.ORDER_ID = o.ID
JOIN products p ON p.ID = oi.PRODUCT_ID
And:
SELECT d.DELIVERY_NUMBER, d.ORDER_ID, di.ORDER_ITEM_ID, di.QTY
FROM deliveries d
JOIN delivery_items di ON di.DELIVERY_ID = d.ID
What would you have to do to tie those together?