Would anybody be able to help me with this exercise. I am used to querying on postgresql and not t-sql and I am running into trouble with how some of my data aggregates
My assignment requires me to:
Create a query that returns the number of comments made on each day for each post from the top 50 most commented on posts in the past year.
For example, this query below is giving me a non aggregated result set:
select cast(creationdate as date),
postid,
count(id)
from comments
where postid = 17654496
group by creationdate, postid
The schema is all here
https://data.stackexchange.com/stackoverflow/query/edit/898297
You can try to use CTE get the count by date.
then use window function with ROW_NUMBER make row number order by count amount desc.
;with CTE as (
select cast(creationdate as date) dt,
postid,
count(id) cnt
from comments
WHERE creationdate between dateadd(year,-1,getdate()) and getdate()
group by cast(creationdate as date), postid
), CTE2 AS (
select *,ROW_NUMBER() OVER (order by cnt desc) rn
from CTE
)
SELECT *
FROM CTE2
WHERE rn <=50
https://data.stackexchange.com/stackoverflow/query/898322/test
Related
Priority is the output column.
The group contains duplicate content.
how can I fix this using SQL query?.
enter image description here
One way to achieve the desired result with this data is to use Dense_rank() function like below:
select *, dense_rank() over (order by [Group]) as Priority
from tab
order by No
For any value, please try the following
;with cte as
(
select [Group], ROW_NUMBER() over (order by No_min) as rn
from
(
select [Group], min([No]) No_min
from tab
group by [Group]
)t
)
select t.*, x.rn as [Priority]
from cte x
join tab t on t.[Group] = x.[Group]
order by 1
Please find the db<>fiddle here.
How can I get only the last run of each SSIS job using a T-SQL query?
Please check the image. These details are stored in a single Table XYZ. Can anyone help me by providing a T-SQL query?
I am using this query:
select Job_Name, Start_Date, Finish_Time, Remarks
from #XYZ
order by Job_Name
You can use row_number()
select x.*
from (select x.*,
row_number() over (partition by job_name order by try_convert(datetime, Finish_Time) desc) as seq
from #XYZ x
) x
where x.seq = 1;
You can do this by adding a row_number to sort the data and then filtering to where a 1 is returned:
select cols
from(select cols
,row_number() over (partition by Job_Name order by Start_Date desc) as rn
from table
) as t
where rn = 1
I have a table which has around 300,000 rows. 225 Rows are being added to this table daily since March 16,2015 till July 09,2015
My problem is that, from last 1 week or so, some duplicate rows are being entered in the table (i.e more than 225 per day)
Now I want to select (and ultimately delete!) all the duplicate rows from the table that have more than 1 siteID+ reportID combination existing against one Date column .
Example is attached in the screenshot:
When Row_Number() is used with Partition By clause, it can provide the SQL developer to select duplicate rows in a table
Please check the SQL tutorial on how to delete duplicate rows in SQL table
Below query is what is copied from that article and applied to your requirement:
;WITH DUPLICATES AS
(
SELECT *,
RN = ROW_NUMBER() OVER (PARTITION BY siteID, ReportID ORDER BY Date)
FROM myTable
)
DELETE FROM DUPLICATES WHERE RN > 1
I hope it helps,
When you want to filter duplicated rows I suggest you this type of query:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY Col3) As seq
FROM yourTable) dt
WHERE (seq > 1)
Like this:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY siteID, reportID, [Date] ORDER BY ID) As seq
FROM yourTable) dt
WHERE (seq > 1)
I'm having trouble figuring out how to make this query work. I've tried everything under the sun to avoid looping.
The table has ID (pk), UserID, BookID, BookDate (datetime), and SellerID. There are duplicate combinatins of UserID and BookID.
I am trying to retrieve distinct records by UserID and BookID that have the most recent BookDate. That's easy enough (below), but I also need to retrieve the ID and SellerID columns for the returned record. That's where I'm having trouble...
Select Distinct
UserID, CourseID, MAX(AssignedON)
From
AssignmentS
Group By
UserID, CourseID
Every time I add a join I get all records. I've tried rowover, exists and nothing seems to work. Any help would be greatly appreciated!
select userid,courseid,bookdate,sellerid from
(select userid,courseid,bookdate,sellerid,
row_number() over (partition by userid,courseid
order by bookdate desc) as RNUM
from yourtable where yourwhere)
where rnum = 1;
[This][1]
[1]: http://coding.feron.it/2012/08/mssql-having-maxid-id-problem-row_number-partition/ blog post describes in detail how to do this with multiple tables
Figured it out. I just had to move things around a bit and this is working perfectly!
select userid,courseid,bookdate,sellerid from (
select * row_number() over(partition by userid,courseid, order by bookdate desc) as RNUM
from yourtable where yourwhere)
where rnum = 1;
Consider following tables:
How to skip and take groups from the table? Tried using Row_Number() but it doesn't help. Any ideas?
Used query
;WITH cte AS (SELECT Room.Id, Room.RoomName,
ROW_NUMBER() OVER
(ORDER BY Room.Id) AS RN
FROM Room INNER JOIN
RoomDetails ON Room.Id = RoomDetails.RoomId)
SELECT Id, RoomName
FROM cte
WHERE RN = 1
You need to use partition as part of the dense_rank function
dense_rank() over (partition by roomid) as row
see here for some more examples Windowing functions