How to select last inserted row by id? - database

that my problem:
I have database table like that:
id (AI) market_id
1 6
2 10
3 6
4 10
5 11
How to select last inserted market_id by id?

You could do something like this:
SELECT * FROM <your_table> ORDER BY id DESC LIMIT 1;
If you only are interested in the market_id you could change the * in the above query to market_id.

You can use LAST_INSERT_ID():
SELECT market_id FROM <table> where id=LAST_INSERT_ID();

For SQL Server,
Select TOP 1 ID, market_I'd from table order by ID desc

Related

SQL group need related value instead of aggregate function

Have a table like this
FileID Value Version
-------------------------
1 Welle 2
1 Achse 3
2 Box 5
2 Enclosure 7
I need to "sum" up the lines with same FileID -> take highest value from column VERSION and get back the related value.
Desired result would be:
FileID Value Version
-------------------------
1 Achse 3
2 Enclosure 7
However using GROUP By sums up, but brings wrong result for Value:
SELECT
[FileID],
MAX([Value]),
MAX([Version])
FROM [ValueMist]
GROUP BY FileID
This returns:
FileID Value Version
------------------------
1 Welle 3
2 Enclosure 7
One option is WITH TIES in concert with row_number()
Example
Select top 1 with ties *
From YourTable
Order By row_number() over (partition by FileId Order By version desc)
You can achieve this by using ROW NUMBER
;WITH CTE AS (SELECT ROW_NUMBER() OVER ( PARTITION BY
ID ORDER BY VERSION DESC) AS RW
FROM TABLE)
SELECT * FROM CTE
WHERE RW=1

How to select the value from the table based on category_id USING SQL SERVER

How to select the value from the table based on category_id?
I have a table like this. Please help me.
Table A
ID Name category_id
-------------------
1 A 1
2 A 1
3 B 1
4 C 2
5 C 2
6 D 2
7 E 3
8 E 3
9 F 3
How to get the below mentioned output from table A?
ID Name category_id
--------------------
1 A 1
2 A 1
4 C 2
5 C 2
7 E 3
8 E 3
Give a row number for each row based on group by category_id and sort by ascending order of ID. Then select the rows having row number 1 and 2.
Query
;with cte as (
select [rn] = row_number() over(
partition by [category_id]
order by [ID]
), *
from [your_table_name]
)
select [ID], [Name], [category_id]
from cte
where [rn] < 3;
Kindly run this query It really help You Out.
SELECT tbl.id,tbl.name, tbl.category_id FROM TableA as tbl WHERE
tbl.name IN(SELECT tbl2.name FROM TableA tbl2 GROUP BY tbl2.name HAVING Count(tbl2.name)> 1)
Code select all category_id from TableA which has Name entries more then one. If there is single entry of any name group by category_id then such data will be excluded. In above example questioner want to eliminate those records that have single Name entity like wise category_id 1 has name entries A and B among which A has two entries and B has single entry so he want to eliminate B from result set.

Select randomly few Rows of the same ID in the same table (T-SQL)

I'm trying to select randomly few rows for each Id stored in one table where these Ids have multiple rows on this table. It's difficult to explain with words, so let me show you with an example :
Example from the table :
Id Review
1 Text11
1 Text12
1 Text13
2 Text21
3 Text31
3 Text32
4 Text41
5 Text51
6 Text61
6 Text62
6 Text63
Result expected :
Id Review
1 Text11
1 Text13
2 Text21
3 Text32
4 Text41
5 Text51
6 Text62
In fact, the table contains thousands of rows. Some Ids contain only one Review but others can contain hundreds of reviews. I would like to select 10% of these, and select at least once, all rows wich have 1-9 reviews (I saw the SELECT TOP 10 percent FROM table ORDER BY NEWID() includes the row even if it's alone)
I read some Stack topics, I think I have to use a subquery but I don't find the correct solution.
Thanks by advance.
Regards.
Try this:
DECLARE #t table(Id int, Review char(6))
INSERT #t values
(1,'Text11'),
(1,'Text12'),
(1,'Text13'),
(2,'Text21'),
(3,'Text31'),
(3,'Text32'),
(4,'Text41'),
(5,'Text51'),
(6,'Text61'),
(6,'Text62'),
(6,'Text63')
;WITH CTE AS
(
SELECT
id, Review,
row_number() over (partition by id order by newid()) rn,
count(*) over (partition by id) cnt
FROM #t
)
SELECT id, Review
FROM CTE
WHERE rn <= (cnt / 10) + 1
Result(random):
id Review
1 Text12
2 Text21
3 Text31
4 Text41
5 Text51
6 Text63

sql select from multiple records only the most recent

i have a table named customer_age that loks like this:
ID 1 2 3 4 5 6 7 8 9
NAME JIM JIM JIM NICK NICK NICK Paul Paul Paul
VALUE 20 13 12 10 20 8 4 24 14
and i want to display only the first record from each name. Something like this
ID 1 4 7
NAME JIM NICK Paul
VALUE 20 10 4
So far i have not been able to work it out.
i use sql server 2005
Any help would be appreciated...
Try using a subselect to find the lowest ID for each name, and use that set of IDs to pull the records from the main table:
SELECT ID, Name, Value
FROM customer_age
WHERE ID IN
(
SELECT MIN(ID) AS ID
FROM customer_age
GROUP BY Name
)
Just select the first record for each name using cross apply:
SELECT
ca.ID, ca.NAME, ca.VALUE
FROM customer_age c
CROSS APPLY (SELECT TOP 1 ID, NAME, VALUE
FROM customer_age ca
WHERE ca.NAME = c.NAME ORDER BY ID) ca
ORDER BY ca.ID
How about using window functions??
SELECT Id, Name, Value
FROM (
SELECT Id, Name, Value, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Id ASC) AS rowNum
FROM customer_age
) AS sub
WHERE rowNum = 1
Assuming first record means highest ID, you may try your query with descending orderby ID and TOP n.

Want data from table in SQL server

I have table data like this
id id1 name
1 1 test1
1 1 test1
1 2 test2
2 1 test1
2 2 test2
3 1 test1
3 2 test2
3 2 test2
now from table i want the data as below
like
for id = 1 order by id1 asc the first name = test1
so i want the first two row
id id1 name
1 1 test1
1 1 test1
not third row
For id=2 order by id1 asc the first name = test1
so i want first row as test1 has assign only ones for id=2
id id1 name
2 1 test1
And for id=3 same as id=2
Please suggest me how can get the perticlur value for ID , because the scenerio is differnt for ID=1
Use RANK() or DENSE_RANK() to get the first ranked rows, including duplicates, for each id.
select * from (
select *, dense_rank() over (partition by [id] order by [id2]) as ranking
from [table]
) as t
where ranking = 1
Sounds to me like you just want select * from [tablename] where id1 = 1, but I might be wrong. I find the question a bit, well, vague...
Perhaps this:
select * from [table] where id = 1 order by id1
I think the point is that you can use one column in the where clause, and a different column in the order by clause. that's no problem.
But I'm not sure you could actually have the data table you describe, because the first two rows are identical (how can SQL tell them apart? Or more technically, there'd be a primary key violation)?
select * from [table] where id = 1 and name like "test1" order by id1

Resources