Skip to content

Tag: tsql

SQL. Join with filter and offset

I need to create a SQL query that will return images with tags. The result should be ordered by some column and filtered by UserID and tags. The result should be paginated. There is a SQL statement This request is not working as expected. I expect to get 50 images, but they will be combined with tags and the …

Query to display Employee and Manager

I have to write a SQL Server query to display the employee last name and his respective manager’s name, as given below. These are the schema for the two tables. Expected Output : I tried this code but its not right Detailed Schema: Schema Answer I guess something as simple as this should do it How it works: T…

SQL – % Breakout by Hour

I’m trying to write a query that will break out what percentage of time is utilized within a hour given a time range. Sample data: declare @date table(id int, Start_time time, End_time time) …

Dynamically fill an SQL table by detecting a name?

I want to dynamically fill a SQL table by detecting duplicate names. So, when I use an insert statement on this table – if the name already exists it adds an entry with the same ID. What I mean by this, for example I’ve got this empty table: Table Domain: DomainID Name URL StatusID ErrorID Date So…

Transpose row to column in SQL Server

I have a table like below: The first row is the header. Now, I would like to transpose table into this I have tried unpivot but the result is not desired table. Thanks in advance, Answer I recommend using cross apply to unpivot and then aggregation: I much, much prefer this over pivot/unpivot. Why? APPLY impl…