Skip to content
Advertisement

Tag: tsql

Get max date based on another column in SQL

I have a table variable which consists of columns id, date and status as shown below And follows is the sample data Also declared a variable as shown below. From this table I need to get the row which containing the maximum value of date( MAX(dates) ) where status is ‘ABC’, from the above sample values, I should get the

SQL SELECT “pair” records

There are cities, suppliers, and customers tables. Each supplier, (as well as customer), is located in one and only one city. How could I select pairs of supplier and customer within the city, where each of them is mentioned only once? P.S. if there is no “pair” NULL should be instead. E.g.: cityId supplierId customerId 1 1 1 1 2

Pivot data T-SQL

I have the following table: I want to pivot it to the following table: I tried to work with the following example: https://www.sqlshack.com/dynamic-pivot-tables-in-sql-server/ But in this case a SUM operator is mandatory. I don’t want to use the SUM operator for my data. What would be the best approach to go. Eventually I want to use the data again, I

TSQL – join two tables with TotalCounter and Pagination

I’m trying to join 2 tables (one to many relation) that include all the columns from first table and only the number of rows from the second one. Use case: one Service has many Reviews. The query looks like: The error is I need to include either average or group by clause but how would that look like I can’t

Flag “yes/No” if the subsequent row has same ID

I have data like this. If the same id is present in the next row, I want to flag as Yes. If it is not present then Make it as ‘No’. Can you kindly help me with the query? Thanks Answer The problem with multiple rows for the same ID and no other column that can be used to futher

Advertisement