SQL Server : Update WHERE xxx AND most recent record - sql-server

Similar to this question, but I require this in Microsoft SQL Server.
I'm looking to update values in my database, but only the most recently added value for each date.
For example if I have 4 rows where column_day = '2023-02-01',
and they have updated datetimes in a column [Time_Stamp] of
2023-01-01, 2023-01-27, 2023-01-10, and 2023-01-05,
I would like to only update the Jan 27, 2023 2023-01-27 line.
The answer provided here which does not work in Microsoft SQL Server is:
UPDATE your_table
SET some_column = 1
ORDER BY date_time_column DESC
LIMIT 1
My code I'm trying is slightly different as it includes a WHERE:
I get an error
Incorrect syntax near the keyword 'order'
UPDATE your_table
SET some_column = 1
WHERE column_tag = 'test' AND column_day = '2023-02-01'
ORDER BY Time_Stamp DESC
LIMIT 1
How can I achieve this in Microsoft SQL Server?

You can use TOP in an UPDATE, however, you can't provide an ORDER BY; as such it's really used for batching and previously UPDATEd rows are filtered in each iteration in the WHERE clause.
For what you want, one method would be to use an UPDATEable CTE with ROW_NUMBER:
WITH CTE AS(
SELECT SomeColumn,
ROW_NUMBER() OVER (ORDER BY date_time_column DESC) AS RN
FROM dbo.YourTable
WHERE ColumnTag = 'Test'
AND ColumnDay = '20230201')
UPDATE CTE
SET SomeColumn = 1
WHERE RN = 1;

Related

SQL duplicates with percentage result

I have an issue where my database contains a table with these columns:
Program_ID, Vehicle_VIN, Vehicle_Type
I need to create a report where will be:
Program_ID, AmountOfAllPoliciesInProgram, PercentageDuplicatesInProgram
where Vehicle_type = 10
and criteria for the duplicate is to have Vehicle_VIN more than 1 unique time in the table for dedicated Program_ID.
It's in Microsoft SQL Server Management Studio
AmountOfAllPoliciesInProgram is:
SELECT
PROGRAM_ID, COUNT(*) AS AmountOfAllPoliciesInProgram
FROM
dbo.table
WHERE
Vehicle_type = 10
GROUP BY
PROGRAM_ID
I'm not 100% sure I understand you correctly, but you can count distinct Vehicle_VIN and use the having clause to test if the count of distinct Vehicle_VIN is larger than 1. Like so,
SELECT PROGRAM_ID, COUNT(*) as AmountOfAllPoliciesInProgram, count(distinct Vehicle_VIN) as VIN_COUNT
FROM dbo.table
where Vehicle_type = 10
group by PROGRAM_ID
having count(distinct Vehicle_VIN)>1

How to calculate the days between 2 dates from 2 successive IDs

I need some help to create a new column in a database in SQL Server 2008.
I have the following data table
Please have a look at a snapshot of my table
Table
In the blank column I would like to put the difference between the current status date and the next status' date. And for the last ID_Status for each ID_Ticket I would like to have the difference between now date and it's date !
I hope that you got an idea about my problem.
Please share if you have any ideas about how to do .
Many thanks
kind regards
You didn't specify your RDBMS, so I'll post an answer for both since they are almost identical :
SQL-Server :
SELECT ss.id_ticket,ss.id_status,ss.date_status,
DATEDIFF(day,ss.date_status,ss.coalesce(ss.next_date,GETDATE())) as diffStatus
FROM (
SELECT t.*,
(SELECT TOP 1 s.date_status FROM YourTable s
WHERE t.id_ticket = s.id_ticket and s.date_status > t.date_status
ORDER BY s.date_status ASC) as next_date)
FROM YourTable t) ss
MySQL :
SELECT ss.id_ticket,ss.id_status,ss.date_status,
DATEDIFF(ss.date_status,ss.coalesce(ss.next_date,now())) as diffStatus
FROM (
SELECT t.*,
(SELECT s.date_status FROM YourTable s
WHERE t.id_ticket = s.id_ticket and s.date_status > t.date_status
ORDER BY s.date_status ASC limit 1) as next_date)
FROM YourTable t) ss
This basically first use a correlated sub query to bring the next date using limit/top , and then wrap it with another select to calculate the difference between them using DATEDIFF().
Basically it can be done without the wrapping query, but it won't be readable since the correlated query will be inside the DATEDIFF() function, so I prefer this way.

SQL Server : error which works well in Oracle

I'm migrating from an Oracle database to SQL Server 2012. Some SQL which works well in Oracle doesn't work with SQL Server.
The following is my SQL and the error.
SELECT
SUM(COUNT(DISTINCT dfc.rentalNumber))
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
GROUP BY
dfc.rentalNumber
and the error is
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
Remove the SUM from the query, as it has no use, since you are also using GROUP BY. The results will be the same as your original query.
SELECT
COUNT(DISTINCT dfc.rentalNumber)
FROM DueFromClient dfc
WHERE dfc.facilityId = facilityId
AND dfc.isRentalComponent = 1
GROUP BY dfc.rentalNumber
This doesn't make much sense, so I suggest also adding the rentalNumber to the SELECT in order to make sense of your data and to also make full use of the GROUP BY.
SELECT
COUNT(DISTINCT dfc.rentalNumber)
, dfc.rentalNumber
FROM DueFromClient dfc
WHERE dfc.facilityId = facilityId
AND dfc.isRentalComponent = 1
GROUP BY dfc.rentalNumber
You don't need to sum count and group by. Try this:
SELECT
COUNT(DISTINCT dfc.rentalNumber)
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
sqlfiddle for this query
All of proposed below are suboptimal and returns same result as query above, but they may meet needs of #Sachi Pj with using of same construction as in original query with SUM(COUNT(DISTINCT()) two more options:
SELECT SUM(dfc2.rentalNumber)
FROM
(
SELECT
COUNT(DISTINCT dfc.rentalNumber) rentalNumber
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
) AS dfc2
GROUP BY dfc2.rentalNumber
sqlfiddle for this query
And without GROUP BY since doubles was eliminated by distinct:
SELECT SUM(dfc2.rentalNumber)
FROM
(
SELECT
COUNT(DISTINCT dfc.rentalNumber) rentalNumber
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
) AS dfc2
sqlfiddle for this query
You can compare with your original query sqlfiddle to being sure what the results are same.

Replace Group By clause with any other clause

In below query, I am using GROUP BY clause to get list of recently updated records depends on updated date. But I would like to have the query without a GROUP BY clause because of some internal reasons. Can please any one help me to solve this.
SELECT Proj_UpdatedDate,
Proj_UpdatedBy
FROM ProjectProgress PP
WHERE Proj_UpdatedDate IN (SELECT MAX(Proj_UpdatedDate)
FROM ProjectProgress
GROUP BY
Proj_ProjectID)
ORDER BY
Proj_ProjectID
Using TOP 1 should give you the same result assuming you meant the MAX(Proj_UpdatedDate):
SELECT Proj_UpdatedDate,
Proj_UpdatedBy
FROM ProjectProgress PP
WHERE Proj_UpdatedDate IN (SELECT TOP 1 Proj_UpdatedDate
FROM ProjectProgress
ORDER BY Proj_UpdatedDate DESC)
ORDER BY
Proj_ProjectID
However your query actually returns multiple dates since it's GROUPED BY Proj_ProjectId (the max date for each project). Is that your desired outcome - to show a list of dates that the projects were updated and by whom?
If so, try using ROW_NUMBER():
SELECT Proj_UpdatedDate, Proj_UpdatedBy
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY Proj_ProjectID ORDER BY Proj_UpdatedBy DESC) rn,
Proj_UpdatedDate,
Proj_UpdatedBy
FROM ProjectProgress
) t
WHERE rn = 1
And here is the SQL Fiddle. This assumes you are running SQL Server 2005 or greater.
Good luck.

How to create RowNum column in SQL Server?

In Oracle we have "rownum".
What can I do in SQL Server?
In SQL Server 2005 (and 2008) you can use the ROW_NUMBER function, coupled with the OVER clause to determine the order in which the rows should be counted.
Update
Hmm. I don't actually know what the Oracle version does. If it's giving you a unique number per row (across the entire table), then I'm not sure there's a way to do that in SQL Server. SQL Server's ROW_NUMBER() only works for the rows returned in the current query.
If you have an id column, you can do this:
select a.*,
(select count(*) from mytable b where b.id <= a.id) as rownum
from mytable a
order by id;
Of course, this only works where you're able to order rownums in the same (or opposite) order as the order of the ids.
If you're selecting a proper subset of rows, of course you need to apply the same predicate to the whole select and to the subquery:
select a.*,
(select count(*) from table b where b.id <= a.id and b.foo = 'X') as rownum
from table a where a.foo = 'X'
order by id;
Obviously, this is not particularly efficient.
Based on my understanding, you'd need to use ranking functions and/or the TOP clause. The SQL Server features are specific, the Oracle one combines the 2 concepts.
The ranking function is simple: here is why you'd use TOP.
Note: you can't WHERE on ROWNUMBER directly...
'Orable:
select
column_1, column_2
from
table_1, table_2
where
field_3 = 'some value'
and rownum < 5
--MSSQL:
select top 4
column_1, column_2
from
table_1, table_2
where
field_3 = 'some value'

Resources