Skip to content

Tag: sql-server

Difference between dm_exec_sql_text vs dm_exec_input_buffer

As per Microsoft docs, dm_exec_sql_text returns the text of the SQL batch that is identified by the specified sql_handle while dm_exec_input_buffer returns information about statements submitted to an instance of SQL Server. I used DBCC INPUTBUFFER for getting the last executed SQL. But it seems both dm_exec_sql_text and dm_exec_input_buffer are replacement DBCC INPUTBUFFER. If this is the case then what

Exclude Records Based on Criteria

I need to exclude the record: If a customer bought “Shoe” and “Hat” on the same day, exclude the record with “Hat”. If a customer bought “Shoe” and “Cloth” on the same day, exclude the record with “Cloth”. Here is the sample data: Here are the expected results: Answer Self join to your shoe criteria and make sure it is

SQL case expression divide values for percentage

I’m trying to divide two values to get the percentage. I need the ‘VA’ AUM / ‘TOTAL’ AUM to get the percentage. Below are the two different methods that I have tried, but they are not providing the percentage. The first method only provides ‘NULL’. How do I take the product of ‘VA’ and the AUM field value and divide

Consolidate Rows with SQL

Trying to consolidate test results so that each test is in one row. Database I am pulling from has an answer result per row. Sample Data: Student Name Test Name Question Attempt Answered Correctly (0 = no, 1= yes) Mary Intro Assessment 1 1 Mary Intro Assessment 1 0 Mary Intro Assessment 1 1 Joseph Intro Assessment 1 1 Joseph

Combine two large multi-JOIN and GROUP BY queries

So I have one query that is performing one GROUP BY that requires a lot of joining to get it to perform aggregation of a row count basically Then there is a similarly structured query but performing a different aggregation on a row count. How would you nest the second query into the first query so that the final columns

Print within the SQL Server Select query based on the condition

I have this query from the link here which works fine, only that I am adding include FK constraints ‘ON Delete/ Update cascade’ if delete/update_referential_action is 1. I am unable to achieve the desired result. Any help appreciated. Answer Ms Sql 2016 dose not support if statement in middle of query. You must use case statement instead of it. you


I want to replace some OUTER APPLYs in my SQL because they seem to be a little bit slow and eating resources () on a poor VPS. I have no idea what to use instead? LEFT OUTER JOIN (??) Here’s my code The query processor ran out of internal resources and could not produce a query plan. This is a