I have a table where I have data from sales.
The structure is
ProductID
Items
Price
Action
a1
10
100
1
a1
6
60
0
a1
5
50
2
a2
3
30
1
a2
4
40
0
a2
1
70
2
When the Action is 0 then the items not count on Sum.
When the Action is 1 then the items are count on Sum.
When the Action is 2 then the items are substruct from the Sum.
So, I want to make a Sum on Items based on Action field.
The correct result must be 5 Items on product a1 and 2 Item for the product a2.
Do you have any ideas on how can I do this?
You can use a case expression in combination with aggregation (group by, sum):
select ProductID,
sum(case Action
when 2 then Items
when 3 then -Items
else 0
end) SumItems
from Sales
group by ProductID;
you can try something like this
with x (ProductID,Items,Price,Action)
as
(
Select 'a1','10','100','1'
union all Select 'a1','6','60','0'
union all Select 'a1','5','50','2'
union all Select 'a2','3','30','1'
union all Select 'a2','4','40','0'
union all Select 'a2','1','70','2'
)
Select
y.ProductID,
sum(y.[Count]) as sum
from
(
Select
*,
case when Action = 0 then 0 when Action = 1 then items when Action = 2 then -1*items end as Count
from x
) y
GROUP by
y.ProductID
I dont know for other databases but this is the query for Access.
select ProductID, sum(switch (Action = 2, -Items,action=1,Items)) as
SumItems from Sales group by ProductID
Related
I have the below records in my table
ID Name Price
1 XYZ 100
2 XYZ 100
3 AAA 100
4 AAA 100
5 ABC 100
6 ABD 100
I would like to group single row entries into one group and multi-row entries into another group.
The Output will have only 2 rows- Single Entry and Multiple Entry and Percentage of the Price both Categories hold when compared with Total.
ABC and ABD are single Entries as they have only 1 row and XYZ and AAA are multiple entries as they have 2 rows. I would like to sum all Price columns of multiple entries and Single entries and then calculate the percentage
Output:
Category Percentage
Single_Entry 33.3% ABC and ABD: (200/600*100)
Multiple_Entries 66.7% XYZ and AAA: (400/600*100)
How to achieve this in mySQL.
I view this as two levels of aggregation:
select (case when cnt = 1 then 'single' else 'multiple' end) as entry_type,
sum(price) / sum(sum(price)) over() as ratio
from (select category, count(*) as cnt, sum(price) as price
from t
group by category
) c
group by entry_type;
You can use two levels of aggregation:
select category, 100 * sum(price) / sum(sum(price)) over() as percentage
from (
select sum(price) price,
case when count(*) = 1 then 'single entry' else 'multiple entriers' end as category
from mytable t
group by name
) t
group by category
I have a table with the following entries:
id customer amount kind
1 123 15 g
2 123 30 op
3 234 20 g
4 345 25 g
5 456 12 g
6 456 15 op
What I want to do is to sum all amounts with the kind "g".
Now I want to add a condition:
"Only sum the amount to the sum if there is another entry of the customer with the kind 'op'"
Means my result should be 27 in this case and not 72.
What's a good way to add this condition?
Thanks!
To get the sum for each customer do
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
to get the total sum do
select sum(c_sum)
from
(
select customer, sum(case when kind = 'g' then amount else 0 end) as c_sum
from your_table
group by customer
having sum(kind = 'op') > 0
) tmp
Hi I have two tables one is product and next is price table
Product Table
Id Name
1 Bike
2 Car
3 Van
Price Table
Id Price Pid
1 100 1
2 150 1
3 200 1
4 100 2
5 110 2
6 120 2
7 300 3
8 310 3
My Sql query
$sql = "SELECT SUM(CASE WHEN price >= 0 AND price <= 200 THEN 1 ELSE 0 END) AS `0-2`,
SUM(CASE WHEN price >= 201 AND price <= 500 THEN 1 ELSE 0 END) AS `2-5`,
COUNT(pid) AS `All Values`
FROM price where pid IN(1,2,3)";
when I run this query price count values showing like below
0-2 has (6) count
2-5 has (2) count
but here I need to display as like.
here I am looking to display as two products between 0-200 matched that are 1,2 pids and so on
0-2 (2) count
2-5 (1) count
because in price table there are more price options there for products so every product has 2 to 5 different prices in the table but I should display it as one product count even more prices had.
Kindly tell me how to write mysql query.
I would appreciate you help.
You can use COUNT(DISTINCT CASE ..) and in THEN clause use pid instead of 1
SELECT
COUNT(DISTINCT CASE WHEN price >= 0 AND price <= 200 THEN pid END) AS `0-2`,
COUNT(DISTINCT CASE WHEN price >= 201 AND price <= 500 THEN pid END) AS `2-5`,
COUNT(DISTINCT pid) AS `All Values`
FROM price
WHERE pid IN(1,2,3)
DEMO
I have an orders table with the following fields : id, name, price_paid
The easiest part is this:
SELECT
name,
SUM(price_paid) AS total_price_paid
FROM
Orders GROUP BY
name
How should I modify my SQL statement so I get the following at the output?
name, total_price_paid, purchase_level
Where purchase level would be:
1 if total_price_paid is in the range of 0 - 100
2 if in a range 101-350
and 3 if above 350
Thank you in advance.
SELECT
name,
SUM(price_paid) AS total_price_paid,
CASE WHEN SUM(price_paid) BETWEEN 0 AND 100 THEN 1
WHEN SUM(price_paid) BETWEEN 101 AND 350 THEN 2
WHEN SUM(price_paid) > 350 THEN 3 END AS purchase_level
FROM
Orders
GROUP BY
name
Use conditional sum:
SELECT
name,
SUM(IF(price_paid BETWEEN 0 AND 100, price_paid, 0)) AS sum_0_100,
SUM(IF(price_paid BETWEEN 101 AND 350, price_paid, 0)) AS sum_101_350,
SUM(IF(price_paid>350, price_paid, 0)) AS sum_350_plus
FROM
Orders
GROUP BY
name
Or else, with level:
SELECT
name,
SUM(price_paid),
CASE
WHEN price_paid BETWEEN 0 AND 100 THEN 1
WHEN price_paid BETWEEN 101 AND 350 THEN 2
WHEN price_paid>350 THEN 3
END AS level
FROM
Orders
GROUP BY
name,
-- don't forget group then:
level
Difference between those two queries are that fires will result in pivot while second will result in plain rows grouping.
I am trying to compare 2 rows and display the same ones.I did browse but was not able to find the right solution.
Table A
Count status Division
20 A 1
30 B 2
10 c 1
12 z 1
From the above table I want to display whose division is same.
Count status Division
20 A 1
10 c 1
12 z 1
Try this
Select * from TableA
Group By Division
Having Count(*) > 1
Select * from TableA
Group By Division
having Count(*) = 1
Here i used case statement , it worked for me
select CompanyCode ,'Commission Pec', Year
,sum(case when CommissionType='Commission Recevied' then JAN else 0 end)/sum(case when CommissionType='Net Payments from WM' and isnull(JAN,0)<>0 then JAN else 1 end)
from Commission_Consolidate
group by CompanyCode,Year
end