Skip to content

Tag: sql-server

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…

Using the Union function in SQL to reverse columns

I have the following SQL code to produce the following table of data: I am hoping to transform data to create a table that looks like: What would be the best way to achieve this? Something like creating two tables and then a Union join? Answer You can use self join to achieve what are you looking for. Schema:…

Conditional Aggregation with multiple case and group by

The query below gives me average of case when QuoteStatusID = 6 but it I am having issues with associating the average by Street column. QuoteTable QuoteID QuoteStateID ProjectManager_userID Shipping_AddressID 1 6 12 56 2 6 12 56 3 26 12 56 4 6 12 18 5 26 12 18 Shipping_AddressID 56: 338 Elizabeth St 18: 83 E…