I am using SQL Server Express 2014 and I need to pull out the last record for few (3 for now) tags with different IDs from one table.
So far I made it but not at all. I am using
SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int1'
order by [TagTimestamp] desc
SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int2'
order by [TagTimestamp] desc
SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int3'
order by [TagTimestamp] desc
and the result is what I need, but not exactly. I need to get the three results in single table like:
TagItemId TagValue
Random.Int1 55
Random.Int2 75
Random.Int3 23`
and not like:
TagItemId TagValue
Random.Int1 55
TagItemId TagValue
Random.Int2 75
TagItemId TagValue
Random.Int3 23`
The reason is that I need to use the data for a chart.
Best regards and thanks!
You could do this using Row_Number
SELECT [TagItemId],
[TagValue]
FROM
(
SELECT [TagItemId],
[TagValue],
ROW_NUMBER() OVER (PARTITION BY [TagItemId] ORDER BY [TagTimestamp] DESC) Rn
FROM [DB].[dbo].[Table]
WHERE [TagItemId] IN ('Random.Int1','Random.Int2','Random.Int3')
) t
WHERE Rn = 1
There are several ways to accomplish this:
SELECT
MT.TagItemID,
MT.TagValue
FROM
My_Table MT
INNER JOIN
(
SELECT TagItemID, MAX(TagTimestamp)
FROM My_Table
WHERE
MT.TagItemID IN ('Random.Int1', 'Random.Int2', 'Random.Int3')
GROUP BY TagItemID) SQ ON SQ.TagItemID = MT.TagItemID
WHERE
MT.TagItemID IN ('Random.Int1', 'Random.Int2', 'Random.Int3')
Or:
SELECT
MT.TagItemID,
MT.TagValue
FROM
My_Table MT
WHERE
MT.TagItemID IN ('Random.Int1', 'Random.Int2', 'Random.Int3') AND
NOT EXISTS (SELECT * FROM My_Table MT2 WHERE MT2.TagItemID = MT.TagItemID AND MT2.Timestamp > MT.Timestamp)
Or:
;WITH CTE_WithRowNums AS
(
SELECT
MT.TagItemID,
MT.TagValue,
ROW_NUMBER() OVER(PARTITION BY TagItemID ORDER BY Timestamp DESC) AS row_num
FROM
My_Table MT
)
SELECT
TagItemID,
TagValue
FROM
CTE_WithRowNums
WHERE
row_num = 1
Could you not just do a simple UNION ALL
select * from
(SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int1'
order by [TagTimestamp] desc )
UNION ALL
select * from
(SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int2'
order by [TagTimestamp] desc )
UNION ALL
select * from
(SELECT TOP 1 [TagItemId], [TagValue]
FROM [DB].[dbo].[Table]
where [TagItemId] like 'Random.Int3'
order by [TagTimestamp] desc )
Probably crashed because each select TOP had an order by, so I "wrapped" the queries into select * so each subquery could retain its own ORDER BY clause
Related
I have a Oracle table with the following columns
Table Structure
In a query I need to return all the records with CPER>=40 which is trivial. However, apart from CPER>=40 I need to list 5 random records for each CPID.
I have attached a sample list of records. However, in my table I have around 50,000 records.
Appreciate if you can help.
Oracle solution:
with CTE as
(
select t1.*,
row_number() over(order by DBMS_RANDOM.VALUE) as rn -- random order assigned
from MyTable t1
where CPID <40
)
select *
from CTE
where rn <=5 -- pick 5 at random
union all
select t2.*, null
from my_table t2
where CPID >= 40
SQL Server:
with CTE as
(
select t1.*,
row_number() over(order by newid()) as rn -- random order assigned
from MyTable t1
where CPID <40
)
select *
from CTE
where rn <=5 -- pick 5 at random
union all
select t2.*, null
from my_table t2
where CPID >= 40
How about something like this...
SELECT *
FROM (SELECT CID,
CVAL,
CPID,
CPER,
Row_number() OVER (partition BY CPID ORDER BY CPID ASC ) AS RN
FROM Table) tmp
WHERE CPER>=40 OR pids <= 5
However, this is not random.
Assuming that you want five additional random records, you can do:
select t.*
from (select t.*,
row_number() over (partition by cpid,
(case when cper >= 40 then 1 else 2 end)
order by dbms_random.value
) as seqnum
from t
) t
where seqnum <= 5 or cper >= 40;
The row_number() is enumerating the rows for each cpid in two groups -- based on the cper value. The outer where is taking all cper values in the range you want as well as five from the other group.
how do i produce this SQL snippet as a temp table so I can join some other stuff into it?
with MyCTE AS
(
select *, RANK() OVER (PARTITION BY workplace ORDER BY Total DESC) AS Rank
from [dbo].[OriginDestination]
)
select * from MyCTE where Rank <= 5
Like this:
with MyCTE AS
(
select *, RANK() OVER (PARTITION BY workplace ORDER BY Total DESC) AS Rank
from [dbo].[OriginDestination]
)
select *
into #yourTempTable
from MyCTE
where Rank <= 5
I want to retrieve latest records of two tables using UNION, it returns data, but not latest records, even with ORDER BY. It's my query:
SELECT TOP(1) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM TheaterNews
UNION
SELECT TOP(3) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM MoviesNews
ORDER BY 3 DESC
This is query's output:
But the latest record of TheaterNews is newer:
SELECT OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM TheaterNews
ORDER BY NewsCreationDate DESC
How can I fix this? even with another method.
The order by applies to the entire result of the union, not the separate queries, so the top is applied before the result is sorted.
Use subqueries to order the separate results:
SELECT * FROM (
SELECT TOP(1) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM TheaterNews
ORDER BY 3 DESC
) x
UNION
SELECT * FROM (
SELECT TOP(3) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM MoviesNews
ORDER BY 3 DESC
) y
Because top clause is getting executed before order by clause. try this
Select * from (SELECT TOP(1) OwnerID,NewsTitle,NewsCreationDate,
NewsEnglishName
FROM TheaterNews ORDER BY 3 DESC) as tn
UNION
Select * From (SELECT TOP(3)
OwnerID,NewsTitle,NewsCreationDate,NewsEnglishName
FROM MoviesNews ORDER BY 3 DESC) as mn
ORDER BY 3 DESC
The outer order by will order the union result
This should work in your instance. Might be a rough solution but you will get your answers. you can replace the #tables with #tables.
SELECT TOP(1)
OwnerID,
NewsTitle,
NewsCreationDate,
NewsEnglishName
into
#Table1
FROM
TheaterNews
ORDER BY 3 DESC
SELECT TOP(3)
OwnerID,
NewsTitle,
NewsCreationDate,
NewsEnglishName
into
#Table2
FROM
MoviesNews
ORDER BY 3 DESC
select * from #Table1
union
select * from #Table2
drop table #Table1
drop table #Table2
Try this:
Select * From ( SELECT TOP(1) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM TheaterNews ORDER BY NewsCreationDate DESC) as tempA
UNION
Select * From ( SELECT TOP(3) OwnerID,NewsTitle,NewsCreationDate,NewsTitle,NewsEnglishName
FROM MoviesNews ORDER BY NewsCreationDate DESC) as tempB
How to select top 2 , bottom 2 and 6 random (not in Top 2 and Bottom 2) records of the table using one SQL select query?
In MS SQL 2005/2008:
with cte
as
(
select
row_number() over (order by name) RowNumber,
row_number() over (order by newid()) RandomOrder,
count(*) over() Total,
*
from sys.tables
)
select *
from cte
where RowNumber <= 2 or Total - RowNumber + 1 <= 2
union all
select *
from
(
select top 6 *
from cte
where RowNumber > 2 and Total - RowNumber > 2
order by RandomOrder
) tt
Replace sys.tables with your table name and alter order by name to specify order condition for top 2 and bottom 2.
Perhaps not a single select statment, but it can be executed in one call:
/* Top 2 - change order by to get the 'proper' top 2 */
SELECT * from table ORDER BY id DESC LIMIT 2
UNION ALL
/* Random 6.. You may want to add a WHERE and random data to get the random 6 */
/* Old Statement before edit - SELECT * from table LIMIT 6 */
SELECT * from table t
LEFT JOIN (SELECT * from table ORDER BY id DESC LIMIT 2) AS top ON top.id = t.id
LEFT JOIN (SELECT * from table ORDER BY id DESC LIMIT 2) AS bottom ON bottom.id = t.id
WHERE ISNULL(top.id ) AND ISNULL(bottom.id)
ORDER BY RANDOM()
LIMIT 6
UNION ALL
/* Bottom 2 - change order by to get the 'proper' bottom 2 */
SELECT * from table ORDER BY id ASC LIMIT 2
Something along those lines. Basically the UNION All is the trick.
Assuming the "order" is by the id column:
select * from (select id, id from my_table order by id limit 2) t1
union
select * from (select id, id from my_table where id not in (
select * from (select id from my_table order by id asc limit 2) t22
union
select * from (select id from my_table order by id desc limit 2 ) t23)
order by rand()
limit 6) t2
union
select * from (select id, id from my_table order by id desc limit 2) t3
EDIT: Fixed syntax and tested query - it works
I'm using SQL Server 2005 and I'm trying to achieve something like this:
I want to get the first x rows and the last x rows in the same select statement.
SELECT TOP(5) BOTTOM(5)
Of course BOTTOM does not exist, so I need another solution. I believe there is an easy and elegant solution that I'm not getting. Doing the select again with GROUP BY DESC is not an option.
Using a union is the only thing I can think of to accomplish this
select * from (select top(5) * from logins order by USERNAME ASC) a
union
select * from (select top(5) * from logins order by USERNAME DESC) b
Check the link
SQL SERVER – How to Retrieve TOP and BOTTOM Rows Together using T-SQL
Did you try to using rownumber?
SELECT *
FROM
(SELECT *, ROW_NUMBER() OVER (Order BY columnName) as TopFive
,ROW_NUMBER() OVER (Order BY columnName Desc) as BottomFive
FROM Table
)
WHERE TopFive <=5 or BottomFive <=5
http://www.sqlservercurry.com/2009/02/select-top-n-and-bottom-n-rows-using.html
I think you've two main options:
SELECT TOP 5 ...
FROM ...
ORDER BY ... ASC
UNION
SELECT TOP 5 ...
FROM ...
ORDER BY ... DESC
Or, if you know how many items there are in the table:
SELECT ...
FROM (
SELECT ..., ROW_NUMBER() OVER (ORDER BY ... ASC) AS intRow
FROM ...
) AS T
WHERE intRow BETWEEN 1 AND 5 OR intRow BETWEEN #Number - 5 AND #Number
Is it an option for you to use a union?
E.g.
select top 5 ... order by {specify columns asc}
union
select top 5 ... order by {specify columns desc}
i guess you have to do it using subquery only
select * from table where id in (
(SELECT id ORDER BY columnName LIMIT 5) OR
(SELECT id ORDER BY columnName DESC LIMIT 5)
)
select * from table where id in (
(SELECT TOP(5) id ORDER BY columnName) OR
(SELECT TOP(5) id ORDER BY columnName DESC)
)
EDITED
select * from table where id in (
(SELECT TOP 5 id ORDER BY columnName) OR
(SELECT TOP 5 id ORDER BY columnName DESC)
)
No real difference between this and the union that I'm aware of, but technically it is a single query.
select t.*
from table t
where t.id in (select top 5 t2.id from table t2 order by MyColumn)
or
t.id in (select top 5 t2.id from table t2 order by MyColumn desc);
SELECT *
FROM (
SELECT x, rank() over (order by x asc) as rown
FROM table
) temp
where temp.rown = 1
or temp.rown = (select count(x) from table)
Then you are out - doing the select again IS the only option, unless you want to pull in the complete result set and then throwing away everything in between.
ANY sql I cna think of is the same way - for the bottom you need to know first either how many items you have (materialize everything or use count(*)) or a reverse sort order.
Sorry if that does not suit you, but at the end.... reality does not care, and I do not see any other way to do that.
I had to do this recently for a very large stored procedure; if your query is quite large, and you want to minimize the amount of queries you could declare a #tempTable, insert into that #tempTable then query from that #tempTable,
DECLARE #tempTable TABLE ( columns.. )
INSERT INTO #tempTable
VALUES ( SELECT.. your query here ..)
SELECT TOP(5) columns FROM #tempTable ORDER BY column ASC -- returns first to last
SELECT TOP(5) columns FROM #tempTable ORDER BY column DESC -- returns last to first