I’m trying to get the sum of production per month and pivot the month row to columns. Could you please tell me what is wrong with this code?’ Answer There were several items wrong in your query. Columns need aliases and your max() within the PIVOT was referencing an invalid column name
Tag: tsql
Returning non-overlapping records within a date range
I have the following data. I have looked over a lot of threads about overlapping and non-overlapping dates but none seemed to help me. From this table with a SQL query I want to return the first record out of overlapping dates or basically I have been struggling a lot with this query and was wondering if this is actually
Problem with creating a SQL view with multiple joins
I am trying to create a view which will be a base for Excel export in API. Basically, what it contains is information about particular projects. To said projects calculations can be added (it all happens on the form on frontend). Those calculations are called EBIT, EBIT+ and OVI. User can add either one, two or all of them, so
How can I join two tables on an ID and a DATE RANGE in SQL
I have 2 query result tables containing records for different assessments. There are RAssessments and NAssessments which make up a complete review. The aim is to eventually determine which reviews were completed. I would like to join the two tables on the ID, and on the date, HOWEVER the date each assessment is completed on may not be identical and
Get MIN value between 3 columns on same row
Though I have come up with a couple of working solutions to what I am looking for, I am wondering if there is a more streamlined way of determining which of 3 columns from a single row contains the smallest/min value. Below is an example of the data I am working with: AccountNumber Job DaysSinceLastSale DaysSinceLastCharge DaysSinceEstablished YO502 NULL NULL
Looping through a column to check if a cast succeeds in SQL Server
I am trying to check if an entire column which is varchar and make sure it can be cast to float. I have a cursor portion like so: For some reason, it is always returning 1. I already in a previous part of the cursor (not shown but above), verified that the column passed in (@ColumnName) is NOT NULL at
How do I do multiple CASE WHEN, using LEAD() to look at an entire row in SQL?
I have a set of data that I need to manually adjust a column (inserting 0) for the row trailing a specific row, based on a record in one specific column. For example: My desired output: TYPE ObjectID DATE AMT1 AMT2 AMT3 AMT4 TOTAL AMT C 1234 3/1 2 0 2 2 4 A 1234 3/2 5 0 0 0
Return all values from objects using OPENJSON()
Here is my problem: My JSON may vary based on API response and there is no guarantee that “shortTitle”, for example, can be present in my JSON next time when there is a post request. I have to figure how to “loop” (if that’s the correct term) through JSON to get all fieldID and contents without losing any data. Here
Using JSON_VALUE for parse column in SQL Server table
I have never worked with JSON in SQL Server before that’s why need some help. I have written a simple snippet of code: In sample above I write code how get first “id” from “tags”. But how looks like script if in “tags” not 2 “id”, but an unknown number this “id” and result should be in column like this:
How can I view an earlier version of a table on a T-SQL server?
I’ve just done something very silly. In both the productive and development environments on our SSMS server, I’ve changed the wrong column in one table. I know exactly what rows have been changed, but not what their original values were. I did not use a transaction. Is there any way to view the history of the table or see a