Inserting json data into query - mysql

I'm looking for help for one of my previous coding challenge, so I have two table, the first one is Sales and the second one is Product. Which look something like this :
Sales table:
ord_id item_id cust_id status qty total_price order_date
---------------------------------------------------------------------------------
1 A308 CS209 Sold 1 30000 10-10-21
2 A405 CS209 Cancel 4 44000 10-18-21
3 B476 CS890 Sold 8 6400 10-29-21
and Product table
item_id item_category item_price item_name date_created date_updated
----------------------------------------------------------------------------------------
A308 Electronic 30000 TV 08-10-21 10-10-21
A405 Furniture 11000 Chair 12-25-20 10-18-21
B476 Misc 800 Cutter 05-11-21 10-29-21
I was told to write a query to get a result set like this:
order_date detail total_customer total_price
-------------------------------------------------------------------------------------------
10-10-21 {"item_id":"A308","item_category":...} 1 30000
10-29-21 {"item_id":"B476","item_category":...} 1 6400
The other parts are easy but I'm still confused on how to turn certain columns from the Product table into JSON and inserting that json into the Detail column as shown above.
I've done something like this to get the first part :
SELECT order_date, COUNT(cust_id) as total_customer, SUM(total_price) as total_price
FROM Sales
WHERE status = "sold"
GROUP BY order_date
Can anyone help me how to do this in a single query?

Related

Incremental subtotal in MySQL result

I need to calculate - let's say - item prices based on purchase price and date (FIFO/LIFO). So, let's say I've made theese purchases:
3 x ABC for $100 at 2017-11-10
2 x ABC for $80 at 2017-11-12
5 x XYZ for $120 at 2017-11-11
7 x XYZ for $110 at 2017-11-12
Items first bought determine final product price, so ABC will cost $100 + markup for first 3 items sold, and $80 + markup for another 2.
What I need now is a sort of cache table, that should look like so:
SKU price max_qty
ABC 100 3
ABC 80 5
XYZ 120 5
XYZ 110 12
Please note that third column contains not purchased quantity, but rather a sort of "cap" of quantity to which this price is referring. That way I can quickly estimate sell price for 4 items of ABC by doing a simple JOIN on max_qty >= basket_qty. This is important as there will be much more reads than writes to this table.
What i'm struggling with is generating such table in a manner regarding performance and resource cost. I can't do it in a "query per sku" manner, as there can be quite a lot of records to update at a time. I've tried using MySQL variables;
SELECT
sku,
price,
#r := #r + qty AS max_qty
FROM
(SELECT #r := 0) AS r
JOIN
warehouse_items
GROUP BY
sku
ORDER BY
sku, date_in
This resultset would be easly inserted into cache table, but #r doesn't reset for each new sku in the result.

MYSQL for selecting values from table and show with comma separator

I have two tables:
sales Table
===============================
id cust_id total_price
===============================
1 1 1000
2 2 1500
sales_item Table
======================================================
id sales_id cust_id product quantity
======================================================
1 2 2 pen 2
2 2 2 pencil 3
3 1 1 book 2
4 1 1 pencil 2
I need to query these two tables inorder to get the following result:
=========================================
sales_id cust_id product
=========================================
2 2 pen,pencil
1 1 book,pencil
Can anyone help me to query these 2 tables inorder to get the above result??
I tried using GROUP_CONCAT. Hers is what I have tried:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
And the result I got is:
======================================================
id cust_id total_price product
======================================================
2 2 1500 pen,pencil,book
This is the not the result am expecting..
Try something like this, consider that this is not tested, it may help you.
select sales.id,sales.cust_id, concat(sales_item.product)
from sales LEFT JOIN sales_item ON sales_item.sales_id = sales.id
group by sales.id
I got the answer by using GROUP_CONCAT itself. Here is my Query:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
GROUP BY s.id

Find all rows with matching columns with two conditions

I have two tables and I want to find which orders have two rows in the line_items table and also where one of those rows has the sku of BALL. Basically trying to find all the customers who ordered only a ball, regardless of the qty of the BALL row. There needs to be two rows per order at least, because one of the rows will always be for the SHIPPING sku.
In the below data, john and sam would be the only valid orders returned because the only skus their orders have in the line_items table are BALL and SHIPPING (regardless of BALL qty). I'd like the tables joined so that the returned data would have the order_id, customer, date_placed, and qty.
TABLE orders
id customer date_placed
===========================
0 john 1/1/2000
1 bill 2/1/2000
2 sam 2/5/2000
TABLE line_items
id order_id qty sku
=========================
0 0 1 BALL
1 0 1 SHIPPING
2 1 1 BALL
3 1 1 ROPE
4 1 1 SHIPPING
5 2 3 BALL
6 2 1 SHIPPING
Thank you so much!
If I understand correctly, you should be able to write it like this:
SELECT o.id, customer, date_placed, li.qty
FROM orders o
INNER JOIN line_items li
ON o.id=li.order_id AND li.sku='BALL'
WHERE EXISTS(
SELECT order_id
FROM line_items tli
WHERE o.id=tli.order_id
GROUP BY order_id
HAVING count(*)=2)

what is mysql query for below four tables

I have four tables.
User:
user_id user_name client_id
1 abhi 1
2 ravi 2
Client:
client_id client_name products
1 tom cake, patties
2 pet cookie, cake
Products:
product_id product_name
1 cake
2 cookie
3 patties
Report:
report_id product_id
1 1
2 3
3 2
4 1
5 3
If Ravi login then he only able see report like below:
report_id product_id
1 1
3 2
4 1
I need only MySQL Query
In your "Client" table instead of saving multiple values of "product" split it and put only "product id" rather than actual product value that's why you have table "product". Your table "client" is not in First normal form. So if possible, change the structure of table
I agree with Simemon's answer, you need to normalize your DB tables.
Then also if you want to go with this table structure only, you can try this.
select * from reports where product_id in (select product_id from products where product_name in (select products from clients where client_id = (select client_id from users where username = 'ravi' limit 1)))
I have not tested this. let me know if it gives any error, I will check it.

Query to eliminate multiple rows based on oldest date

Working in MS Access 2003 SP3: I have a query that I am running to find what 'cars' were sold with a date after the delivery date. I have thousands of rows. When it is all said and done, I want to just have a handful of rows for each 'car' and then the oldest date. Any suggestions?
CAR DATE ORDERED DATE DELIVERED CUSTOMER NUMBER DATE SOLD
FORD MUSTANG 20061002 20080413 ABC123 20080422
FORD MUSTANG 20061002 20080413 ABC124 20080429
CHEVY IMPALA 20061002 20080413 ABC125 20080505
This could be better if you had an ID field:
DELETE
FROM Cars
WHERE Cars.DATESOLD Not In (
SELECT TOP 5 DateSold
FROM Cars c
WHERE c.Car=Cars.Car
ORDER BY DateSold DESC)
And Cars.DATESOLD Not In (
SELECT TOP 1 DateSold
FROM Cars c
WHERE c.Car=Cars.Car
ORDER BY DateSold)
If there are duplicate dates, you will end up with more than 5 records.