Skip to content

Tag: tsql

How to add pagination to this dynamic sql

I have the following table with its respective data I wanted to be able to sort the dynamic column example sort by ‘StudentName’ ASC it would sort data using column returning When sorting by StudentName: When sorting by Grade ASC: When sorting by TeacherName ASC (different table) I have manage to …

SQL Query to determine number of tables

I have this table and I’m trying to determine how many Tables (in a restaurant) are awaiting food. The table( in the database) has the following columns: TableNumber, ObjectType, ObjectDescription and Quantity (amongst others). I made this query to see what is ordered, but would like to know how many un…

Filter on Rows that are Within a Certain Timeframe

I have a table of patients that were admitted and discharged from the hospital. I am trying to find any patients that have an admission within 30 days of their previous discharge date. So for example, I have the table: Name Admission Date Discharge Date Patient A 1/1/2021 1/5/2021 Patient A 1/7/2021 1/10/2021…

Select specific columns from the stored procedure

I have a stored procedure in other database which is maintained by other team. Assume that it is currently returning 3 columns, and my system only needs those 3 columns but the other team can add few more columns for their own use which is causing my system to fail. Other database SP And below is my query, wh…

How can I compare rows of two similar tables in SQL?

These TSQL tables are exact but I can’t guarantee that. I cannot guarantee which rows will be non-existent in either table. What I want to be able to do is to subtract one from the other into a new table. For instance, “Agg Asslt” should have a column value for “OffenseCount” of …

How to write sql query to get this result

I had two tables as below: request_id update_from_id sw_ref_number raised_by_user_id raised_date 1 0 1 3 2019-08-29 15:08:16.000 id request_id input_id value is_deleted 21 1 1 00001 0 22 1 2 3 0 75 2 1 00002 0 76 2 2 0 My query is: And after that my result is: request_id sw_ref_number value input_id 1 1 00001…

Pulling out monthly daypart data

I’m dealing with sales data that looks like this Date Time Net Amount Store No Item Category Code 2021-03-21 00:00:00.000 1754-01-01 08:01:14.627 100 001 FOOD 2021-01-31 00:00:00.000 1754-01-01 15:42:21.670 120 002 FOOD 2021-04-24 00:00:00.000 1754-01-01 17:35:30.070 160 002 FOOD 2021-03-14 00:00:00.000…

SQL Subquery as expression

I have the following two tables: I want to be able to return all the claims that are a specific FormType and AdjustmentVersion. Given the following query: How do I correct the syntax to match the AdjustmentVersion on the given integers and the subquery at the same time? Currently, I am getting error ‘Su…