All the customers who have spend more than 1200 - mysql

I need to write a query where on this table
CREATE TABLE sales (
OrderID INT,
OrderDate DATE,
OrderPrice INT,
OrderQuantity INT,
customerName VARCHAR(20)
);
This is my table right now
+---------+------------+------------+---------------+--------------+
| OrderID | OrderDate | OrderPrice | OrderQuantity | customerName |
+---------+------------+------------+---------------+--------------+
| 1 | 2005-12-22 | 160 | 2 | Smith |
| 2 | 2005-08-10 | 190 | 2 | Johason |
| 3 | 2005-07-13 | 500 | 5 | Baldwin |
| 4 | 2005-07-15 | 420 | 2 | Smith |
| 5 | 2005-12-22 | 1000 | 4 | Wood |
| 6 | 2005-10-02 | 820 | 4 | Smith |
| 7 | 2005-11-03 | 2000 | 2 | Baldwin |
+---------+------------+------------+---------------+--------------+
Where I need to get the names of all the unique customers who have spent more than 1200

Use aggregation and having:
select customerName
from sales
group by customerName
having sum(orderPrice) > 1200

Related

Joins with two tables conditionally based on the value of ONE column

I have following tables to manage purchase and issues of items.
item Table
+---------+-----------+
| item_id | item_name |
+---------+-----------+
| 500 | A4 |
| 501 | A5 |
| 502 | B5 |
| 503 | B4 |
| 504 | A3 |
+---------+-----------+
supplier Table
+-------------+---------------+
| sup_id | supplier_name |
+-------------+---------------+
| 1 | ABC Suppliers |
| 2 | DEF Suppliers |
| 3 | GHI Suppliers |
+-------------+---------------+
officer Table
+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1 | Jhon |
| 2 | William |
| 3 | Ken |
| 4 | Robert |
+------------+--------------+
purchase / issue table
+-----------+---------+---------+---------+------------+-----+-------------+
| update_id | bill_no | sup_id | item_id | date | qty | type |
+-----------+---------+---------+---------+------------+-----+-------------+
| 1000 | 10 | 1 | 500 | 2018-11-01 | 50 | purchase |
| 1001 | 40 | 1 | 500 | 2018-11-02 | 25 | purchase |
| 1002 | 32 | 2 | 500 | 2018-11-04 | 10 | issue |
| 1003 | 25 | 3 | 500 | 2018-11-05 | 12 | issue |
| 1004 | 14 | 1 | 500 | 2018-11-08 | 22 | purchase |
| 1005 | 55 | 2 | 501 | 2018-11-09 | 10 | purchase |
| 1006 | 30 | 2 | 502 | 2018-11-10 | 40 | purchase |
+-----------+---------+---------+---------+------------+-----+-------------+
02) purchase / issue table holds the purchase details and issue details by mentioning the "type" at the end of table.
03) I need to get the following output.
+-----------+------------------------------+------------+-----+------------+
| item_name | supplier_name / officer_name | date | qty |type |
+-----------+------------------------------+------------+-----+------------+
| A4 | ABC Suppliers | 2018-11-01 | 50 | purchase |
| A4 | ABC Suppliers | 2018-11-02 | 25 | purchase |
| A4 | William | 2018-11-04 | 10 | issue |
| A4 | Ken | 2018-11-05 | 12 | issue |
| A4 | ABC Suppliers | 2018-11-08 | 22 | purchase |
| A5 | DEF Suppliers | 2018-11-09 | 10 | purchase |
| B5 | DEF Suppliers | 2018-11-10 | 40 | purchase |
+-----------+------------------------------+------------+-----+------------+
03) I used the following query
select item.item_name,
supplier.supplier_name,
purchase.date,
purchase.qty,
purchase.type
from purchase
join item on item.item_id = purchase.item_id
where supplier.sup_id in (select sup_id from supplier)
04) But I did't get the desired output. I can not understand what I am going wrong. Can anyone help me ?
In your SELECT clause, you are referring to supplier table, without joining to that table. Now, it seems that you want to show officer_name when the purchase type is issue, else supplier_name when it is purchase.
We can do a LEFT JOIN to both the tables, and use CASE .. WHEN to determine the respective name.
I have removed the WHERE .. IN subquery, which does not seem to serve any purpose here.
select item.item_name,
CASE purchase.type
WHEN 'purchase' THEN supplier.supplier_name
WHEN 'issue' THEN officer.officer_name
END AS `supplier_name_officer_name`
purchase.date,
purchase.qty,
purchase.type
from purchase
join item on item.item_id = purchase.item_id
left join supplier on purchase.sup_id = supplier.sup_id
left join officer on purchase.sup_id = officer.officer_id
You need to join the supplier just as you did for the item.
And I see no point in the where clause at all.

Running total by item and week

I am trying to determine what my inventory level for each product is at the end of each business week.
I have a table that records all product transactions
+------------+-------------+------------+------------+
| ProductID | Quantity | Created_at |Cause |
+------------+-------------+------------+------------+
| 1 | 200 | 2015-06-01 |Delivery |
| 1 | -2 | 2015-06-02 |Order |
| 1 | -1 | 2015-06-12 |Order |
| 2 | 45 | 2015-06-15 |Delivery |
| 2 | -5 | 2015-06-16 |Order |
| 2 | -1 | 2015-06-17 |Broken |
| 1 | 100 | 2015-06-21 |Delivery |
+------------+-------------+------------+------------+
I would need to sum the quantity only up to the particular date, by product, to show something like this
+------------+-------------+------------+
| ProductID | Quantity | Week |
+------------+-------------+------------+
| 1 | 198 | 2015-06-05 |
| 1 | 197 | 2015-06-12 |
| 1 | 197 | 2015-06-19 |
| 2 | 39 | 2015-06-19 |
| 1 | 297 | 2015-06-26 |
| 2 | 39 | 2015-06-26 |
+------------+-------------+------------+
I have tried various combinations of With Rollup and #runtot:=
But have not been successful so far.
So, something like:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(product_id INT NOT NULL
,created_at DATE NOT NULL
,quantity INT NOT NULL
,cause VARCHAR(20) NOT NULL
,PRIMARY KEY(product_id,created_at)
);
INSERT INTO my_table VALUES
(1,'2015-06-01',200,'Delivery'),
(1,'2015-06-02', -2,'Order'),
(1,'2015-06-12', -1,'Order'),
(2,'2015-06-15', 45,'Delivery'),
(2,'2015-06-16', -5,'Order'),
(2,'2015-06-17', -1,'Broken'),
(1,'2015-06-21',100,'Delivery');
SELECT x.*
, CASE WHEN #prev = product_id THEN #i:=#i+total ELSE #i:=total END running
, #prev:=product_id prev
FROM
( SELECT product_id
, YEARWEEK(created_at) yw
, SUM(quantity) total
FROM my_table
GROUP
BY product_id
, yw
) x
, (SELECT #prev:=null,#i:=0) vars
ORDER
BY product_id
, yw;
+------------+--------+-------+---------+------+
| product_id | yw | total | running | prev |
+------------+--------+-------+---------+------+
| 1 | 201522 | 198 | 198 | 1 |
| 1 | 201523 | -1 | 197 | 1 |
| 1 | 201525 | 100 | 297 | 1 |
| 2 | 201524 | 39 | 39 | 2 |
+------------+--------+-------+---------+------+

How to Add Serial No in Select Statement in Mysql Join

There is two table in database.
== tblOrder ==
OrderId
Customer
== tblOrderItem ==
OrderItemId
OrderId
ItemId
Qty
Price
My Query :-
SELECT o.OrderId,o.Customer,oi.ItemId,oi.Qty,oi.Price
FROM tblorder o
JOIN tblorderitem oi ON oi.OrderId=o.OrderId
Result :-
+---------+----------+--------+-----+-------+
| OrderId | Customer | ItemId | Qty | Price |
+---------+----------+--------+-----+-------+
| 1001 | john day | 501 | 1 | 10 |
| 1001 | john day | 502 | 2 | 9 |
| 1002 | amy gill | 201 | 5 | 2 |
| 1003 | hardcaur | 501 | 1 | 10 |
| 1003 | hardcaur | 509 | 2 | 5 |
| 1003 | hardcaur | 201 | 2 | 2 |
+---------+----------+--------+-----+-------+
I want to generate SNO and SNOI(Temp Serial No) in select statement so that result will be like this :-
+------+---------+----------+------+--------+-----+-------+
| SNO | OrderId | Customer | SNOI | ItemId | Qty | Price |
+------+---------+----------+------+--------+-----+-------+
| 1 | 1001 | john day | 1 | 501 | 1 | 10 |
| 1 | 1001 | john day | 2 | 502 | 2 | 9 |
| 2 | 1002 | amy gill | 1 | 201 | 5 | 2 |
| 3 | 1003 | hardcaur | 1 | 501 | 1 | 10 |
| 3 | 1003 | hardcaur | 2 | 509 | 2 | 5 |
| 3 | 1003 | hardcaur | 3 | 201 | 2 | 2 |
What will be my query?
You can use variables.
set #ord ='';
set #val1 =1;
set #val2 =0;
select SR_No_1, OrderId, Customer, SR_No_2, ItemId, Qty, Price
from
(
SELECT t.*,
#val1 := if(#ord=OrderId, #val1+1, 1) as SR_No_2,
#val2 := if(#ord=OrderId,#val2, #val2+1) as SR_No_1,
#ord := OrderId
FROM table1 t
) t
ORDER BY orderId asc;
Result:
+---------+---------+----------+---------+--------+-----+-------+
| SR_No_1 | OrderId | Customer | SR_No_2 | ItemId | Qty | Price |
+---------+---------+----------+---------+--------+-----+-------+
| 1 | 1001 | john day | 1 | 501 | 1 | 10 |
| 1 | 1001 | john day | 2 | 502 | 2 | 9 |
| 2 | 1002 | amy gill | 1 | 201 | 5 | 2 |
| 3 | 1003 | hardcaur | 1 | 501 | 1 | 10 |
| 3 | 1003 | hardcaur | 2 | 509 | 2 | 5 |
| 3 | 1003 | hardcaur | 3 | 201 | 2 | 2 |
+---------+---------+----------+---------+--------+-----+-------+
DEMO
P.S. Kindly note that for demonstration purpose i have inserted data to one table. you can modify the query by introducing the join between 2 tables

MySql: get data from two tables with grouping by Date

I have two table with almost same columns, i want to merge them and grouped by date
Table1: order_payments_detail
| payment_by | amount( as Debit in result) | added_on |
| Ali | 1000 | 2014-09-21 |
| Aslam | 2000 | 2014-09-25 |
| Akram | 4000 | 2014-09-28 |
Table2: orders
| cust_name | amount( as credit in result)| added_on |
| Shop1 | 1000 | 2014-09-22 |
| Shop2 | 2000 | 2014-09-26 |
| Shop3 | 4000 | 2014-09-29 |
Result will be like this
| particulars| debit | credit | added_on |
| Ali | 1000 | null | 2014-09-21 |
| Shop1 | null | 1000 | 2014-09-22 |
| Aslam | 2000 | null | 2014-09-25 |
| Shop2 | null | 2000 | 2014-09-26 |
| Akram | 4000 | null | 2014-09-28 |
| Shop3 | null | 4000 | 2014-09-29 |
You can readily do this with union all:
select payment_by, amount as debit, NULL as credit, added_on
from order_payments_detail
union all
select cust_name, NULL, amount, added_on
from orders
order by added_on;
By "grouped by date", I assume that you really mean "ordered by date".

Complex MySQL query summing joined records where parent and child ids are equal

Maybe this will be an easy one for some of you MySQL masters who see this stuff like a level 3 children's book.
I have multiple tables that I'm joining to produce statistical data for a report and I'm getting tripped up at the moment trying to figure it out. It's obviously imperative the figures are correct because it impacts a number of decisions going forward.
Here's the lay of the land (not the full picture, but you'll get the point):
Affiliate Table
+----+-----------+------------+---------------------+
| id | firstname | lastname | created_date |
+----+-----------+------------+---------------------+
| 1 | Mike | Johnson | 2010-11-22 17:44:37 |
| 2 | Trevor | Wilson | 2010-12-23 16:24:24 |
| 3 | Bob | Parker | 2011-11-04 10:33:49 |
+----+-----------+------------+---------------------+
Now our query should only find results for Bob Parker (id 3) so I'll only show example results for Bob.
Affiliate Link Table
+-----+-----------+--------------+-----------+----------+---------------------+
| id | parent_id | affiliate_id | link_type | linkhash | created_date |
+-----+-----------+--------------+-----------+----------+---------------------+
| 21 | NULL | 3 | PRODUCT | fa2e82a7 | 2011-06-15 16:18:37 |
| 27 | NULL | 3 | PRODUCT | 55de2ae7 | 2011-06-23 01:03:00 |
| 28 | NULL | 3 | PRODUCT | 02cae72f | 2011-06-23 01:03:00 |
| 29 | 27 | 3 | PRODUCT | a4dfb2c8 | 2011-06-23 01:03:00 |
| 30 | 28 | 3 | PRODUCT | 72cea1b2 | 2011-06-23 01:03:00 |
| 36 | 21 | 3 | PRODUCT | fa2e82a7 | 2011-06-23 01:07:03 |
| 59 | 21 | 3 | PRODUCT | ec33413f | 2011-11-04 17:49:17 |
| 60 | 27 | 3 | PRODUCT | f701188c | 2011-11-04 17:49:17 |
| 69 | 21 | 3 | PRODUCT | 6dfb89fd | 2011-11-04 17:49:17 |
+-----+-----------+--------------+-----------+----------+---------------------+
Affiliate Stats
+--------+--------------+--------------------+----------+---------------------+
| id | affiliate_id | link_id | order_id | type | created_date |
+--------+--------------+---------+----------+----------+---------------------+
| 86570 | 3 | 21 | NULL | CLICK | 2013-01-01 00:07:31 |
| 86574 | 3 | 21 | NULL | PAGEVIEW | 2013-01-01 00:08:53 |
| 86579 | 3 | 21 | 411 | SALE | 2013-01-01 00:09:52 |
| 86580 | 3 | 36 | NULL | CLICK | 2013-01-01 00:09:55 |
| 86582 | 3 | 36 | NULL | PAGEVIEW | 2013-01-01 00:09:56 |
| 86583 | 3 | 28 | NULL | CLICK | 2013-01-01 00:11:04 |
| 86584 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:11:04 |
| 86586 | 3 | 30 | NULL | CLICK | 2013-01-01 00:30:18 |
| 86587 | 3 | 30 | NULL | PAGEVIEW | 2013-01-01 00:30:20 |
| 86611 | 3 | 69 | NULL | CLICK | 2013-01-01 00:40:19 |
| 86613 | 3 | 69 | NULL | PAGEVIEW | 2013-01-01 00:40:19 |
| 86619 | 3 | 69 | 413 | SALE | 2013-01-01 00:42:12 |
| 86622 | 3 | 60 | NULL | CLICK | 2013-01-01 00:46:00 |
| 86624 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:46:01 |
| 86641 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:55:58 |
| 86642 | 3 | 30 | 415 | SALE | 2013-01-01 00:56:35 |
| 86643 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:56:43 |
| 86644 | 3 | 60 | 417 | SALE | 2013-01-01 00:56:52 |
+--------+--------------+---------+----------+----------+---------------------+
Orders
+------+--------------+---------+---------------------+
| id | affiliate_id | total | created_date |
+------+--------------+---------+---------------------+
| 411 | 3 | 138.62 | 2013-01-01 00:09:50 |
| 413 | 3 | 312.87 | 2013-01-01 00:09:52 |
| 415 | 3 | 242.59 | 2013-01-01 00:09:55 |
| 417 | 3 | 171.18 | 2013-01-01 00:09:55 |
+------+--------------+---------+---------------------+
Now the results that I need should look like this (only show main/parent link id)
+---------+---------+
| link_id | total |
+---------+---------+
| 21 | 451.49 | <- 1 order from parent (21), 1 from child (69)
| 27 | 171.18 | <- 1 order from child (69)
| 28 | 242.59 | <- 1 order from child (30)
+---------+---------+
I'm not quite sure how to write the query so that I can sum where affiliate_link.id and affiliate_link.parent_id are combined. Is this even possible with a couple of JOINs and GROUPing?
I'm not too sure why you have denormalised affiliate_id (by placing it in each table) and, therefore, whether one can rely on all Stats and Orders that stem from a particular Link to have the same affiliate_id as that Link.
If it's possible, I'd suggest changing the AffiliateLink.parent_id column such that parent records point to themselves (rather than NULL):
UPDATE AffiliateLink SET parent_id = id WHERE parent_id IS NULL
Then it's a simple case of joining and grouping:
SELECT AffiliateLink.parent_id AS link_id,
SUM(Orders.total) AS total
FROM AffiliateLink
JOIN AffiliateStats ON AffiliateStats.link_id = AffiliateLink.id
JOIN Orders ON Orders.id = AffiliateStats.order_id
WHERE AffiliateLink.affiliate_id = 3
GROUP BY AffiliateLink.parent_id
See it on sqlfiddle.
If it's not possible to make the change, you can effectively create the resulting AffiliateLink table using UNION (but beware the performance implications, as MySQL will not be able to use indexes on the result):
(
SELECT parent_id, id, affiliate_id FROM AffiliateLink WHERE parent_id IS NOT NULL
UNION ALL
SELECT id , id, affiliate_id FROM AffiliateLink WHERE parent_id IS NULL
) AS AffiliateLink
See it on sqlfiddle.