I really don't know how to title this problem properly.
Heres the table structure:
ID | CLIENT_ID | …
ID is primary and auto increment. CLIENT_ID on the other hand can occur multiple times.
What i want is to fetch the rows by CLIENT_ID with highest ID ... Heres a example
ID | CLIENT_ID
1 | 1
2 | 1
3 | 2
4 | 3
5 | 2
So here CLIENT_ID 1 and 2 occurs multiple times (because there is a newer version).
After the query i want the following IDs in the results: 2,4,5 (Because the highest ID in rows with CLIENT_ID 1 is the row with ID 2 and so on)
If you need all the columns you can use a select in
select * from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
but if you need only the id
select id from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
or more simple
select max(id)
from my_table
group by client_id;
SELECT * FROM table GROUP BY client_id HAVING max(id)
this should be more efficient than a sub select
Related
I am using mariadb and I have a table called links:
id | product_id | last_change
------------------------------
1 1 xxx
2 2 xxx
3 5 xxx
4 5 xxx
I want to find every object (3, 4 in this example) that occures more than once. Following this answer I tried:
SELECT product_id, COUNT(*) from links HAVING COUNT(*) > 1
But this results in the (adapted to this example) first row being shown and the total number of product_id occurrences:
product_id | COUNT(*)
---------------------
1 4
I wanted to achieve a list of all items occuring more than once:
id | product_id | last_change
------------------------------
3 5 xxx
4 5 xxx
An aggregation function without GROUP BY always results in only one row result as it aggregates all rows
So use a GROUP BY
SELECT product_id, COUNT(*) from links GROUP BY product_id HAVING COUNT(*) > 1
To see all entry with the count of the product_id , you can do following
SELECT l1.product_id , last_change , Count_
FROM links l1
JOIN (SELECT product_id, COUNT(*) as Count_ from links GROUP BY product_id HAVING COUNT(*) > 1) l2
ON l1.product_id = l2.product_id
Try below statement
select id, product_id, count(product_id)
from links
group by (product_id)
having count(product_id)> 1;
I'm using MySql. I have a table with 2 column id (Primary Key) and id_of (Foreign Key). Number of id can have the same id_of. I want to get all the id and get the count of rows having the id_of related to the id. How to make this sql query/queries? So far I could only get this:
SELECT id, (SELECT COUNT(id_of) FROM test_table) AS count FROM test_table;
database's table:
id | id_of
----------------
abasb | 2131233
hdafd | 2131233
fajdf | 3546541
pogad | 3546541
afdaj | 2131233
fafda | 8661565
the results I want:
id | count
----------------
abasb | 3
hdafd | 3
fajdf | 2
pogad | 2
afdaj | 3
fafda | 1
just need a bit of correction your query
SELECT id,
(SELECT COUNT(*) FROM test_table t2 where t2.id_of=t1.id_of) AS count
FROM test_table t1
You may try this...
; with cte as ( select distinct id_of, count(*) as Coun from testtable )
select t.id , c.coun from testtable as t inner join cte as c on t.id_of=c.id_of
You can try this:
select id , count(*) over (partition by id_of) id_of from Yourtable
You could use ajoin on subqiery for count
select a.id, t.count_of_id_of
from test_table a
inner join (
select id_of, count(*) count_of_id_of
from test_table
group by id_of
) t on t.id_of= a.id_of
suppose i have the table student
roll name
1 Nitin
2 Rohit
3 Nitin
then i want to find non repeated names. so it should return me only rohit. got it. thank you waiting for the answer
1) count the number of occurrences in the sub query t1 2) inner join to the sub query
drop table if exists t;
create table t (id int,name varchar(10));
insert into t values (1,'nitin'),(2,'rohan'),(3,'nitin');
select t.*
from t
join
(
select t.name
from t
group by t.name having count(*) = 1
) t1 on t1.name = t.name;
+------+-------+
| id | name |
+------+-------+
| 2 | rohan |
+------+-------+
1 row in set (0.02 sec)
I have different tables (per region) with some columns that are the same. Now I need to have a count for each value that is placed in one column over multiple tables. I'm trying to get a sum for this so I don't have to use 4 separate queries and outputs. Furthermore the values are matched with a lookup table
Region table(s) - Table1:
id | Column1 |
---------|----------|
1 | 1
2 | 2
3 | 3
etc
Lookup table
id | Description |
---------|-------------|
1 | Description1
2 | Description2
3 | Description3
The query I'm using to get the count from 1 of the tables is :
SELECT Description, Count(*) as Number from Table1, LookupTable
WHERE Column1 = LookupTable.id GROUP BY Column1 ORDER BY Number Desc
The output is
Description | Number
---------------|--------
Description1 | Number
Description2 | Number
Etc.
Any idea on how to sum up the counts for each Description/value of Column1 from 4 tables that generates the output as displayed above (but then with the sum value for each description)?
It's not clear but I guess you can use:
select LookupTable.id,LookupTable.Description, SUM(Cnt) as Number
from LookupTable
JOIN
(
SELECT Column1 as CId, count(*) as Cnt from Table1 group by Column1
union all
SELECT Column2 as CId, count(*) as Cnt from Table2 group by Column2
union all
SELECT Column3 as CId, count(*) as Cnt from Table3 group by Column3
union all
SELECT Column4 as CId, count(*) as Cnt from Table4 group by Column4
) T1 on LookupTable.id =T1.Cid
GROUP BY LookupTable.id,LookupTable.Description
ORDER BY Number Desc
Use this query:
SELECT LookupTable.Description, Count(*) as Number
FROM Table1, LookupTable
WHERE Table1.Column1 = LookupTable.id
GROUP BY Table1.Column1;
You have leave the name of table or its alias to call the column.
OK, Here is what my table looks like
------------------------------------------------
Textid type
-----------------------------------------------
1 a
2 b
1 a
1 c
2 c
1 a
3 a
------------------------------------------------
Now, I need a query that can give me this output...
-------------------------------------
Distinct(textid) | rand(type) |
--------------------------------------
1 a
2 c
3 a
--------------------------------------
rand(type) gives me number.... Do I need to pass a different records inside rand() like random(SELECT type FROM mytable)
UPDATE
I am trying to get a distinct id from the table and random field(type) associated with that distinct id
SELECT textid,
(
SELECT type
FROM mytable mi
WHERE mi.textid = md.textid
ORDER BY
RAND()
LIMIT 1
)
FROM (
SELECT DISTINCT textid
FROM mytable
) md
Create a composite index on mytable (textid, type) for this to work fast:
CREATE INDEX ix_mytable_textid_type ON mytable (textid, type)