This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
I am trying to solve a duplicate issue in SQL server.
I have a lot of duplicate records with the exact information except the column time like:
I would like to select the record that has the max datetime for each subset of duplicates.
I tried to use the MAX() aggregation but that does not work.
Any ideas?
In MS SQL Server, you would do something like this:
SELECT [id], [name], MAX([date])
FROM [table_name]
GROUP BY [id], [name]
SELECT id, name, MAX(date) as maxdate FROM mytable GROUP BY id, name
This should give you exactly the results you describe, in SQL Server.
This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 years ago.
hey i have an database project and i want to get the last row from my table gadgets, for the first row i wrotte this and it worked
SELECT *
from Gadgets
where ROWNUM =1
but for the last index for example 100 it doesn't work
SELECT *
from Gadgets
where ROWNUM =100
how i have to writte my comand that i will work, i work on oracle
There is no such thing as a "first" or "last" row in the table. SQL tables represent unordered sets (technically multi-sets because duplicates are allowed). You need an order by.
If you have an incremental id or created date, then you can use that to define "first" or "last". For the "first" row:
select t.*
from t
order by <ordering column>
fetch first 1 row only;
And for the "last" row:
select t.*
from t
order by <ordering column> desc
fetch first 1 row only;
This question already has answers here:
PHP PDO returning single row
(7 answers)
Closed 4 years ago.
select * from addcuust order by customer asc
This is my query. When I run this query both rows selecting what should I do?
I just want 1 row at once.
select * from addcuust order by customer asc limit 1
This question already has answers here:
SELECT DISTINCT on one column
(7 answers)
Closed 8 years ago.
I appreciate that this question has been asked before but I am struggling to find an answer that will even run within Oracle 10g (10.2.0.5.0)
I have a table called BASIC which contains approximately 70 columns. Currently, I return a specified number of rows using the following code (as an example) - the result being the first 20 members who have a MEMBNO after 5000
SELECT * FROM BASIC WHERE MEMBNO>5000 AND ROWNUM <=20 ORDER BY MEMBNO;
Within the 20 rows returned, several of the rows have the same value in the NINO column
I would like to modify my SELECT statement to return the next 20 rows with distinct/unique NINO values
Simply wrapping a DISTINCT around the * gives me an ORA-00936: missing expression error, plus it would not be as precise as I would like.
Can you try the code below:- I have used analytical query concept to fetch only distinct nino values.
select * from
(SELECT b.*,row_number() over (partition by nino order by MEMBNO ) rn
FROM BASIC b WHERE MEMBNO>5000)
where rn =1 AND ROWNUM <=20 ORDER BY MEMBNO;
Let me know in case you encounter any issues.
I think I have found a solution via another source
This shows the rows where there are duplicates...
select * from basic where rowid not in (select min(rowid) from basic group by nino)
This shows the rows with the duplicate rows removed...
select * from basic where rowid in (select min(rowid) from basic group by nino)
Then I can add my row count and membno filters for the final result...
select * from basic where rowid in (select min(rowid) from basic where membno>6615 group by NINO) and rownum <=20 order by membno;
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
SQL query to find Missing sequence numbers
I have a table which has a user Id column, The user could select which user ID to add in the table. I am wondering if there is a one sql code that could point me to the list of unused user id or even just the smallest unused ID?
For example, I have the following IDs
USER_ID
1
2
3
5
6
7
8
10
I would like to know if there is a way to select 4 or even selecting 4 and 9?
You can try using the "NOT IN" clause:
select
user_id
from table
where
user_id not in (select user_id from another_table)
Like this:
select
u1.user_id + 1 as start
from users as u1
left outer join users as u2 on u1.user_id + 1 = u2.id
where
u2.id is null
From here.
It depends on the Database you are using. If you are using Oracle, something like this will work:
Step 1: Find out max value of userid in your table:
select max(userid) from tbl_userid
let this number be m
Step 2: Find out the max value of rownum in the foll query
select rownum from all_objects
Step 3: If the max value is greater than m then you can use the foll query to list your unused user ids
select user_id
from tbl_userid
where user_id NOT IN (select rownum from all_objects)
If max value returned by step 2 is less than m you can tweak your query to the following
select user_id
from tbl_userid
where user_id NOT IN
(select rownum
from (select *
from all_objects
UNION ALL
select * from all_objects)
)
Repeat the UNION ALL until you get max(rownum) >= m
If you are using SQL server, kindly let me know. There is no direct equivalent of ROWNUM pseudocolumn in sql server but there are workarounds using the RANK() function.
Given that SQL is generally a set-based language, the only way I could think to do this would be to create the full set of ID's, and outer join your table where no ID's matched. Problem with that is if your table has a significant number of records, you would have to generate a temporary table containing every ID from 1 through MAX(USER_ID). Given a table with tens or hundreds of millions of records, that could be very slow.
Just out of curiosity, why do you need to know the ID holes? Is there some specific reason, or are you just trying to not "waste" an ID? Given the processing effort to find the holes, I would think it is more efficient to just let them be.
Here's one way to do it using SQL Server 2005 or later. It may or may not work efficiently for you:
insert into T values
(1),(2),(3),(5),(6),(9),(11);
with Trk as (
select userid,
row_number() over (
order by userid
) as rk
from T
), Truns(start,finish,gp) as (
select -1+min(userid), 1+max(userid),
userid-rk
from Trk
group by userid-rk
), Tregroup as (
select start, finish,
row_number() over (
order by gp
) as rk
from Truns
), Tpre as (
select a.finish, b.start
from Tregroup as a full outer join Tregroup as b
on a.rk + 1 = b.rk
)
select
rtrim(finish) + case when start = finish then '' else + '-' + rtrim(start) end as gap
from Tpre
where finish+start is not null
drop table T;
Short of looping through all the ids (perhaps using binary search tree logic?) I don't have a good answer for you.
I would ask what you want this for? By their nature, ids are essentially meaningless - all they do is identify some data, not describe it, and as such it shouldn't be a problem if you have large gaps in your user ids. (In fact, some people would say that it's even better to have unguessable ids, to avoid users tampering with information to find security holes)