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.
Related
I have the following table:
id
code
amount
qty
1
1
25
36
2
2
30
6
3
5
100
1
4
1
25
100
5
1
20
1
6
4
10
136
7
1
10
20
I want to find the sum of all amounts where code = 1, and for all such occurrences also want comma separated values of all qty and comma separated value of all ids.
Eg:
The output should look like:
code
amount
quantities
ids
1
80
36, 100,1, 20
1,4,5, 7
I know I can do something like
SELECT
code
,SUM(amount)
FROM
table1
where code = 1
group by code;
for getting the sum corresponding to that code but don't know how to get all such quantities and ids.
DBFiddle
You can simply use GROUP_CONCAT to group all your data:
SELECT
t.`code`,
SUM(amount) ,
GROUP_CONCAT(t.`qty` SEPARATOR ',') AS qtys,
GROUP_CONCAT(t.`id` SEPARATOR ',') AS ids
FROM
yourTable t
WHERE t.`code` = 1
GROUP BY t.`code` ;
GROUP_CONCAT by default uses comma (,) as separator, so you can write same query as:
SELECT
t.`code`,
SUM(amount) ,
GROUP_CONCAT(t.`qty`) AS qtys,
GROUP_CONCAT(t.`id`) AS ids
FROM
yourTable t
WHERE t.`code` = 1
GROUP BY t.`code` ;
If you want some other separators, you can exclusively define that too.
In MySQL you can use GROUP_CONCAT
Query #1
select
code,
sum(amount) as total_amount,
GROUP_CONCAT(id) as ids,
GROUP_CONCAT(qty) qts
from yourTable
where code = 1
GROUP BY code;
code
total_amount
ids
qts
1
80
1,4,5,7
36,100,1,20
View on DB Fiddle
In Postgres you can use string_agg
Query #1
select
code,
sum(amount) as total_amount,
string_agg(id::text,',') as ids,
string_agg(qty::text , ',') qts
from yourTable
where code = 1
GROUP BY code;
code
total_amount
ids
qts
1
80
1,4,5,7
36,100,1,20
View on DB Fiddle
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 that records product clicks. For example:
productId ipv4 date flag
100 32.59.xx.xx 2015-11-29 1
100 32.59.xx.xx 2015-11-29 1
101 32.59.xx.xx 2015-11-29 1
100 64.34.xx.xx 2015-11-29 1
100 64.34.xx.xx 2015-11-29 1
I need to group by productId but only count one productId-ipv4 pair. I need to ignore any duplicate clicks on a product from the same user.
Expected result:
productId COUNT(*) SUM(flag)
100 2 2
101 1 1
How would I structure the query?
Something like this:
select productid, count(distinct ipv4), count(distinct ipv4, flag)
from foo
group by productId
Check sqlfiddle: http://sqlfiddle.com/#!9/ac487/1
Consider the following table:
id, invoice_number, item_number, qty, price
1 5000 200 1 4.50
2 5000 201 2 5.50
3 5000 201 1 5.75
2 5000 202 1 5.50
3 5000 202 1 5.75
4 5000 203 1 6.00
How can I get the following result:
invoice, item, count
5000, 200, 1
5000, 201, 2
5000, 202, 2
5000, 203, 1
I know I can group by the invoice number, but then I cannot simply count the invoices, or I will lose their name. Additionally I would love to know if they have a different price, even if it was just a flag, however I am thinking to get that level of detail I might as well put this in a script and loop through the items, its just that my data set is very large, and id love to do it with pure SQL :)
Thanks in advance!
To get your exact desired output:
select invoice_number, item_number, count(*)
from tbl
group by invoice_number, item_number
To get your desired output along with the number of unique prices within the group, and what those prices are:
select invoice_number,
item_number,
count(*) as num_rows,
count(distinct price) as num_prices,
group_concat(distinct price) as prices
from tbl
group by invoice_number, item_number
I have this result set from my query:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
I need to return this result into this:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
Just combining the CustomerId and ProducerId into UserId. Same with the Payment Columns.
Is there any way to achieve this with using just a simple select and group by? I'm avoiding temp tables, calling multiple same queries and like for optimization. I hope this is possible.
Thanks a lot
SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders
Try something like this:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
Dont know how you query looks like atm?