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
Related
Due to Great support from Tim Biegeleisen, i finally got this, i want to make it little bit more look feel removing the Duplicates while ordering the Customer. But i dont want the Customer to be duplicate.
My output is this:
Query:
SELECT Customer, Product, Price
FROM yourTable
UNION ALL
SELECT Customer, 'Total', SUM(Price)
FROM yourTable
GROUP BY Customer
UNION ALL
SELECT '', 'Grand Total', SUM(Price)
FROM yourTable
ORDER BY CASE WHEN Customer = '' THEN 1 ELSE 0 END,
Customer,
CASE WHEN Product = 'Total' THEN 1 ELSE 0 END,
Product;
This is the Output i got with Above Query
Customer Product Price
-------------------------------
Suryam Shampo 100
Suryam Rubber 150
Suryam Glass 250
Suryam Total 500----->Total
Raghu Bottel 60
Raghu Biscuit 180
Raghu Total 240----->Total
Grand Total 740----->Grand Total
But i want this if it is possible:
Customer Product Price
---------------------------------
Suryam Shampo 100
Rubber 150
Glass 250
Total 500----->Total
Raghu Bottel 60
Biscuit 180
Total 240----->Total
Grand Total 740----->Grand Total
I am practising my skills in MySQL using the Sakila DB.
I would I have created a view called rentals_customer_store_film_category with is the union of customers, payments, rentals, film, and category tables.
I would now like to get the top 5 films by income. Meaning that I would lie to sum up all incomes of each film by store and then return the first 5.
I tried the below code but it does not work.
I cannot figure out what is wrong with it
Any help?
SELECT store_id, film_id, income
FROM
(SELECT film_id, store_id, sum(amount) as income,
#store_rank := IF(#current_store = store_id, #store_rank + 1, 1) AS store_rank,
#current_store := store_id
FROM rentals_customer_store_film_category
group by store_id, film_id
ORDER BY store_id, income DESC, film_id
) ranked
WHERE store_rank <= 5
RESULTS BELOW. As you can see, it does not stop at the fifth row per store. It shows all films by store while I would like only the top 5 for store:id 1 and top 5 for store id 2.
store_id film_id income
1 971 134.82
1 879 132.85
1 938 127.82
1 973 123.83
1 865 119.84
1 941 117.82
1 267 116.83
1 327 110.85
1 580 106.86
1 715 105.85
1 897 104.85
...
...
...
...
2 878 127.83
2 791 127.82
2 854 123.83
2 946 117.86
2 396 113.81
2 369 111.84
2 764 110.85
2 260 110.84
2 838 110.82
2 527 109.83
2 893 106.84
2 71 102.87
2 8 102.82
...
...
...
...
The order in this case is important to compare the previous store_id with the current,try this:
SELECT store_id, film_id, income
FROM
(SELECT film_id, store_id, sum(amount) as income,
#First compare previus with current
#store_rank := IF(#prev_store = store_id, #store_rank + 1, 1) AS store_rank,
#asign previus store
#prev_store := store_id
FROM films
group by store_id, film_id
ORDER BY store_id, income DESC, film_id
) ranked
WHERE store_rank <= 5
My data looks like this:
UserID Hours BillRate
1 1.50 2.25
1 2.50 3.25
1 3.50 3.25
2 5.50 4.25
2 6.50 5.25
2 7.50 5.25
In detail page, I have this query to get total spend for each UserID
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
GROUP BY UserID
The result is 22.88 for UserID_1 and 96.88 for UserID_2 (the total is 119.76)
In summary page, I have to run a query with 2 SELECT statements to get the right total:
SELECT SUM(TotalSpend)
FROM (
SELECT UserID, ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
WHERE UserID IN (1, 2)
GROUP BY UserID
) a
Is there anyway that i can get the total in summary page with one SELECT statement?
SELECT ROUND(SUM(Hours*BillRate), 2) AS TotalSpend
FROM mytable
WHERE UserID IN (1, 2)
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've got this table:
id | payment_id | quantity | unit cost
1 633 1 750
2 633 1 750
3 632 2 750
4 632 2 750
What I need is :
payemnt_id | total
633 1500
632 3000
You can also think of this as Countries and then you are trying to find the total for each Country. I was sure there were some tutorials like this but could not find by STFW.
You can simply put the formula (expression) inside SUM:
SELECT payment_id, SUM(`unit cost` * quantity) AS total
FROM myTable
GROUP BY payment_id
SELECT
payment_id,
SUM(subtotal) AS total
FROM(
SELECT
payment_id,
(unit_cost * quantity) AS subtotal
) AS t1
GROUP BY
payment_id
SELECT payemnt_id, SUM(`unit cost` * quantity) as total
FROM Table1
GROUP BY payemnt_id
SELECT COALESCE (SUM(cost_field * quantity_field),0) AS tot FROM Table_Name
I Think This One Is Good, Why ?
Because If Their is No Result It Will Give You Zero