This was an old SQL Server 2008 Express database (five of them actually) that I just migrated to SQL Server 2019 Express. Everything seemed to be working fine until my crew got in and we were getting an error everywhere. Turns out we had RAISEERROR in the triggers, and even though my compatibility appears to …
Tag: sql-server
Updating Status when there is a duplicate
How can I update my status to 1 if the condition is same Reference, but 1 reference should be Debit and 1 credit. If they are 3 records with same Reference but 2 with same debit amount and 1 credit 2 records should be updated to 1 that is 1 with credit and 1 with Debit using SQL Answer Looks
How to print NULL when table has no rows in SQL Server?
I have a table with a named column but no rows. I wanted to write a select similar to: I wanted to get a result like: But instead I get just the Col1 name and no results. How can I get a table with NULL as the result? Answer You can use aggregation: An aggregation query with no group by
Expand Col values based on names/email SQL
I want to explode my table based on their ‘KEY’s and assort their names and emails separately into individual cols. In Python such scenarios are dealt easily(like we use explode in pandas). since I’ve just started with SQL I’m finding it difficult to get my feet wet. My googling didn&#…
Get the count of records from another table
I have a view that looks like this: which is defined using this SQL statement: Now I want to count the records for another table (TABLE 2) based on the C_CODE, P_CODE and PH_CODE and display it as a column TOTAL in my view. How can I do this? Answer one way is to use subquery:
Return records that only have a certain status from a select query that includes multiple joins
I have a query: That returns records such as these: I want to be able to only return records that only have ‘I’ values under Inv_Disp where the Order_Num may or may not be the same. But where if an Order_Num Inv_Disp contains an ‘I’ AND some other value such as ‘CH’ or R…
SUM with Multiple Conditions
I’m writing a query on SQL Server and have hit a wall on cumulative sum with multiple conditions. I need to sum all the purchases in a day made by a user, I can do it separately, sum purchases by day(Daily_Total), or sum purchases by user(User_Total), but I run into a wall when I need both conditions me…
Compare 3 SQL Server tables and once matched based on some attribute put the result on one table when not matched put the result in another
I have the simple requirement, there are the 3 SQL Server tables like this: Table 1; columns Table 2; columns Table 3; columns I wish to join (inner joins) based on Name, DOB, Gender & PostCode across all the tables, if the record matches I would like to put the result in one table and the remaining recor…
T-SQL Trigger – Audit Column Change
Given a simple table, with an ID what is the correct way to audit a column being changed. I am asking after looking after various answers which seem not to be working. Here is what I have: Create …
sql that finds records within 3 days of a condition being met
I am trying to find all records that exist within a date range prior to an event occurring. In my table below, I want to pull all records that are 3 days or less from when the switch field changes from 0 to 1, ordered by date, partitioned by product. My solution does not work, it includes the first record