I have following data in my table t1
unique_id myid price1 price2 price3 price4
1 100 20 30 40 50
2 200 12 24 48 90
3 100 15 20 30 25
4 300 100 200 300 400
5 400 10 10 20 40
6 100 5 6 7 8
7 200 1 2 3 4
Now i want data of particular myid and whose price3 is minimum
Expected output :
unique_id myid price1 price2 price3 price4
1 100 5 6 7 8
2 200 1 2 3 4
4 300 100 200 300 400
I have tried following query :
select t1.*,t2.txn_amount from table1 t1
left join table1 t2 on t2.ummyid = t1.myid
where t1.myid IN (100,200,300)
GROUP by t1.myid HAVING min(price3)
But it is not working as expected.
If your db version not allow over partition` you could use a join with the subquery for min values
select * from table1 t1
inner join (
select myid, min(price3) min_price
from table1
group by myid
) t on t.myid = t1.myid
and t.min_price = t1.price
And for you case
select t1.*
, t2.txn_amount
from table1 t1
inner join (
select myid, min(price3) min_price
from table1
group by myid
) t on t.myid = t1.myid
and t.min_price = t1.price
left join table1 t2 on t2.ummyid = t1.myid
where t1.myid IN (100,200,300)
GROUP by t1.myid
If you are using MySQL 8.0 then following solution should work using window function row_number. Here is the demo.
select
unique_id,
myid,
price1,
price2,
price3,
price4
from
(
select
unique_id,
myid,
price1,
price2,
price3,
price4,
row_number() over (partition by myid order by price3) as rn
from myTable
) subq
where rn = 1
You have to use analytic functions, please use below query
select unique_id, myid, price1, price2, price3, price4 from
(slect unique_id, myid, price1, price2, price3, price4,
row_number() over(partition by myid order by price3) as rnk from table) qry
where rnk = 1;
I think that this solution should work:
Select distinct myid,
min(price3)
from t1 group by myid
Related
I am trying to retrieve rows with same Volume value or with only 1 Volume, but could not come up with a SQL logic.
Data:
ID
Volume
A
100
A
100
B
101
B
102
B
103
B
104
C
400
Required Output:
ID
Volume
A
100
A
100
C
400
This one is achievable using a subquery.
select * from test where col1 in (
select t.col1
from(
select col1, col2,
dense_rank() over (partition by col1 order by col2) as dr
from test) t
group by t.col1
having sum(case when t.dr = 1 then 0 else t.dr end) = 0)
Try this dbfiddle.
This can be done on a more easy way:
select t1.id,
t1.volume
from tbl t1
inner join (select id
from tbl
group by id
having count(distinct volume) = 1
) as t2 on t1.id=t2.id;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=92bc234e631a1106b0e322bc4954d696
having count(distinct volume) = 1 will return only the id that have the same volume , including the id with just one volume.
I'd naturally be inclined towards Ergest Basha's pattern.
It can also be expressed using NOT EXISTS()
SELECT
t.*
FROM
tbl AS t
WHERE
NOT EXISTS (
SELECT *
FROM tbl
WHERE id = t.id
AND volume <> t.volume
)
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=243c42008f527391514d1ad124730587
I'm trying to do a request with a group BY.
Here is an exemple of my table ticket :
id DtSell Price Qt
1 01-01-2017 3.00 1
1 02-01-2017 2.00 3
2 01-01-2017 5.00 5
2 02-01-2017 8.00 2
And my request :
SELECT id, Price, sum(Qt) FROM ticket
GROUP BY id;
but unfortunately, the price returned is not necessarily the right one; I would like to have the last price according to DtSell like that :
id Price sum(Qt)
1 2.00 4
2 8.00 7
But i didn't find how to do it.
Can you help me ?
Thank you in advance!!
You might need a sub query,try below:
SELECT
t1.id,
(SELECT t2.price FROM ticket t2 WHERE t2.id=t1.id
ORDER BY t2.DtSell DESC LIMIT 1 ) AS price,
SUM(t1.Qt)
FROM ticket t1 GROUP BY t1.id;
You can do this with a group_concat()/substring_index() trick:
SELECT id, Price, SUM(Qt)
SUBSTRING_INDEX(GROUP_CONCAT(price ORDER BY dtsell DESC), ',' 1) as last_price
FROM ticket
GROUP BY id;
Two notes:
This is subject to internal limits on the length of the intermediate string used for GROUP_CONAT() (a limit that can easily be changed).
It changes the type of price to a string.
Try this query.
SELECT id, Price, sum(Qt) FROM ticket
GROUP BY id,Price
Your Output;
id Price sum(Qt)
1 3.00 4
2 8.00 7
You can select all rows from ticket grouped by id ( to sum quantity), then join to the rows which have the max dtsell for each id group( to select the price).
http://sqlfiddle.com/#!9/574cb9/8
SELECT t.id
, t3.price
, SUM(t.Qt)
FROM ticket t
JOIN ( SELECT t1.id
, t1.price
FROM ticket t1
JOIN ( SELECT id
, MAX(dtsell) dtsell
FROM ticket
GROUP BY id ) t2
ON t1.id = t2.id
AND t1.dtsell = t2.dtsell ) t3
ON t3.id = t.id
GROUP BY t.id;
You can do it like this:
declare #t table (id int, dtsell date, price numeric(18,2),qt int)
insert into #t
values
(1 ,'01-01-2017', 3.00 , 1),
(1 ,'02-01-2017', 2.00 , 3),
(2 ,'01-01-2017', 5.00 , 5),
(2 ,'02-01-2017', 8.00 , 2)
select x.id,price,z.Qt from (
select id,price,dtsell,row_number() over(partition by id order by dtsell desc ) as rn from #t
)x
inner join (select SUM(qt) as Qt,ID from #t group by id ) z on x.id = z.id
where rn = 1
I've got a following query:
SELECT BusID, count(BusID) as 'NoofConnections'
FROM mytable
Group By BusID
Order by BusID
And I get following table:
BusID NoofConnections
=========================
1 6
2 6
4 3
5 2
3 1
And I will to select just maximum from NoofConnections, so in this case LIMIT 1 doesn't work, so we have two equal values. Like this:
BusID NoofConnections
=========================
1 6
2 6
Any ideeas?
You can use a subquery:
SELECT BusID, COUNT(*) as NoofConnections
FROM mytable
GROUP BY BusID
HAVING COUNT(*) = (SELECT COUNT(*)
FROM mytable
GROUP BY BusID
ORDER BY COUNT(*) DESC
LIMIT 1
)
ORDER BY BusID;
You could use a subselect
select * from (
SELECT BusID, count(BusID) as NoofConnections
FROM mytable
Group By BusID
) t1
where t1.NoofConnections = ( select max(NoofConnections)
from (
SELECT BusID, count(BusID) as NoofConnections
FROM mytable
Group By BusID
)
I want a sql query to get the row of products with a minimum price and get all of other fields of two table.
Consider this table:
T1: T2:
id Title id pcount price t1_id(foreign key)
1 x 1 3 3000 2
2 y 2 8 2500 2
3 z 3 4 1200 1
4 6 1000 1
5 9 4000 3
How can I select the below columns that have the minimum value in the price column, grouped by Title and get below fields? Like this:
id Title pcount price t1_id
1 y 8 2500 2
2 x 6 1000 1
3 z 9 4000 3
For Sql Server you can use OUTER APPLY:
select * from t1
outer apply(select top 1 * from t2 where t1_id = t1.id order by price) oa
Try like this,
SELECT t1.*,
t3.*
FROM T1 t1
CROSS apply (SELECT Min(price) AS price
FROM T2
WHERE t1_id = t1.tableid)t2
LEFT OUTER JOIN t2 t3
ON t3.t1_id = t1.tableid
AND t3.price = t2.price
select *
from t1
left join
(select pcount, price, t2.t1_id
from t2
join
(select t1_id, min(price) pmin
from t2
group by t1_id
) tt
where t2.t1_id = tt.t1_id and price = pmin
) tt1
on t1.id = tt1.t1_id
result
id Title pcount price t1_id
1 x 6 1000 1
2 y 8 2500 2
3 z 9 4000 3
Also check this :
declare #t1 table(id int , title varchar(50))
declare #t2 table(id int , pcount int, price int, t1_id int)
insert into #t1 values (1, 'x' ), (2,'y'), (3,'z')
insert into #t2 values (1, 3, 3000, 2 ), (2, 8, 2500, 2 ),(3, 4, 1200, 1 ),(4, 6, 1000, 1),(5, 9, 4000, 3)
;with cte
as(
select * from (select * , ROW_NUMBER() OVER (PARTITION BY t1_id ORDER BY t1_id asc, price asc ) AS sequence_id from #t2 ) a where sequence_id = 1
)
select t1.*, cte.pcount ,cte.price from #t1 t1 join cte on t1.id = cte.t1_id
--or understand more
;with cte as
(
select t1_id, min(price) price
from #t2 group by t1_id
)
, cte1 as
(
select t1.*,t2.pcount, cte.* from #t1 t1
left outer join #t2 t2 on t1.id = t2.t1_id
left outer join cte cte on t1.id = cte.t1_id and (t2.t1_id =cte.t1_id and t2.price = cte.price)
)
select * from cte1 where t1_id is not null
I have below two query's SUM the values
Query1:*
SELECT SUM(price) FROM TABLE1 WHERE acc_id = '555'
Query2:
SELECT SUM(price) FROM TABLE2 WHERE account = '555' && active='1'
I try to combine this two query but give wrong sum result , for example if query1 sum is: -86500 and Query2 sum is: 76000 , RESULT must be -10500 but result shown with a number like -486000
I'm trying like this, but i'm not getting expected result.
SELECT SUM(t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
Table image :
Due to join the number of records get duplicated and you get a higher value for sum
try this
SELECT sum(prc)
FROM (
SELECT SUM(price) prc FROM TABLE1 WHERE acc_id = '555'
union all
SELECT SUM(price) prc FROM TABLE2 WHERE account = '555' && active='1'
) a
Try this
SELECT SUM(C.TOTAL) AS TOTAL_CREDIT FROM (SELECT SUM(A.price) AS TOTAL FROM TABLE1 A WHERE A.acc_id = '555'
UNION ALL
SELECT SUM(B.price) AS TOTAL FROM TABLE2 B WHERE B.account = '555' && B.active='1') C;
try that
SELECT (SUM(t1.price) + SUM(t2.price)) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
try this
SELECT (t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
EDIT:
here what you looking for i think
SELECT (SUM(t1.price)+SUM(t2.price) )/2 AS TotalCredit
FROM Table1 AS t1, Table2 AS t2
WHERE t1.`acc_id` = '555' && t2.`account`='555' && t2.`active`='1'
DEMO FIDDLE HERE
How about this:
SELECT SUM(a)
FROM
(SELECT SUM(price) AS a
FROM TABLE1
WHERE acc_id = '555'
UNION ALL
SELECT SUM(price) AS a
FROM TABLE2
WHERE account = '555' && active='1')
Join could be better. :) Would be even better if you could have showed us the table schema. Here is a solution based on some assumed sample data.
Sample data:
Table1:
ID PRICE
11 200
55 300
33 200
44 100
55 500
Table2:
ID PRICE ACTIVE
1 200 0
2 300 1
55 200 0
55 100 1
55 400 1
SQLFIDDLE DEMO
Query:
select sum(t.price) + x.tb2credit
from tb1 as t
inner join
(SELECT id, SUM(price) AS Tb2Credit
FROM tb2
WHERE id = 55
and `Active`=1) x
on t.id = x.id
Results:
SUM(T.PRICE) + X.TB2CREDIT
1300