I have a table which contains Job, City and PersonName (who works in that city). I want to return all jobs and cities even if there is no one working in that city. This is a simple example : Creation of the table : My Query My result But I would like to have : I have no clue how
Tag: sql-server
Create custom field by subtracting column values from two different rows based on multiple IDs
I have to create custom column by subtracting column values from two different rows based on multiple IDs. Data set is like below: ID2 is different for each date. Now I have to display custom field like below: Repeated for other ID2 also. Please suggest how should I proceed. I tried using Cross Join but but n…
Add check constraint related to other column
My table structure is as below I want to add a constraint to this table where only if IsActive=1, GoSequence value can be change. If IsActive=0, GoSequence stays 0. Any help is appreciated. Thanks! Answer Probably the simplest way of expressing that is that either GoSequence must be 0 or IsActive must be 1. S…
Update table value from a second table by joining a third table
I have 3 tables, TableA, TableB and TableC I want to migrate the data from TableA column DateA to TableB column DateB, only if the date field is null in table B (I don’t want to overwrite any …
Convert rows to columns using PIVOT
I have a table named AmountList in SQL Server : where Primary key is ” UniqueCol1, UniqueCol2, AmountID ” and on the basis of AmountID I want to Pivot the results as: I am facing the challenge using Pivot because only Aggregate functions can be used but I need actual values of the fields. UPDATE: …
Return the second newst record of a group sql
I have a table like the following: how can I get this: the result should be the second newest record of a coworker if there’s more than one, or the only present Thanks for any help! Answer Try the following with window function row_number. Here is the demo. Output:
I want row data of same id in one single row
The Table Format is like I want to result as following Answer first concate two columns and then do string_agg. Here is the demo. output: If you are using older version of SQL Server then try the following
SQL Subquery with aggregate function – seeking alternative
My table (Table1) I did a sort of Pivot and it’s fine: http://sqlfiddle.com/#!18/45b9d/1/0 Now I need to calculate the difference between measures, to get something like this: But if I don’t use an aggregate, the result brings me null cells – http://sqlfiddle.com/#!18/45b9d/5/0 And if I do u…
CTE Recursive Query to Grandparents
I have following table: What I need to get is SELECT query that will return only rows that have ISDELETED 0 and ISINEDIT 0 and whose parents or grandparents are 0 as well I currently have: But for some reason it returns double rows Answer You need to add the same isdeleted = 0 AND isinedit = 0 predicate to
Update column data using SQL query
In our SQL Server database, we are trying to update the old column data in a table. In a table there is lot of data similar to Agency Administrator. This is data is in old format where we were taking single values in a column. Now, due to an update in data structure, we are storing the data as [“Agency