I have a big problem that I've been trying to tackle. Reading older similar issues hasn't been fruitful.
My table has columns [id], [parentid], [data1], [data2] etc.
There are some cases where only one record has same [parentid] and others have 2-10.
I would like to group by [parentid] and print out all data of the record which has maximum [id]. So I would only get all the "latest" details for the records which share the same [parentid] number.
I hope this is comprehensible goal in any way :).
I've also tried to tackle this in Crystal Reports XI and Qlikview 11 without major success.
Thanks in advance for any help!
Can values in your ID column be reused? If no, then juergen's answer will work.
If they can be reused, you will need to use the same table twice in your query, once to get the max id for each parent id, and once to get the row for each max id/parent id.
Like so:
select
t1.*
from aTable t1,
(select parentid, max(id) as id
from aTable group by parentid) t2
where t1.id = t2.id
and t1.parentid = t2.parentid
SQLFIddle!
select * from your_table
where id in
(
select max(id)
from your_table
group by parentid
)
A solution with qlikview would be:
Script:
Table:
Load id,
parentid,
d1,
d2
inline
[
id, parentid, d1, d2
1, 0, Hep, 01-04-2010
2, 1, Hap, 09-04-2010
3, 1, Hup, 10-10-2012
4, 2, Bep, 01-12-2009
5, 4, Ceg, 02-10-2010
6, 4, Pen, 05-10-2009
7, 4, Heg, 01-10-2009
8, 4, Ran, 08-01-2010
];
Then I added the fields id and parentid to the dashboard.
To calulate the results use a table diagram where parentid is the dimension. Add a formula
=max(id)
and name it 'max(id)'
Then you get the following result:
Related
Hello anyone can tell me what should I study about joining a table if I want to display two table with same row name? its hard to explain but I can give an example
I have 2 tables name tbwinner and tbloser.
they have the same rows ( ID, Date, Name, Points, Result )
what I want is to display all tbwinner rows with specific Date together with the tbloser asc by ID.
example
ID, Date, Name, Points, Result
1, 12-12-2001, Tester1, 50, Lose (this is from table tbloser)
2, 12-12-2001, Tester2, 90, Win (this is from table tbwinner)
3, 12-12-2001, Tester3, 87, Win (this is from table tbwinner)
1, 12-12-2001, Tester4, 40, Lose (this is from table tbloser)
I tried
SELECT *
FROM tbwinner
INNER JOIN tbloser ON tbwinner.Date = tbloser.PVDate where tblpvdeleted.PVDate ='2023-01-09' OR tblpv.PVDate = '2023-01-09'
The result show is for tbwinner only and the loser table is not displaying.
the result is half good, because only the tbwinner is display while the tbloser is not
I hope someone can understand this thanks
You can use either approach as per your need:
Using UNION
SELECT ...
FROM (
SELECT f1,f2,f3 FROM table1
UNION
SELECT f1,f2,f3 FROM table2
)
WHERE < your condition >
Multi-select (easy)
SELECT * from table1,table2
I have a table that has 3 columns
id, Name, time
id is an incrementing int value
Name is a string identifier
time is just a epoc value of when the item was added to the table.
I can have something that looks like this.
1, Jeff, 1520288589
2, Jeff, 1520288590
3, Jeff, 1520288591
4, Tim, 1520288592
5, Jeff, 1520288593
I would like to know how I can create an SQL statement to request the last Tim row and the last Jeff row.
In your case, you can just do:
select max(id), name, max(time)
from t
group by name;
The id and time seem to both be incrementing over time.
A more general solution is:
select t.*
from t
where t.time = (select max(t2.time) from t t2 where t2.name = t.name);
I have a table which contains a column called ticket_id and it contains values as follows:
ticket_id
STK0000000001
STK0000000002
STK0000000001
STK0000000003
STK0000000002
STK0000000001
The ticket_id value will repeat in certain rows, so it is not unique.
I am using this query to get the next available id, but I am not able to get it working. It always returns STK0000000002.
Any help is appreciated!
SQL:
SELECT
CONCAT('STK', LPAD(seq, 10, '0')) AS nextID
FROM
(SELECT
#seq:=#seq+1 AS seq,
num
FROM
(SELECT
CAST(SUBSTR(ticket_id, 4) AS UNSIGNED) AS num
FROM
sma_support_tickets
UNION ALL
SELECT
MAX(CAST(SUBSTR(ticket_id, 4) AS UNSIGNED))+2 AS num
FROM
sma_support_tickets
ORDER BY
num) AS ids
CROSS JOIN
(SELECT #seq:=0) AS init
) AS pairs
WHERE
seq!=num
LIMIT 1
Maybe I'm missing something in your question, but it seems that this should do it:
SELECT CONCAT('STK',
LPAD(MAX(SUBSTRING(ticket_id, 4)) + 1,
10,
'0')
)
FROM sma_support_tickets;
Try: This table must have one serial number or unique number or ID for the table for each row. Find out that unique number(primary key) through code and add 1 or increment that number, but not to the ticket_id as you are doing it now. so that it can move forward to next row.
I've got table with two fields: id, numbers
I want to check if the same array values (etc 1,2,5,6) already exist in table, but they have to have the same 1nd row number (id).
I've tried with this sql query, but this one doesn't check if there is the same id:
SELECT id, numbers FROM `table` WHERE numbers IN (1,2,5,6) AND id = id
I know that "id = id" doesn't work, but I posted it so you'll know what I mean.
SELECT id, COUNT(*) num_count, GROUP_CONCAT(numbers ORDER BY numbers) all_numbers
FROM `table`
WHERE numbers IN (1, 2, 5, 6)
GROUP BY id
If you only want to see the ones that have all 4 numbers, add:
HAVING num_count = 4
If you want the IDs that have all and only those 4 numbers, use:
SELECT id, COUNT(*) all_count, SUM(numbers IN (1, 2, 5, 6)) in_count
FROM `table`
GROUP BY id
HAVING all_count = in_count
i have simple query:
SELECT data FROM table WHERE id IN (5, 2, 8, 1, 10)
Question is, how can i select my data and order it like in my IN.
Order must be 5, 2, 8, 1, 10.
Problem is that i have no key for order. IN data is from other query (1), but i need to safe order.
Any solutions?
(1)
SELECT login
FROM posts
LEFT JOIN users ON posts.post_id=users.id
WHERE posts.post_n IN (
2280219,2372244, 2345146, 2374106, 2375952, 2375320, 2371611, 2360673, 2339976, 2331440, 2279494, 2329266, 2271919, 1672114, 2301856
)
Thanx for helping, solutions works but very slow, maybe find something better later, thanx anyway
The only way I can think to order by an arbitrary list would be to ORDER BY comparisons to each item in that list. It's ugly, but it will work. You may be better off sorting in whatever code you are doing the selection.
SELECT data FROM t1 WHERE id IN (5, 2, 8, 1, 10)
ORDER BY id = 10, id = 1, id = 8, id = 2, id = 5
The order is reversed because otherwise you would have to add DESC to each condition.
You can use a CASE statement
SELECT data
FROM table WHERE id IN (5, 2, 8, 1, 10)
ORDER BY CASE WHEN id = 5 THEN 1 WHEN id = 2 THEN 2 WHEN id = 8 THEN 3 WHEN id = 1 THEN 4 WHEN id = 10 THEN 5 END
SELECT data FROM table
WHERE id IN (5, 2, 8, 1, 10)
ORDER BY FIELD (id, 5, 2, 8, 1, 10)
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
Might be easier to auto-generate (because it basically just needs inserting the wanted IDs comma-separated in the same order a second time) than the other solutions suggested using CASE or a number of ID=x, ID=y ...
http://sqlfiddle.com/#!2/40b299/6
I think that's what you're looking for :D Adapt it to your own situation.
To do this dynamically, and within MySql, I would suggest to do the following:
Create a temp table or table variable (not sure if MySql has these), with two columns:
OrderID mediumint not null auto_increment
InValue mediumint -(or whatever type it is)
Insert the values of the IN clause in order, which will generate ID's in order of insertion
Add a JOIN to your query on this temp table
Change your Order By to be
order by TempTable.OrderID
Drop temp table (again, in SQL inside a stored proc, this is automatic, not sure about MySql so mentioning here for full disclosure)
This effectively circumvents the issue of you not having a key to order by in your table ... you create one. Should work.