I am trying to create a script to calculate the fill rates for each column in a table Data_table and insert it into a second table Metadata_table. The Data_table has 30 columns in it, and some columns have 100% data in them, and some have less than 100% (due to nulls). My code to calculate the fill rate looks like
Tag: tsql
TSQL-ORDER BY clause in a CTE expression?
Can we use ORDER BY clause in a CTE expression? Error message: Msg 1033, Level 15, State 1, Line 14 The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified. Msg 102, Level 15, State 1, Line 25 Incorrect syntax near ‘,’. Answer You
Getting error while creating View in SQL server
Hi Could any one please help me while creating a view in SQL server toad I’m getting below error.Thanks in Advanced. Error:- SQL Server Database Error: Incorrect syntax near the keyword ‘with’. Answer A CTE must be the first part of the batch, docs are here. Change the body of your view to something like this: EDIT: with added INCIDENT__ID
Sql query to Count Total Consecutive Years from latest year
I have a table Temp: I want to calculate the total consecutive years starting from the most recent Year. Result should look like this: Answer e.g. for ID=1: As long as there’s no gap, both sequences increase the same. Now check for equal sequences and count the rows: Edit: Based on your year zero comment:
Dynamic SQL output of a query to a variable
I would like to output the result of the dynamic SQL into a variable called @Count but not sure what the syntax or even the code should like to accomplish this. The code looks as follows: thank you Answer You can utilize sp_executesql to execute your count() query, and output it @Count. Try this: One last thing: Person.person looks like
TSQL find number of correspondences
I have the following table: Test Data: I need to find out how many times correspondence has been between each pair. For example, Anthony has contacted John 5 times, John has contacted Anthony 5 times. So total correspondence between John and Anthony has been 10 times. Similarly, David has contacted John total of 9 times, John has contacted David 4
How does a recursive correlated expression speed up distinct queries?
I found this post about speeding up distinct queries: Super-fast DISTINCT using a recursive CTE: — 15ms CPU The recursive CTE is 100 times more efficient 🙂 This kind of speedup would be extremely valuable for my current project, but I am not sure in which cases this approach is beneficial. To be honest: I don’t get why this speeds
How to update Sql table from excel directly?
I have an sql database and I am able to connect with excel spreadsheet. But when I update the table from excel directly it’s not updating the database and once I click refresh all the entered data is …
Update statement SQL with CTE
I have a common table expression that I am using trying to use as an update statement. The only reason for the CTE is so I can use a where clause to filter by CredCount. I would like to use that where clause to update only records that match in this case CredCount of 2. However, I am having trouble
How can I refactor the below code to make it faster?
This code is developed by my colleague, and it is really slow. I don’t want to create any temp table. How can I make this query faster? Answer