Get MAX value from a TOP number of records - sql

I have the following MS SQL Query
select top 10 RegisterTime
from orderNotifications
order by RegisterTime asc
How can I get the max RegisterTime of that query ?
I have tried this
select max(RegisterTime) in (
select top 10 RegisterTime
from orderNotifications
order by RegisterTime asc
)
But I am getting
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'in'.
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the
keyword 'order'.

Make your TOP 10 query a subquery:
SELECT MAX(RegisterTime)
FROM (SELECT TOP 10 RegisterTime
FROM orderNotifications
ORDER BY RegisterTime
)sub

Related

Select column based on another column value

I have a query that I would like to select a different column based on a value billback_id. If billback_id = 3 then I want to select bhh.ctv otherwise I want bsih.total. Is this possible in a join?
select
bsih.invoice_amount,
case when bhh.billback_id = 3 then bhh.ctv else bsih.total end as bsih.total
from [movement].[dbo].[billback_header_history] as bhh
left join
(select invoice_number, invoice_extension,store_number, sum(ctv) as total
from [movement].[dbo].[bsih]
where ctv <> 0
group by invoice_number, invoice_extension, store_number
) as bsih
on bhh.invoice_number = bsih.invoice_number
where last_movement_update between '2022/01/08' and '2022/01/14'
error
Msg 156, Level 15, State 1, Line 111 Incorrect syntax near the keyword
'if'. Msg 156, Level 15, State 1, Line 111 Incorrect syntax near the
keyword 'then'. Msg 156, Level 15, State 1, Line 119 Incorrect syntax
near the keyword 'as'.
In SQL, you can use case (instead of if) in the select.
The statement is:
case when {condition1} then {result1} when {condition2} then {result2} else {result3} end.
So, we could have:
select
bsih.invoice_amount,
case when bhh.billback_id = 3 then bhh.ctv
else bhih.total end as col2
from [movement].[dbo].[billback_header_history] as bhh
left join (
select invoice_number, invoice_extension,store_number, sum(ctv) as total
from [movement].[dbo].[bsih]
where ctv <> 0
group by invoice_number, invoice_extension, store_number
) as bsih on bhh.invoice_number = bsih.invoice_number
where last_movement_update between '2022/01/08' and '2022/01/14'

how do I fix this coding issue?

WITH nl_sentcount
AS(
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
Why the error shows as
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
You need a select statement after the WITH to do something:
WITH nl_sentcount AS (
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
select *
from nl_sentcount;

Select top n after data operations SQL

I want to
Filter a data set
Take distinct of a column
take out top 10 rows of that data set with the distinct column
The code I am using is
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
)
;
and the error I am getting is
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Where am I going wrong?
You have to give a name to the subquery:
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
) AS my_subquery
ORDER BY businessdata
;
make sure you also an ORDER BY to correctly set the order and thus make TOP meaningful

SQL Update field Query error

I receive the following error message when trying to setup a simple query to update any '0's to '1's.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'UPDATE'.
Msg 156, Level 15, State 1, Line 17
Incorrect syntax near the keyword 'AS'.
SELECT *
FROM
UPDATE Table Policies_with_claims
SET Policies_with_claims.Claim_Count=1
FROM
(
SELECT DISTINCT [Sheenal_Policy_Data].[PolicyIdentifier],
COUNT([Sheenal_Claims_Data].[PolicyIdentifier]) AS Claim_Count
FROM (
[Medical Malpractice].[dbo].[Sheenal Policy Data] AS Sheenal_Policy_Data
LEFT JOIN [Medical Malpractice].[dbo].[Sheenal Claims Data] AS Sheenal_Claims_Data ON
(Sheenal_Policy_Data.PolicyIdentifier = Sheenal_Claims_Data.PolicyIdentifier)
)
GROUP BY [Sheenal_Policy_Data].[PolicyIdentifier]
) AS Policies_with_Claims
WHERE Policies_with_claims.Claim_Count=0

Howto query average of most recent data in sqlserver

I want to find the average of the most recent x results. Is it possible to use an aggregate function on a limited number of results with sql server?
Here is what I have tried and the errors I get:
select avg(top(100) convert(float, AnalysisData))
from tab
order by DatePerformed desc
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword
'top'.
select AVG(data)
from (
select top(100) convert(float, AnalysisData) As data
from tab
order by DatePerformed desc
);
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
This is sql server 2008
In your second example, you need an alias name for the subquery:
select AVG(data)
from (
select top 100 convert(float, AnalysisData) As data
from tab
order by DatePerformed desc
) Top100Records;
You probably don't need the parenthesis around 100 either.