I have two columns in a table and I want a query to fetch First column values based on distinct values from Second column. Since, There are multiple combinations of First and second column I would like to have the first match from each combination. DATASET: First Column Second Column A abc B abc C abc D abc F…
Tag: ssms
SQL help – Trying to tag values which complement the same number with opposite sign
I am trying to flag the numbers in a column which has both positive and negative numbers and adds to zero if summed up. I would like to do this by partitioning one table into multiple chunks based on Account column in the table. I have posted a picture to further explain how my result (Flag column) is expecte…
Bring a row for each specific column that is not empty, with the column name
I’ve this table with the following data: ID Data1 Data2 Data3 Data4 Data5 Data6 RandomInformation 1 Test1 Test2 Test3 Information1 2 Test5 Test6 Test7 Information2 3 Test8 Test9 Test10 Test11 Information3 The resultant table i want should be as below: ID Data RandomInformation 1 Data1 Information1 1 Dat…
Flag items not having the specified combination in a column
I need to flag ID#s not having a specified combinations of DC and Storage Locations. For example: Articles must have the following combinations for each DC: DC Storage Location DC01 ABC, BCA, DCA DC02 ABC, BCA DC03 ABC, DCA My desired outcome would be the below as I’d like to show the Storage Location m…
SQL Server syntax correction [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 9 months ago. Improve this question I am trying to create a SQL Server stored procedure but I am getting…
Include Column name in SQL Unpivot
I have an unnormalized data set I need to normalize for reporting purposes. I successfully unpivoted the data using this: How can unpivot column headers Question1, Question2, Question3, Question4 as a third column in my unpivot query, like this? Answer Your syntax looks like SQL Server. If so, just use a APPL…
How to assign the IDs to the referring table and how to display this correctly? (SSMS)
I am in the process of creating an audit plan using ERD, going off the below image you can see that there’s a permissions table with four FK columns referring to the other four tables PK column. I am just confused as to how the IDs will relate to the other tables and how will it show up correctly in
Creating a SQL Loop to Change the Table Name
I am needing to change the ending number of the table name in a loop to go from 1 through 12. This is a more straight forward example of my actual problem, but if this can be solved, then I can duplicate the results later on (Actual problem is using the past 12 months on a rolling basis for a
Update Text Field on Multiple Rows Using Current Text In Field SQL
I have a table with a text field that imported with incorrect characters delimited by a space. Expected Output I need to run an update to remove all the text AFTER the space on a specific number of rows. I figured out a way to select the correct text, but I’m stuck on how to update in bulk. Will return
Pass Minus Values for a SP
I need to pass a value for a SP by multiply it by -1. This is my query and I want pass the quantity by multiply it by -1. Is there any way to do this? Answer change your fist query to negate the sign @Quantity = -qty OR multiply it by -1