MySQL -- Making it all one query - mysql

I am trying to see if I can accomplish the following situation in one query:
I have a table with multiple columns, however, only two are important: version and groupId.
Many rows can share the same groupId value, the version column is a number that needs
to be sorted.
Given two groupId values, A and B, I would like to return two rows in the end. I want to find the most recent version number for each group A and B.
Thanks for your help. Sorry if this is fairly obvious, but I was having difficulty

Something like this?
SELECT groupId, MAX(version) max_version
FROM YourTable
WHERE groupId IN ('A', 'B')
GROUP BY groupId;
You didn't specify any data types so I assumed that groupId could actually take character values like 'A'. Just change this to suit your needs. The basic idea is that you GROUP BY your groupId after filtering out only those values which interest you. Then you SELECT the MAX(version) for each of those values.

Try below
select p.id,p.groupid,p.version from tablename p
left join
(
select max(id) id1 from tablename
group by groupId
order by max(id) desc
) t on t.id1 = p.id
Assuming you have a primary key column id in table

I assume version is an integer
SELECT MAX(version), `group` FROM table WHERE `group` IN (A, B) GROUP BY `group`

Related

Mysql: Select last occurence of every ID

I have multiple occurrences of a Client-ID called "IDKLIENT". I want to select the last occurrence of IDKLIENT for each ID, like
1|x 2|x
1|y 2|y
1|z 2|z
would be:
1|z
2|z
I used this code:
select a.*
from test a inner join
(select Name_Kl, max(IDKLIENT) as maxid from test group by IDKLIENT) as b on a.IDKLIENT = b.maxid
This way, I only get the same output as with
select a.*
Any help is appreciated!
Thanks in advance.
Edit: The table also has timestamps. So I would be content, if for each ID the max(timestamp) is selected.
Judging by the expected output, I believe you are looking to group by id to find the alphabetically greatest value for idklient. You can sort alphabetically by idklient using max if that is what you need:
select id, max(idklient) from test group by id;
If instead, you want it sorted by the insert order, I would suggest having an AUTO_INCREMENT field which you can then use to do the grouping. This might work better than inserting a timestamp
In response to your edit:
select id, max(timestamp) from test group by id;
This is a classic example for the group-by statement
I think group by has been changed.
Try this way
select a.*
from test a
inner join
(select Name_Kl, max(IDKLIENT) as maxid
from test
group by Name_Kl
) as b
on a.IDKLIENT = b.maxid

Get biggest COUNT(DISTINCT) with condition mysql

right now I'm trying to return the biggest COUNT(DISTINCT column)-number from a mysql table.
It's hard to describe, so I'll give you an example:
My table has the following columns: s_id, k_id, p_id.
Now I want to count the different s with the condition that every entry has the same p_id, too. I need this to prepare a HTML-Table (so i know how many Columns this table will have).
Data Example:
This is what I got, so far:
SELECT COUNT(DISTINCT k_id) AS a FROM `table`
the problem with this is, that there may be 4 different k_ids but 3 of them are related to p_id = 1 and the last one is releated to p_id = 2.
a returns 4 instead of 3.
Thanks for support!
I think you want this:
select p_id, count(distinct s_id) as cnt
from table
group by p_id
order by cnt desc
limit 1;
Please consider this:
select max(count(distinct(k_id))) from table
group by p_id

Get last but one row for each ID

I am using query like
select * from audittable where a_id IN (1,2,3,4,5,6,7,8);
For each ID its returning 5-6 records. I wanted to get the last but one record for each ID.
Can i do this in one sql statement.
Try this query
SELECT
*
FROM
(SELECT
#rn:=if(#prv=a_id, #rn+1, 1) as rId,
#prv:=a_id as a_id,
---Remaining columns
FROM
audittable
JOIN
(SELECT #rn:=0, #prv:=0) t
WHERE
a_id IN (1,2,3,4,5,6,7,8)
ORDER BY
a_id, <column> desc)tmp --Replace column with the column with which you will determine it is the last record
WHERE
rId=1;
If your database is having DateCreated or any column in which you are saving the DateTime as well like when your data is inserted for a particular row then you may use query like
select at1.* from audittable at1 where
datecreated in( select max(datecreated) from audittable at2
where
at1.id = at2.id
order by datecreated desc
);
You may also use LIMIT function as well.
Hope you understand and works for you.
In SQLite, you have the columns a_id and b. For each a_id you get a set of b's. Let you want
to get the latest/highest (maximum in terms of row_id, date or another naturally increasing index) one of b's
SELECT MAX(b), *
FROM audittable
GROUP BY a_id
Here MAX help to get the maximum b from each group.
Bad news that MySQL doesn't associate MAX b with other *-columns of the table. But it still can be used in case of simple table with a_id and b columns!

select A, B , C group by B with A from the row that has the highest C

I have collected informations from different sources about certain IDs that should match a single name. Some sources are more trustworthy than others in giving the correct name for a given ID.
I created a table (name, id, source_trustworthiness) and I want to get the most trustworthy name for each ID.
I tried
SELECT name, id, MAX( source_trustworthiness )
FROM table
GROUP BY id
this returns th highest trustworthiness available for each ID but with the first name it finds, regarless of its trustworthiness.
Is there a way I can get that right ?
Mysql has special functionality to help:
SELECT * FROM (
SELECT name, id, source_trustworthiness
FROM table
ORDER BY 3 DESC ) x
GROUP BY id
Although this wouldn't even execute in other databases (not naming all non-aggregate columns in the GROUP BY clause), with mysql it returns the first row encountered for each unique value of the grouped by columns. By ordering the rows greatest first, the first row for each id will be the most trustworthy.
Since this question is tagged mysql, this query is OK. Not only is it really simple, it's also quite fast.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT id, MAX(source_trustworthiness) max_val
FROM TableName
GROUP BY ID
) b ON a.ID = b.ID AND
a.source_trustworthiness = b.max_val

Shortest way to GROUP BY similar values and retain latest rows?

Sometimes I want to get just one row of each similar value, I ussually do somethingl ike this:
SELECT * GROUP BY Text ORDER BY Date DESC
My problem using GROUP to select similar rows is that I don't get the values from the latest rows in the row (I'm not quite sure what's the criteria to choosing the row that stays). I want to retain only the newest row in the group.
I know how to do it with a self join but when statements are already very long it seems a bit complicated. Is there any shorter method? Maybe using DISTINCT instead of GROUP BY?
Assuming you have a table that has multiple columns and two of which are GroupID and DATE. If you want to select the latest record for each GroupID, you need to have a subquery which gets the latest Date for each GroupID, example
SELECT a.* -- this just selects all records from original table
FROM tableName a
INNER JOIN
(
-- this subquery gets the latest DATE entry for each GROUPID
SELECT GroupID, MAX(DATE) maxDate
FROM tableName
GROUP BY GroupID
) b ON a.GroupID = b.GroupID AND
a.Date = b.maxDate
if this answer is not clear, please do ask :D
Did you try to use the max function:
SELECT A,B,max(Date) GROUP BY Text