Skip to content

Tag: sql-server

SQL lag function avoid getting first row as null

I have a sql query if my filter date is > 20210625 , I get value as 27 and previous value as null , is it possible to get the prev_value 25. If there is no filter I will get it. But filter is required to prevent reading the whole table. Also one more requirement. If previous value is null

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 …

Getting empty result from a query

I have Client table ClientGuid Email Phone Also have Activity table ActivityTitle Date ClientGuid If client does some “Activity” it is saved in Activity table. I want to get Emails of clients who did not have any activity in last three months and had activities before. Tried this : Getting empty r…

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…

Power BI Report Builder Indicator Formula

I am adding in an indicator to a PBI Report Builder Report. The indicator is based off multiple fields from the dataset so I need to use a formula, to create the three up/down/side arrows. Previously in Crystal Reports this could be implemented using a series of IF statements as follows. The below example is …

Select last value in each group

In database I have two table. I want to join two tables and get last row each SensorType Sensors table: Id CodeNumber PortNumber SensorType IsActive 13 Dht11 3 0 1 14 Dht11 3 2 1 17 Global 100 4 1 18 Dht11 3 1 1 19 Dht11 3 3 1 SensorsValue table (SensorId is foreign key Sensors table): Id SensorId