Quantity Litre BillNo
2 1.00 1
3 0.76 1
5 1.00 2
5 1.00 2
Expected Result
Quantity Litre BillNo
5 4.28 1
10 10.00 2
How to count litre
in BillNo 1
2 Quantity * 1.00 Litre = 2.00 Litre
3 Quantity * 0.76 Litre = 2.28 Litre
So Total litre Of Bill 1 is 4.28 and Quantity 2+3=5,
Same as in Bill 2, How can i get Expected Result.
Sorry if i can't explain well......
Use sum and group by
select
sum(quantity) as quantity
, sum(litre) as litre
, sum(quanty*litre) as Total
, billNo
from my_table
group by biiNo
Related
Hello I have two tables:
Table: 1 11_counted_stock
id_counted_stock part_number Number of Tag quantity
1 abcde 2 1000
2 aaaaa 4 222
3 eeeee 6 442
Table:2 11_stock_actual_whs_costed
id_stock material batch A.stock
1 abcde 12334 500
2 aaaaa 23543 233
3 xxxxx 234234 299
Result Query Im looking for
Material Stock Counted_Stock Difference
abcde 500 1000 -500
aaaaa 233 222 11
eeeee 0 442 -442
xxxxx 299 0 299
Thanks a lot!
This is my best attempt but I could only sum :(
SELECT mat, qty, SUM(qty) as TotalQty
FROM (
SELECT 11_counted_stock.part_number AS mat, (11_counted_stock.quantity) AS qty
FROM 11_counted_stock
GROUP BY 11_counted_stock.part_number
UNION
SELECT 11_stock_actual_whs_costed.material, SUM(11_stock_actual_whs_costed.available_stock) AS quantity
FROM 11_stock_actual_whs_costed
GROUP BY 11_stock_actual_whs_costed.material
) as t
GROUP BY mat
;
SELECT Material,
SUM(Stock) AS Stock,
SUM(Counted_Stock) AS Counted_Stock,
SUM(Stock - Counted_Stock) AS Difference
FROM (
SELECT part_number AS Material, 0 AS Stock, quantity AS Counted_Stock
FROM 11_counted_stock
UNION ALL
SELECT material, stock, 0
FROM 11_stock_actual_whs_costed
) total
GROUP BY Material;
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;
I'm facing problem with joining three tables and getting sum of its value I have three types of table Tax Types, Purchase Order and Purchase Items
currently I'm facing to get result of tax report kindly check below.
Tax Types
id name
1 No Tax
2 VAT #10%
3 GST #6%
4 VAT #20%
Purchase Order
id total order_tax_id order_tax total_tax grand_total
1 10.0000 1 0.0000 0.0000 10.0000
2 10.0000 2 1.0000 1.0000 11.0000
3 10.0000 3 0.6000 0.6000 10.6000
4 10.0000 4 2.0000 2.0000 12.0000
5 10.0000 1 0.0000 0.9100 10.0000
6 10.0000 1 0.0000 0.5700 10.0000
7 10.0000 1 0.0000 1.6700 10.0000
9 10.0000 2 1.0000 1.5700 11.0000
Purchase Items
id purchase_id tax_rate_id item_tax tax subtotal
1 1 1 0.0000 0.0000 10.0000
2 2 1 0.0000 0.0000 10.0000
3 3 1 0.0000 0.0000 10.0000
4 4 1 0.0000 0.0000 10.0000
5 5 2 0.9100 10.0000% 10.0000
6 6 3 0.5700 6.0000% 10.0000
7 7 4 1.6700 20.0000% 10.0000
9 9 3 0.5700 6.0000% 10.0000
And the output should be like this
tax_name sum_of_subtotal sum_of_item_tax sum_of_grand_total sum_of_total_tax
VAT #20% 10.00 1.67 10.00 2.00 3.67
VAT #10% 10.00 1.67 10.00 2.00 3.67
No Tax 40.00 0.00 0 0 0.00
GST #6% 10.00 1.67 10.00 2.00 3.67
Kindly help me
Query I used
SELECT
tax_rates.name,
(SUM(purchase_items.subtotal)) AS item_total_amount,
(SUM(purchase_items.item_tax)) AS item_tax_amount,
(SUM(purchases.grand_total) - SUM(purchases.order_tax)) AS total_amount,
(SUM(purchases.order_tax)) AS tax_amount
FROM tax_rates
LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id
LEFT JOIN purchase_items ON purchase_items.tax_rate_id = tax_rates.id
GROUP BY tax_rates.id
ORDER BY tax_rates.name desc
LIMIT 0, 10
I fixed the issue
SELECT
tax_rates.name,
purchase_item.subtotal AS item_total_amount,
purchase_item.item_tax AS item_tax_amount,
(purchases.grand_total - purchases.order_tax) AS total_amount,
purchases.order_tax AS tax_amount
FROM tax_rates
LEFT JOIN (SELECT a.order_tax_id, SUM(a.order_tax) as order_tax, SUM(a.grand_total) as grand_total FROM purchases AS a GROUP BY a.order_tax_id) AS purchases ON purchases.order_tax_id = tax_rates.id
LEFT JOIN (SELECT a.tax_rate_id, SUM(a.item_tax) as item_tax, SUM(a.subtotal) as subtotal FROM purchase_items AS a GROUP BY a.tax_rate_id) AS purchase_item ON purchase_item.tax_rate_id = tax_rates.id
GROUP BY tax_rates.id
ORDER BY tax_rates.name desc
LIMIT 0, 10
I am trying to create a query from sales-order-details and invoice for an income statement sales revenues.
when I put this into the sql to look up the query
SELECT salesno, quantity, price, price* quantity
FROM inventory, sales_order_details
LIMIT 0, 5
it keeps giving the same quantity and sales number for all of them. the prices are right and so is the price.. what am I doing wrong?
salesno quantity price and quantity*price
5455 153 2.00 306.00
5455 153 6.00 918.00
5455 153 5.00 765.00
5455 153 4.00 612.00
5455 153 5.00 765.00
My sales-order-details table looks like this
SQL query:
SELECT * FROM `sales_order_details` ORDER BY `sales_order_details`.`quantity` DESC LIMIT 0, 5 ;
Rows: 5
salesno inno quantity
5459 6319 290
5458 6318 220
5455 6315 153
5457 6317 110
5456 6316 101
and my invoice table looks like this
SQL query:
SELECT * FROM `inventory` LIMIT 0, 30 ;
Rows: 5
inno pname qoh price cost
6315 roses 500 2.00 1.00
6316 sunflowers 200 6.00
6317 dandy lions 270 5.00
6318 violets 300 4.00 2.00
6319 tulips 130 5.00 2.50
I need inno, pname, price, quantity and the price*quantity
(Please refer to SQLFiddle for a working example of this post)
I have a table with stock information, as follows:
sp100_id _date bullishness agreement
----------------------------------------------
1 2011-03-16 1.01 0.33
1 2011-03-17 0.85 1.28
1 2011-03-18 0.89 1.25
1 2011-03-21 1.46 1.21
1 2011-03-22 0.39 2.53
2 2011-03-16 3.07 1.27
2 2011-03-17 2.09 0.80
2 2011-03-18 0.91 0.12
2 2011-03-21 1.50 0.00
2 2011-03-22 2.62 1.10
3 2011-03-16 0.73 1.13
3 2011-03-17 1.13 1.21
3 2011-03-18 1.12 0.45
3 2011-03-21 1.00 1.01
3 2011-03-22 1.00 0.53
4 2011-03-16 0.40 1.10
4 2011-03-17 2.40 0.03
4 2011-03-18 3.16 0.10
4 2011-03-21 0.86 0.50
4 2011-03-22 1.00 0.10
I need to order the companies (sp100_id) by their averge bullishness over time into a top-3:
SELECT
sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id LIMIT 3
This works fine, as the result is
SP100_ID BULLISHNESS AGREEMENT
2 2.038 0.658
4 1.564 0.366
3 0.996 0.866
Now that I have the top-3, I need the top-3 to be re-ordered by AGREEMENT, ascending:
SP100_ID BULLISHNESS AGREEMENT
4 1.564 0.366
2 2.038 0.658
3 0.996 0.866
Is this possible to do with one query? I tried the following but it didn't work. It still only orders by bullishness
SELECT
sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id
ORDER BY bullishness DESC, agreement ASC LIMIT 3
So to be clear: (1) I need to find the top-3 companies with highest average bullsihness (2) this top-3 then needs to be ordered from lowest to highest agreement. Preferably with one query. Do you know how?
It's called structured query language because you can build structures in which queries (aka virtual tables) are nested inside other queries.
Take your first query, which is correct except it needs its own ORDER BY clause, and nest it in another, like so.
SELECT *
FROM (
SELECT sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id
ORDER BY bullishness DESC
LIMIT 3
) subquery
ORDER BY agreement ASC
Go fiddle: http://sqlfiddle.com/#!2/c9ff0/7/0