I wonder if there is better way to get max from column c1 and then max from column c2 for selected rows than
SELECT MAX(c1) , MAX(c2) FROM t GROUP BY c1 HAVING c1 = MAX(c1)
SELECT Max(t2.c1) as C1,
Max(t1.c2) as C2
FROM t t1
INNER JOIN (SELECT Max(c1) AS C1
FROM t) t2
ON t1.c1 = t2.c1
I'm not quite sure whether you want all distinct c1's or just the maximum.
If you want all c1's:
SELECT c1, MAX(c2) FROM t GROUP BY c1;
If you want only the maximum c1:
SELECT c1, c2 FROM t ORDER BY c1 DESC, c2 DESC LIMIT 1;
Just read your comment on hkutluay's response, this might be what you want:
select MAX(colName)
from
(select col1 as colName
from tableName
union
select col2
from tableName) subqueryName
I know mysql does not have CTE's; but because the question is also marked SQL, and the CTE is a reasonable clean solution for the subquery, for completeness here is a CTE-version:
WITH zmax AS (
SELECT MAX(v1) AS v1
FROM ztable
)
SELECT zt.v1
, MAX(zt.v2) AS v2
FROM ztable zt
JOIN zmax mx ON zt.v1 = mx.v1
GROUP BY zt.v1
;
Related
I'm new to SQL. I wanted to know, How to check the entries which are not repeated for the other entries.
Better I give one example.
column1
column2
a
1
a
2
a
3
a
4
b
1
b
2
b
3
b
4
c
1
c
2
c
3
I want output as
column1
column2
c
4
because c does not have 4, but the other values a and b do.
You are looking for missing entries. For these to find, you must know which entries you expect. You expect to find one entry for all combinations of column1 values and column2 values. You get these with a cross join.
Now you want to select all these pairs except for those already in the table. You can use EXCEPT for this or NOT EXISTS or NOT IN. You haven't told us your DBMS, so I don't know what it features.
EXCEPT
select c1.column1, c2.column2
from (select distinct column1 from mytable) c1
cross join (select distinct column2 from mytable) c2
except
select column1, column2
from mytable
order by column1, column2;
NOT IN
select c1.column1, c2.column2
from (select distinct column1 from mytable) c1
cross join (select distinct column2 from mytable) c2
where (c1.column1, c2.column2) not in
(
select column1, column2 from mytable
)
order by c1.column1, c2.column2;
NOT EXISTS
select c1.column1, c2.column2
from (select distinct column1 from mytable) c1
cross join (select distinct column2 from mytable) c2
where not exists
(
select null
from mytable
where mytable.column1 = c1.column1
and mytable.column2 = c2.column2
)
order by c1.column1, c2.column2;
Provided columns are not nullable
select distinct column1
from mytable t1
where exists (
select 1
from mytable t2
where t2.column2 <> t1.column2
and t1.column1 not in (
select t3.column1
from mytable t3
where t3.column2 = t2.column2
)
)
I have MySQL table in the following format
id c1 c2
---------------------
id1 c1v1 c2v1 -> keep id1 even it has duplicates
id1 c1v1 c2v1
id2 c1v2 c2v2 -> filter out id2 because it has various c1, c2
id2 c1v2 c2v3
id3 c1v3 c2v4
....
Expected output:
id c1 c2
---------------------
id1 c1v1 c2v1
id3 c1v3 c2v4
....
I want to retain only the records where id only has a unique c1, c2 value.
I do have a solution but it requires full table scan twice including a join which is very inefficient and I am wondering if there is a better way of doing it.
select
distinct id, c1, c2
from table
inner join
(select
id,
count(distinct c1, c2) as counts
from table
group by id
having counts = 1) tmp
on table.id = tmp.id
How about just using group by:
select id, min(c1) as c1, min(c2) as c2
from t
group by id
having min(c1) = max(c1) and min(c2) = max(c2)
If you want all the original records, then use not exists
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and
(t2.c1 <> t.c1 or t2.c2 <> t.c2)
);
You can do this with a self LEFT JOIN on different c1/c2 values, keeping only those rows where there is no matching row (i.e. different values for the same id) in the second table:
SELECT DISTINCT t1.id, t1.c1, t1.c2
FROM test t1
LEFT JOIN test t2 ON t2.id = t1.id AND (t2.c1 != t1.c1 OR t2.c2 != t1.c2)
WHERE t2.id IS NULL
Output:
id c1 c2
id1 c1v1 c2v1
id3 c1v3 c2v4
Demo on dbfiddle
I have a complex mysql query where one of the Select fields is Min(value). Since all the 'values' are unique, is there also a way to get found min value's row id along?
In other words if we simplify the query to this question, it is like this:
SELECT t1.name, MIN(t2.value) AS minval
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY id_user
How can i now know which t2.id was chosen for lowest t2.value for particular user? Thank you!
Use ROW_NUMBER() to find the first value of each id_user
You can replace * with the fields you need
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY t2.id_user ORDER BY t2.value) as rnk
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
) as X
WHERE X.rnk = 1
Maybe this simple, dont know how complex your statement is:
SELECT name,value,id
FROM(
SELECT t1.name,t2.value,t2.id
FROM table1 t1
LEFT JOIN table2 t2
ON t2.id_user = t1.id
GROUP BY t2.id,id_user
ORDER BY t1.name,t2.id asc) as test
GROUP BY name;
How can I write a query to give the results of three tables such that there's only one result per "line"?
The tables are:
T1 (ID, name, IP)
T2 (ID, date_joined)
T3 (ID, address, date_modified)
The relations are:
T1-T2 1:1, T1-T3 1:M - there can be many address rows per ID in T3.
What I want is a listing of all users with the fields above, but IF they have an address, I only want to record ONE (bonus would be if it is the latest one based on T3.date_modified).
So I should end up with exactly the number of records in T1 (happens to be equal to T2 in this case) and no more.
I tried:
select t.ID, t.name, t.IP, tt.ID, tt.date_joined, ttt.ID, ttt.address
from T1 t JOIN T2 tt ON (t.ID = tt.ID) JOIN T3 ttt ON (t.ID = ttt.ID)
And every sensible combination of LEFT, RIGHT, INNER, etc joins I could think of! I keep getting multiple duplicate because of T3
This query should work:
select
t1.ID, t1.name, t1.IP, t2.date_joined, t3x.address
from t1
join t2 on t1.ID = t2.id
left join (
select t3.*
from t3
join (
select id, max(date_modified) max_date
from t3
group by id
) max_t3 on t3.id = max_t3.id and t3.date_modified = max_t3.max_date
) t3x on t1.ID = t3x.id
First you do the normal join between t1 and t2 and then you left join with a derived table (t3x) that is the set of t3 rows having the latest date.
So T2 is actually not relevant here. You just need a way to join from T1 to T3 in a way that gets you at most one T3 row per T1 row.
One way of doing this would be:
select
T1.*,
(select address from T3 where T3.ID=T1.ID order by date_modified desc limit 1)
from T1;
This won't likely be very efficient, being a correlated subquery, but you may not care depending on the size of your dataset.
It's also only good for getting one column from T3, so if you had Address, City, and State, you'd have to figure out something else.
You can use sub query with Top 1 so that u get only one result from T3
here is a sample sql
select * into #T1 from(
select 1 ID
union select 2
union select 3) A
select * into #T2 from(
select 1 ID
union select 2
union select 3) A
select * into #T3 from(
select 1 ID, 'ABC' Address, getDate() dateModified
union select 1, 'DEF', getDate()
union select 3, 'GHI', getDate()) A
select *, (select top 1 Address from #T3 T3 where T3.ID= T1.ID order by datemodified desc) from #T1 T1
inner join #T2 T2 on T1.ID = T2.ID
Bonus :- you can also add order by dateModified desc to get the latest address
Table column headers: n,t1,t2
entries :
1 A B
2 A C
3 B C
4 D E
5 B A
How do I count total number of rows each letter appears in t1 MINUS the number of rows they appear in t2 ? I need to do something like following 2 lines in 1 query :
select count(*) as val,t1 from table group by t1
select count(*) as val,t2 from table group by t2
Thanks,
Martin
Here is one way:
select t1, max(t1cnt) - max(t2cnt) as diff
from ((select t1, count(*) as t1cnt, 0 as t2cnt
from t
group by t1
) union all
(select t2, 0 as t1cnt, count(*) as t2cnt
from t
group by t2
)
) t
group by t1
Using the union all ensures that you get all possible values from both columns, even values that only appear in one column.
You can use the following query to get the result. This query first gets a list of all the distinct t1 and t2 values (this is the UNION query). Once you have the list of these values, then you can use a LEFT JOIN to the original queries that you posted:
select d.col, coalesce(totT1, 0) - coalesce(totT2, 0) Total
from
(
select t1 col
from entries
union
select t2 col
from entries
) d
left join
(
select count(*) totT1, t1
from entries
group by t1
) d1
on d.col = d1.t1
left join
(
select count(*) totT2, t2
from entries
group by t2
) d2
on d.col = d2.t2;
See SQL Fiddle with Demo