I have this recommendation as a missing index on a table for a stored procedure that is ran from the script tool sp_BlitzCache. However I have an index on the columns TeamId, PlayerId, and Active already. Shouldn’t this be used instead and not duplicate indexes with the same columns? The query must be t…
Tag: sql-server
What is wrong with my implementation of this method to calculate a person’s age?
I want to calculate age using the method suggested here, format the date to yyyymmdd and subtract the date of birth (also yyyymmdd) from the current date then drop the last 4 digits you’ve got the age I tried this. But it returns me the same number 20210428 Answer You’re very close, but there̵…
SQL Subquery within a subquery [closed]
I am writing a nested SQL where I need to have a subquery within a subquery Let’s say I am trying to retrieve a row from ORDERS that matches MAIN_ID from SOURCE that matches MODULE from MODULES. …
Dynamic SQL to Unpivot Data using Cross Apply with Multiple Columns
Microsoft SQL Server Management Studio v18.8 I have a table that will have varying columns and column names. I need to unpivot the data so I can then eventually store it into a different table. Unfortunately, this is the process since the original table is being pulled from a Google Sheet. I looked up several…
Calculate cumulative product value
I have the following database table: Date Return Index 01-01-2020 0.1 Null 01-02-2020 0.2 Null 01-03-2020 0.3 Null I would like to update the Index value using the following …
Apply multiple count conditionally in SQL Server query
I have a task table which contains some DateTime type columns like completed, pending, exception. It joins user table and I want to bring userName, AllTaskCount, AllPendingTaskCount, …
Calculate cumulative value in update query using SQL
I have the following database table: Date Return Index 01-01-2020 0.1 Null 01-02-2020 0.2 Null 01-03-2020 0.3 Null I would like to update the Index value using the following …
Select most recent record (with expiration date)
Let’s say that we have 2 tables named Records and Opportunities: Records: RecordID CustomerID CreateDate 777 1 1/1/2021 888 2 1/1/2021 999 1 2/1/2021 Opportunities: OppID CustomerID OppCreateDate 10 1 12/31/2020 11 1 1/10/2021 12 2 2/1/2021 13 1 4/1/2021 14 1 8/5/2025 Desired Output: RecordID CustomerID…
Grouping rows based on a date field in SQL Server
I have the following table exams.sql with exam scores in SQL Server. It saves the student’s ID, their score, and the test date. The problem begins when a student takes the exam two (or more) times. I need the complete list of students with their actual qualification: in case they have been evaluated mor…
SQL Use Value from different column on same Select
I have a question if this is possible which will save me time writing extra code and limits user error. I need to use a value from a column (which has already performed some calulcation) from the same select then do extra calculation on it. I encounter this a lot in my job. I will highlight the problem with a