how to exclude first and last row if group it on particular id - mysql

I have sample table with data like this
id uniqueid values
1 6 0
2 6 1
3 6 2
4 6 0
5 6 1
I want result like this
id uniqueid values
2 6 1
3 6 2
4 6 0
I tried like this
select id,uniqueid,values
FROM t1
WHERE
id not in(SELECT concat(MAX(message_id_pk),',',min(message_id_pk)) FROM t1
where uniqueid=6)
and `uniqueid`=6
GROUP BY uniqueid
but its not working

You can achieve the desired results by doing self join, Inner query will get the the max and min ids for per group and outer query will filter out the results by using minid and maxid
select a.*
from demo a
join (
select `uniqueid`,min(id) minid, max(id) maxid
from demo
where uniqueid=6
group by `uniqueid`
) b using(`uniqueid`)
where a.id > b.minid and a.id < b.maxid /* a.id <> b.minid and a.id <> b.maxid */
Demo

Also you can do it by using 2 sub-queries with EXISTS to exclude the min and max id of each uniqueid.
Query
select `id`, `uniqueid`, `values`
from `your_table_name` t1
where exists (
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` > t1.`id`
)
and exists(
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` < t1.`id`
);
Here is a sql fiddle demo

Try this -
SELECT id, uniqueid, values
FROM YOUR_TABLE
WHERE id NOT IN (MIN(id), MAX(id));

Related

JOIN, GROUP BY, SUM Issue Mysql

Assuming I have this table
tableA
ID value
1 5
1 5
3 10
2 4
2 2
1 2
tableB
ID Name
1 apple
2 carrot
3 banana
If the expected max value of apple is 10, carrot is 5, and banana is 15 the output table would be
table output
ID Name value
1 apple 12
2 carrot 6
what SQL statement I need to solve this?
what I have done so far:
SELECT a,ID, b.name , sum(a.valueSUM) AS value FROM tableA a
INNER JOIN tableB b
ON a.id = b.id
GROUP BY id
what options i need on the WHERE clause to pull this off?
The inner subquery groups them normally and then the main query is what deals with limiting the results.
SELECT * FROM
(select
b.id,
b.name as name,
SUM(a.value) as the_sum
from tableA a inner join tableB b
on a.Id = b.id
group by b.name, b.id
) y
where (name = 'apple' and the_sum >= 10) OR
(name = 'banana' and the_sum >= 15) OR
(name = 'carrot' and the_sum >= 5)
It seems your sample data has changed, please try this. I thought the ID doesnt have to follow tableA/tableB's id and the id is auto-generated as per the results.
Would be nice if you have another table that sets the threshold per name
Assuming threshold can be specified in tableB (makes sense):
SELECT a.ID, b.name, sum(a.value) AS value
FROM tableA a
INNER JOIN tableB b
ON a.id = b.id
GROUP BY a.ID, b.name, b.Threshold
HAVING sum(a.value) > b.Threshold;
Demo: http://rextester.com/ICOQF10295
SELECT TableB.id, TableB.Name, MAX(TableA.value) AS Value
FROM TableA INNER JOIN TableB ON
TableA.id = TableB.id
GROUP BY TableB.id, TableB.Name
Instead of SUM, use MAX aggregate function
This works in SQL Server
--Existing tables
create table #tableA (ID int, value int)
create table #tableB (ID int, Name varchar(30))
insert into #tableA
select 1 , 5 union all
select 1 , 5 union all
select 3 , 10 union all
select 2 , 4 union all
select 2 , 2 union all
select 1 , 2
insert into #tableB
select 1 , 'apple' union all
select 2 , 'carrot' union all
select 3 , 'banana'
--Create new temporary table #tableC
create table #tableC (ID int, MAXvalue int)
insert into #tableC
select 1 , 10 union all
select 2 , 5 union all
select 3 , 15
select c.ID,b.Name, a.value from #tableC c
inner join #tableB b on b.ID = c.ID
inner join (
select ID,SUM(value) as value from #tableA
group by ID
) a on a.ID = c.ID
where a.value >= c.MAXvalue
drop table #tableA
drop table #tableB
drop table #tableC

I need help to find solution for this query in MySQL?

I have MySQL table as follow:
id p_id c_id
1 11 1
2 11 2
3 11 3
4 12 1
5 12 3
6 13 1
7 13 2
I need a query that when c_id is 1 and 2, it should return 11 and 13 of p_id.
I have tried the following query:
SELECT DISTINCT p_id FROM `Table Name` where c_id in (1,2)
which returns: 11, 12, 13.
but I only need it to return: 11 , 13.
You can write your query as:
SELECT DISTINCT a.p_id
FROM table_name AS a
JOIN table_name AS b ON a.p_id=b.p_id
WHERE a.c_id ='1' AND b.c_id ='2';
It's a self-join on the table itself
SELECT DISTINCT a.p_id
FROM table_name AS a
JOIN table_name AS b ON a.p_id=b.p_id
WHERE a.c_id ='1' AND b.c_id ='2';
it worked fo me
Your query is checking for p_ids related to c_is 1 or 2. But what you want is 1 and 2. You can write this like this:
SELECT DISTINCT t1.p_id
FROM table_name as t1
JOIN table_name as t2
ON t1.id = t2.id
WHERE t1.c_id = 1 AND t2.c_id = 2;
SELECT x.id
FROM my_table x
WHERE other_column IN('a','b')
GROUP
BY x.id
HAVING COUNT(*) = 2;

Select rows from a table based on max value in different table

I'm looking for a MySQL query to extract values like in the following example:
TABLE1:
ID name
25 ab
24 abc
23 abcd
22 abcde
21 abcdef
TABLE2:
ID ID_TABLE1 total
1 25 0
2 25 1
3 25 2
4 25 3
5 23 1
6 22 0
7 22 1
8 21 0
9 21 2
10 24 10
11 24 7
I want to return all TABLE1 rows where max value of total column (in TABLE2) is < 3.
So the results should be:
ID name
23 abcd
22 abcde
21 abcdef
I tried this:
SELECT t1.*
FROM TABLE1 t1
INNER JOIN (
SELECT MAX( total ) AS max_total, ID_TABLE1
FROM TABLE2
GROUP BY total, ID_TABLE1
) t2
ON t1.ID = t2.ID_TABLE1
WHERE t2.max_total < 3
but it's not the result I want.
Try this:
SELECT t1.ID, t1.name
FROM TABLE1 t1
INNER JOIN (SELECT ID_TABLE1, MAX(total) AS max_total
FROM TABLE2
GROUP BY ID_TABLE1
) t2 ON t1.ID = t2.ID_TABLE1
WHERE t2.max_total < 3;
Your inner query groups the results by id_table and by total. Since the maximum of total per total is the value itself, it makes the inner query somewhat meaningless. Just remove the total from the group by clause and you should be OK:
SELECT t1.*
FROM TABLE1 t1
INNER JOIN (
SELECT MAX( total ) AS max_total, ID_TABLE1
FROM TABLE2
GROUP BY ID_TABLE1
) t2
ON t1.ID = t2.ID_TABLE1
WHERE t2.max_total < 3
SELECT t1.*
FROM TABLE1 t1
INNER JOIN (
SELECT MAX( total ) AS max_total, ID_TABLE1
FROM TABLE2
GROUP BY ID_TABLE1
having t2.max_total < 3
) t2
ON t1.ID = t2.ID_TABLE1
Here is a way to do using left join without using any subquery and group by clauses.
select t1.* from table1 t1
join table2 t2
on t1.id = t2.id_table1
left join table2 t3 on
t2.id_table1 = t3.id_table1
and t2.total < t3.total
where t3.id is null
and t2.total < 3
Another way is
select t1.* from table1 t1
join table2 t2 on t1.id = t2.id_table1
where not exists(
select 1 from table2 t3
where t2.id_table1 = t3.id_table1
and t2.total < t3.total
)
and t2.total < 3;
SELECT t1.*
FROM TABLE1 t1
INNER JOIN (
SELECT MAX( total ) AS max_total, ID_TABLE1
FROM TABLE2
where total > 3 GROUP BY total, ID_TABLE1
) t2
ON t1.ID != t2.ID_TABLE1
There's a simpler way without using GROUP or MAX:
SELECT * FROM table1
WHERE id NOT IN (
SELECT id_table1 FROM table2 WHERE total >= 3
);
The subquery selects all rows in table2 that have a total >= 3. Then we select those rows from table1 that are not in the subquery result.

Mysql update max value with group by

I have a simple table that has many rows already:
id grade ...
1 1 ...
2 2 ...
3 2 ...
4 1 ...
5 1 ...
Now i want to add a column "relative_order", which is the order in that grade. So the table becomes:
id grade ... relative_order
1 1 ... 1
2 2 ... 1
3 2 ... 2
4 1 ... 2
5 1 ... 3
After i add the column, all the relative_order becomes 0 first. How can i fill the relative_order column using update syntax?
I tried using inner join, but failed:
UPDATE table AS i
INNER JOIN(
SELECT max(relative_order) as mOrder,grade
FROM table
GROUP BY grade
) AS j
ON i.grade = j.grade
SET i.relative_order = j.mOrder + 1
You could use this SELECT query that will return the relative_order that you need:
SELECT
t1.id,
t1.grade,
COUNT(t2.id) relative_order
FROM
yourtable t1 INNER JOIN yourtable t2
ON t1.grade=t2.grade AND t1.id>=t2.id
GROUP BY
t1.id,
t1.grade
or if you want to update the value, you can join your table with the previous query, like this:
UPDATE
yourtable INNER JOIN (
SELECT
t1.id,
t1.grade,
COUNT(t2.id) relative_order
FROM
yourtable t1 INNER JOIN yourtable t2
ON t1.grade=t2.grade AND t1.id>=t2.id
GROUP BY
t1.id,
t1.grade) seq
ON yourtable.id=seq.id AND yourtable.grade=seq.grade
SET
yourtable.relative_order = seq.relative_order
Please see fiddle here.

MySQL count column values equal to another column

I have a MySQL table like this:
Id Id_1 Id_2
1 0 0
2 0 0
3 1 0
4 1 0
5 0 0
6 0 1
7 2 1
8 0 2
where Id_1 and Id_2 could be equal to Id except for itself or equal to 0
and I would like to get this result by using the most efficient query:
id COUNT(Id_1) COUNT(Id_2)
1 2 2
2 1 1
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
Thanks!
Your explanation is not very clear, but based on your sample data and desired output you can accomplish this with two sub-queries outer joined to the base table, like this:
select a.id,
coalesce(b.id_1_count,0),
coalesce(c.id_2_count,0)
from so15273831 a
left outer join (
select id_1, count(*) as id_1_count
from so15273831
group by id_1
) b on b.id_1 = a.id
left outer join (
select id_2, count(*) as id_2_count
from so15273831
group by id_2
) c on c.id_2 = a.id
order by a.id;
SQL Fiddle here: http://sqlfiddle.com/#!2/71b67/1
You can use a CASE statement with subQueries to get your desired results
SELECT ID, SUM(id1.ids), SUM(id2.ids)
FROM mytable
LEFT OUTER JOIN
(SELECT id_1, count(id) ids
FROM my table
GROUP BY id_1) AS id1 ON mytable.ID = id1.id_1
LEFT OUTER JOIN
(SELECT id_2, count(id) ids
FROM my table
GROUP BY id_2) AS id2 AS id2 ON mytable.ID = id2.id_2
GROUP BY ID
ORDER BY ID
Select t1.id1 id,
(Select COUNT(*) From tableName t2 Where t1.id1=t2.id2) COUNT(Id_1),
(Select COUNT(*) From tableName t2 Where t1.id1=t2.id3) COUNT(Id_2)
From tableName t1
Here is the alternate with example:
declare #aa table
(
id1 int,
id2 int,
id3 int
)
insert into #aa
Select
1,0,0
Union All Select
2,0,0
Union All Select
3,1,0
Union All Select
4,1,0
Union All Select
5,0,0
Union All Select
6,0,1
Union All Select
7,2,1
Union All Select
8,0,2
Select t1.id1,IsNull(t2.CountId,0) as [Count(Id_2)],IsNull(t3.CountId,0) as [Count(Id_2)] From #aa t1
Left Join
(
Select COUNT(*) CountId,t2.id2 From #aa t2 Group By t2.id2
)t2
On t1.id1=t2.id2
Left Join
(
Select COUNT(*) CountId,t2.id3 From #aa t2 Group By t2.id3
)t3 On t1.id1=t3.id3