Skip to content

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:

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…