I saw a delete statement written in below format and I need help in understanding it. create table t1 (id int); create table t2 (id int); insert into t1 values(1); insert into t1 values(2); insert …
Tag: sql-server
(SQL Server 2017) I get error Msg 8152 and I don’t know why
I have no idea why, but I am getting the error Msg 8152 (in SQL Server 2017) after trying to insert something like into the table The output I get is Msg 8152, Level 16, State 14, Line 1 String or binary data would be truncated. Thanks in advance for your help! Answer You need to define a length for
Is a cursor/recursion the only possible option here? Set based logic based on certain lag conditions
I’m using SQL Server, I have a table with 3 columns (timeseries) data, with date, hour beginning, AwardStatus. The award status for the most part is randomly generated. There can be two options, …
TSQL – Parent Child (1 to zero/many) Grouping/Aggregation
Code (Sample Data Staging): Description: @Emp is the sample Employee table (Unique Employee records). EId = Employee Id FN = First Name LN = Last Name @EmpPhCont is the sample Employee Phone Contact table (Each Emp from @Emp table can have zero, one, or multiple phone numbers here – unique by Emp/Type).…
I have a trigger on my SQL Server table which takes user updates and log them, in case we need to revert, but it has a problem
The problem is, sometimes in a day that no one is changing anything, a random user just enter the page and the trigger saves a change. The problem is, not only it logs a change that never has occurred …
SQL Count columns from other columns
I have this view generated after using LEFT JOIN over 2 tables (simplified example). Tables: T1: Id, …other columns not used +—-+—–+ | Id | … | +—-+—–+ | 1 | … | | 2 | … | +—-+-…
SQL SERVER PIVOT like PEPSI comes under year then by months etc.. (refer attached image for a clear idea)
I am new to PIVOT… so i dont know whether is it possible or not with pivot. Here is the table and the sample data i used create table sales_history (brandcode varchar(10) , …
Significant delay in generation of View (Viewed Table) in SQL Server
I have a few Personnel Details tables in a database in SQL Server 2014. There is a view that pick up selected data from these tables by implementing a few joins. One condition to select the row is if its columnn isActive = 1 in NominalRoll table. There is a report in SQL Server Reporting Server that calls thi…
Amend Report to use financial years and quarters
I need to amend this existing code to group by financial year (starts 1 July) and quarters, I realize that it is the below part of the query that requires amending but I can’t get it to work The full query is Sample data Lyaout as per below but with finacial yaer ^ qtrs shown instead of months Answer Be…
How to do an exact match on multiple columns and show with the latest date?
I want to only show one occurrences of an exact match of TempString1, TempString2, and TempString3 with the latest date. How can I do an exact match of three fields and only display the row that has the latest date? Table OUTPUT: Answer You can try this using ROW_NUMBER() function. The ROW_NUMBER() is a windo…