I have this records in products table with product_id and its price
product_id
price
1
150
1
190
2
20
2
12
3
123
4
513
5
157
5
147
and I want to get the top 3 products and arrange it by average price something like this
product_id
price
avg_price
4
513
513
1
150
170
1
190
170
5
157
152
5
147
152
how to write/code it in sql query or laravel eloquent query?
WITH AverageCTE AS
(
SELECT product_id, AVG(avg_price) as avg_price
FROM products
GROUP BY product_id
)
SELECT p.product_id, price, avg_price
FROM product p JOIN
(SELECT * FROM AverageCTE ORDER BY avg_price DESC LIMIT 3) a
on p.product_id = a.product_id
ORDER BY avg_price DESC
Table_1 has order_id, country_id details
table_ID order_id country_id
1 100 IN
2 200 USA
3 300 UK
4 400 IN
5 500 UK
6 600 UK
7 700 USA
8 800 USA
9 900 IN
10 1000 UK
Table_2 has shipment_id, order_id details
Shipment_ID order_id
1 100
2 100
3 100
4 200
5 200
6 300
7 300
8 400
9 500
11 500
12 600
13 700
14 700
15 700
16 700
17 800
18 800
19 800
20 900
21 900
22 1000
23 1000
24 1000
I used the following query to find out list of order_id which are for country_id='IN'
select `order_id`
from `Table_1`
where `country_id` = 'IN';
order_id
100
400
900
I need guidance to write the query to find the count of shipment_id which will are mapped to order_id from 'IN'
So order_id 100 has 3 shipment, 400 has 1 and 900 has 2 shipment
Desired final output
count_of_shipment_id
6
here is the query you need:
SELECT country_id, count(*) as count_of_shipment_id
FROM Table_1 a
inner join Table_2 b on a.`order_id` = b.`order_id`
group by country_id
if you need only one country you can always add "where" or "having" to filter the result.
here you can see the sample you posted:
http://sqlfiddle.com/#!9/c90424/2
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
I have a simple table with 10000 rows
product_id | price | price_inc_vat | vat_rate
I want to be able to update the value of price_inc_vat based on the value of price
I have tried this
UPDATE 1_products AS s3,
1_products AS t3
SET t3.price_inc = ROUND(s3.price*1.3,2)
WHERE s3.vat_rate = 20 AND t3.vat_rate=20;
It doesn't error but seems to be taking a very long time as I had to kill the query after 1/2 hour.
Why the join?
UPDATE products SET price_inc_vat = ROUND(price*1.3,2) WHERE vat_rate=20;
UPDATED
I'm not quite sure what you are trying to achieve by updating a product's "Inc. price" with another products price, but the reason that your query is taking so long is because of the way that you are joining.
mysql> create table t1 (product_id integer unsigned primary key, price decimal(10,2), price_inc_vat decimal(10,2), vat_rate integer unsigned);
mysql> insert into t1 values (1, 1.00, 1.20, 20), (2, 2.00, 2.40, 20), (3, 1.00, 1.30, 30);
mysql> select * from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20;
1 1.00 1.20 20 1 1.00 1.20 20
2 2.00 2.40 20 1 1.00 1.20 20
1 1.00 1.20 20 2 2.00 2.40 20
2 2.00 2.40 20 2 2.00 2.40 20
mysql> select s3.*, t3.*, round(s3.price * 1.3, 2) from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20;
1 1.00 1.20 20 1 1.00 1.20 20 1.30
2 2.00 2.40 20 1 1.00 1.20 20 2.60
1 1.00 1.20 20 2 2.00 2.40 20 1.30
2 2.00 2.40 20 2 2.00 2.40 20 2.60
mysql> update (t1 as s3, t1 as t3) set t3.price_inc_vat=round(s3.price*1.3, 2) where s3.vat_rate=20 and t3.vat_rate=20;
mysql> select * from (t1 as s3, t1 as t3) where s3.vat_rate=20 and t3.vat_rate=20;
1 1.00 1.30 20 1 1.00 1.30 20
2 2.00 1.30 20 1 1.00 1.30 20
1 1.00 1.30 20 2 2.00 1.30 20
2 2.00 1.30 20 2 2.00 1.30 20
If you are trying to set product prices based upon a previous price set then perhaps the following will help clarify things:
mysql> create table t1 (
product_id integer unsigned,
vat_rate integer unsigned,
price decimal(10,2),
price_inc_vat decimal(10,2),
primary key(product_id, vat_rate)
);
mysql> insert into t1 (product_id, price, price_inc_vat, vat_rate) values (1, 1.00, 1.20, 20), (2, 2.00, 2.40, 20), (1, 1.00, 0, 30), (2, 2.00, 0, 30);
mysql> create temporary table tmp1 like t1;
mysql> insert into tmp1 select * from t1;
mysql> select * from t1;
1 20 1.00 1.20
1 30 1.00 0.00
2 20 2.00 2.40
2 30 2.00 0.00
mysql> select * from tmp1;
1 20 1.00 1.20
1 30 1.00 0.00
2 20 2.00 2.40
2 30 2.00 0.00
mysql> update t1 left join tmp1 on t1.product_id=tmp1.product_id and t1.vat_rate > tmp1.vat_rate set t1.price_inc_vat = round(tmp1.price*(1+t1.vat_rate/100), 2) where tmp1.vat_rate = 20;
mysql> select * from t1;
1 20 1.00 1.20
1 30 1.00 1.30
2 20 2.00 2.40
2 30 2.00 2.60
So you have a field for price and another for price_inc_vat and yet another for vat_rate? Why not just store price and vat_rate and calculate price_inc_vat when it's needed?
Also, why are you multiplying by 1.3 when the VAT you're targeting appears to be 20%?
All these logical inconsistencies aside, couldn't you just do the following?
UPDATE products
SET price_inc_vat = ROUND(price * 1.3, 2)
WHERE vat_rate = 20
Or to update everything after a VAT change, disregarding the 10% markup:
UPDATE products
SET price_inc_vat = ROUND(price * (1+vat_rate/100), 2)
(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