Skip to content
Advertisement

Tag: sql-server

Counting orders by supplier results in SQL error

I am trying to get how many orders each supplier have, by showing two columns, CompanyName and number of orders. And then Sorting them by descending order. My SQL: But I get the following errors: Msg 145, Level 15, State 1, Line 65 ORDER BY items must appear in the select list if SELECT DISTINCT is specified. Msg 8120, Level

SQL Selecting & Counting From Another Table

I have this query that works excellently and gives me the results I want, however, does anybody know how I can remove any rows that have 0 orders? I am sure it is something simple, I just can’t get my head around it. In other words, should it only show the top 2 rows? ID Company Org Total Orders 1232

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

Sql server Balance Table Update Recursive

I Have a SQL Server database with a table named Balance The table: Id Date IN OUT Balance 3345312 2022-08-07 100 50 250 5435245 2022-08-06 50 50 200 4353451 2022-08-05 0 100 200 5762454 2022-08-04 20 100 300 7634523 2022-08-03 400 100 380 5623456 2022-08-02 100 20 80 4524354 2022-08-01 0 0 0 Id = Unique Identifier Date = Balance

Trying to find the max observation by group

I have the below code where I am trying to find a patients injury level and whether it is between 1 and 5 or 6 and 10. I have used the ‘case when’ clause below. The issue is, some patients have multiple injury levels. Is there a way for me to only get the max injury level and ignore the

Remove overlapping date intervals

I have two tables in my sql server database : PlannedPeriods : containing periods of future events UnavailabledPeriods : list or periods that are not available Here’s an example for structure and data : I would like to remove all unavailable periods from the planned periods in order to obtain this result : Answer In this case you should be

Advertisement