Sum of column for distinct rows - mysql

I have the following MySQL table structure
id product_id order_id order_shipping shipping_code
1 23 1 2.00 GB
2 12 1 2.00 GB
3 5 2 3.50 GB
4 6 2 3.50 GB
5 34 3 3.00 FR
6 8 3 3.00 FR
7 4 4 5.00 IN
I'd like to group the rows by the shipping_code and include the total orders and total shipping cost per code. Something like this:
Code Orders Cost
GB 2 5.50
FR 1 3.00
IN 1 5.00
I'm struggling with the total cost part. The shipping cost is for the order as a whole, not the product. It's just duplicated in each row relating to the same order. So for order #1 the shipping cost was £2, not £4.
How do I do a sum of that column but only for the distinct order rows?

To get around the duplicated shipping costs for each order you need to group your data by order_id first, and then COUNT the number of orders and SUM the shipping costs:
SELECT code, COUNT(o.shipping) AS orders, SUM(o.shipping) AS cost
FROM (SELECT MIN(shipping_code) AS code, MIN(order_shipping) AS shipping
FROM orders
GROUP BY order_id) o
GROUP BY code
Output:
code orders cost
GB 2 5.5
FR 1 3
IN 1 5
Demo on dbfiddle

Related

GROUP BY addition of product multiple order tables

Table 1
ProductID
HSN CODE
GST
1
JJKK
5
2
J1
12
3
JJKK
5
4
J2
18
5
J1
12
6
JJKK
5
7
J2
18
8
J3
18
9
JJKK
5
TABLE 2
PRODUCT ID
PRODUCT PRICE
2
20
3
30
4
40
5
30
6
40
7
20
9
30
TABLE 3
PRODUCT ID
QUANTITY
2
2
3
4
4
6
6
4
7
2
9
1
What I need is the following output
HSN CODE
TOTAL OF QUANTITY AS PER HSN
GST of HSN(B)
GROSS TOTAL OF PRICE(PRODUCT PRICE * QUANTITY)(A)
TOTAL PRICE (A+(B percent of A))
J1
2
12
(2x20) = 40
40+(12% of 40) = 44.8
JJKK
9
5
(4x30)+(4x40)+(1x30) = 310
310+(5% of 310) = 325.5
J2
8
18
(6x40)+(2x20) = 280
280+(18% of 280) = 330.4
TABLE 3 is the base table, ie we need to look for product ids 2,3,4,6,7,9.
The logic for output table is described below:
FOR HSN LOGIC (COLUMN 1 LOGIC)
So for HSN we will look for HSN of these ids from table 1 & then Group by it with HSN & remove duplicate HSN. The answer is written in column 1 of output.
FOR TOTAL OF QUANTITY AS PER HSN (COLUMN 2 LOGIC)
We now add quantity as per product id from table 2 and display as total as per HSN ie. HSN of 3,6 and 9 are same so we will add quantity of table 2 of these id and place it in frnt of JJKK of HSN in column 2. Similar logic for other hsn value will apply.
FOR GST OF HSN (COLUMN 3 LOGIC)
We will write unique gst from table 1 which is written in same row value of HSN, ie HSN JJKK has GSt of 5%. So we will write 5.and same logic goes for other HSN.
FOR GROSS TOTAL OF PRICE (COLUMN 4 LOGIC)
Here we will take product price from table 2 for product id mentioned in table 3. We will multiply that price with quantity of table 3 and the result will be added as per HSN. ie. for product id 3,6 and 9 the HSN is same then we will add (430)+(440)+(1*30)
TOTAL PRICE (A+(B percent of A)) (COLUMN 5 LOGIC)
Here we will add Gross total pf price marked as A with the number mentioned under GST of HSN. ie for J1 HSN the value for column 4 will be (40+(12% of 40))

MySQL: calculate percentiles among each group

I have a Transaction table with transactions data :
"created" - DateTime of transaction
"price" - transaction price
"id": product identifier
Sample data
id
created
price
5
2022-05-08 20:20:00
1
5
2022-05-08 19:00:00
2
5
2022-05-08 7:40:00
3
5
2022-05-05 8:20:00
4
2
2022-05-09 10:40:00
5
2
2022-05-09 10:40:00
6
2
2022-05-07 15:40:00
7
2
2022-05-03 16:30:00
8
Goal: to calculate the 25% percentile for prices and the number of transactions with prices lower than the 25 price percentile(25% percent of all) per id.
Expected result:
id
price 1st q
n_transactions
5
2
1
2
6
1
I have tried:
SELECT
id,
MAX(CASE WHEN Quartile = 1 THEN price END) 1Quartile,
FROM (
SELECT
id,
price,
NTILE(4) OVER (PARTITION BY id ORDER BY price) AS Quartile
FROM
Transactions) Vals
GROUP BY
id
ORDER BY
id
which should return the 1 quartile for the price but it returns only an execution error with the message "check SQL syntax".
MySQL version: 5.7.36

How to add(sum) the column data in SQL

ID pcID contractor approver claimed
-------------------------------------------
1 1 one 1000 900
2 1 two 200 100
3 1 three 1000 1000
4 1 six 100 11
5 2 six 100 22
6 3 six 120 1
7 4 three 102 10
From the above table I need to sum the approver amount and claimed amount based on the contractor. Like the below given table. All has been done by using the stored procedure.
ID pcID contractor approver claimed TotalApprover TotalClaimed
--------------------------------------------------------------------------
1 1 one 1000 900 1000 900
2 1 two 200 100 200 100
3 1 three 1000 1000 1000 1000
4 1 six 100 11 100 11
5 2 six 100 22 200 33
6 3 six 120 1 320 34
7 4 three 102 10 1120 1001
Like the above table I need an output adding(sum) in ascending order based on contractor.
Thanks in advance.
You can use window function
select t.*,
sum(approver) over( partition by contractor order by ID) TotalApprover,
sum(claimed) over( partition by contractor order by ID) TotalClaimed,
from table1 t
You want cumulative sums:
select t.*,
sum(approver) over (partition by contractor order by claimid) as totalapprover,
sum(claim) over (partition by contractor order by claimid) as totalclaim
from t;
This is just accumulated the corresponding values by the dimensions you are asking for.

How to use group_concat with the sum of quantity based on price?

This is my table which store id with its qty and price. I would like to sum qty based on price. Sometimes the qty might be show 0 if qty is negative value. Thus, i will not show the price of qty fall in 0 value when group by price.
id | id_item | qty | price
1 1 10 1.00
2 1 15 2.00
3 1 10 1.00
4 2 5 2.00
5 2 5 2.50
6 3 10 1.00
7 3 10 1.00
8 3 5 1.00
This is what i have tried.
Select id_item, price, sum(qty) as total from sales group by id_item, price having total !=0;
Result
id_item | qty | price
1 20 1.00
1 15 2.00
2 5 2.00
2 5 2.50
3 10 1.00
Expected result with concat
id_item | qty | price
1 20,15 1.00,2.00
2 5,5 2.00.2.50
3 10 1.00
How can i achieve the result as shown?
I think you need two levels of aggregation:
select id_item, group_concat(total order by price) as quantities,
group_concat(price order by price) as prices
from (Select id_item, price, sum(qty) as total
from sales
group by id_item, price
having total <> 0
) s
group by id_item;

SQL query - two group bys on intermediate result in a query

I have a fairly long and complex query that ends up producing a result like this:
item_id | shop_id | sold
5 3 0
5 3 1
5 3 1
8 4 0
8 6 1
8 9 0
I need to then to somehow do a SELECT item_id, COUNT(DISTINCT (shop_id)) FROM tableAbove GROUP BY item_id to get:
item_id | number of shops that sell/have sold item
5 1
8 3
However, I also need for each item the number of shops that have successfully sold it, So something like this:
item_id | number of shops that have sold item
5 1
8 1
Does anyone know how I can put them bother together in a query to give
item_id | shops that sell item |number of shops that have sold item
5 1 1
8 3 1
SELECT item_id,
COUNT(DISTINCT shop_id),
COUNT(DISTINCT CASE WHEN sold = 1 THEN shop_id END)
FROM my_query
GROUP BY
item_id