I’m trying to split a single query into 4 parts, for readability purposes. Having it as a single query, while adequate, isn’t “pretty” for display purposes. An example would be a query that …
Tag: tsql
Determine time duration based on events without using loops
I have a table with timestamps of 5 different types of events (start, stopped, restart, aborted, and completed). The given table looks like this: Time EventID Event 7:38:20 1 start 7:40:20 2 stopped 7:48:20 3 restart 7:50:20 4 aborted 8:00:20 1 start 8:40:20 5 completed 8:58:20 1 start 9:00:15 4 aborted I wou…
Syntax error when using input parameter to create procedure
I’m getting this error Msg 103010, Level 16, State 1, Line 1 Parse error at line: 33, column: 15: Incorrect syntax near ‘@sas_token’ For reference here are the guides I am following: https://learn.microsoft.com/en-us/azure/synapse-analytics/sql-data-warehouse/quickstart-bulk-load-copy-tsql-e…
If a column is not null then convert into a row for multiple columns
I have a table like below where I looking to transpose col4 col5 and col6 into rows but in a specific pattern that I listed below col1 col2 col3 col4 col5 col6 a b c 500 200 w x y 1000 300 z g h 200 600 I want to convert it to the following col1 col2 col3 col4 col5
tsql – pivot sum of employee
I have here a sample table data which i want to use pivot and make my DedCode data into column. the AMount example is already computed to its total but how can i count the number of employee if it is same with branch,deptcode and emptype. my sample table data. expected output: Answer You can use conditional a…
Variable length substring between two characters
Data looks like this: I want it to look like this: It’s pretty simple to get rid of one or the other. This: Gives me this: Initiative: Credible Sources Initiative: Just in Time Initiative: Database Normalization And this: Gives me this: Having a hard time figuring out how to combine the two. Answer How …
How to calculate the degree of agreement by row comparisons in SQL Server?
For a minimal, reproducible example (reprex) let’s assume I have a database object (dbo) in a Microsoft SQL Server and I want to query things in T-SQL. My dbo looks like this: Animal-ID Marker-ID …
Find items in table with 2 specific sizes
I have items table where the item code repeats as it has different sizes, variants. I want to find items which has 2 specific sizes, ie size in both M/Y and Euro. Items table: Required, I want to query for item id 1 and 3. I was trying with SUM(), CASE but not able to figure it as it involves
Find groups that are missing values of one column
Hopefully someone can send some light on an issue I’m trying to resolve. Types Codes I would like to be able to determine those Code/ID pairs that are missing a particular type. The result of this query should yield. Answer You need conditional aggregation for this: Another option is to cross join the p…
How to loop through JSON array to insert rows in SQL Table using TSQL?
I have following query where I would like to insert data from JSON: INSERT INTO EncExt ( reference ,system ,code ,display ) SELECT JSON_VALUE(record, ‘$.id’) AS reference , JSON_VALUE(…