I am trying to create a new table from a percentiles query so I can export it into ARCGIS. I am getting a syntax error, however, that I do not know how to address.
create table tempercent as (select *, percentile_CONT(.95)
WITHIN GROUP (ORDER BY cast(travel_time_minutes as float)) OVER (PARTITION BY TMC_code) AS TTAV_P_80 FROM [dbo].[Andy_I40])
SQL Server does not support CTAs. You could use SELECT ... INTO instead:
SELECT *, PERCENTILE_CONT(.95)
WITHIN GROUP (ORDER BY cast(travel_time_minutes as float))
OVER (PARTITION BY TMC_code) AS TTAV_P_80
INTO tempercent
FROM [dbo].[Andy_I40]
Related
Priority is the output column.
The group contains duplicate content.
how can I fix this using SQL query?.
enter image description here
One way to achieve the desired result with this data is to use Dense_rank() function like below:
select *, dense_rank() over (order by [Group]) as Priority
from tab
order by No
For any value, please try the following
;with cte as
(
select [Group], ROW_NUMBER() over (order by No_min) as rn
from
(
select [Group], min([No]) No_min
from tab
group by [Group]
)t
)
select t.*, x.rn as [Priority]
from cte x
join tab t on t.[Group] = x.[Group]
order by 1
Please find the db<>fiddle here.
How can I get only the last run of each SSIS job using a T-SQL query?
Please check the image. These details are stored in a single Table XYZ. Can anyone help me by providing a T-SQL query?
I am using this query:
select Job_Name, Start_Date, Finish_Time, Remarks
from #XYZ
order by Job_Name
You can use row_number()
select x.*
from (select x.*,
row_number() over (partition by job_name order by try_convert(datetime, Finish_Time) desc) as seq
from #XYZ x
) x
where x.seq = 1;
You can do this by adding a row_number to sort the data and then filtering to where a 1 is returned:
select cols
from(select cols
,row_number() over (partition by Job_Name order by Start_Date desc) as rn
from table
) as t
where rn = 1
I want to use ROW_NUMBER() function and get first and latest values.
I write bellow query. But I got an error.
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
help me to solve the issue. Below the sql query
SELECT *
FROM(
SELECT OPP_ID,PRJ_ID,
ROW_NUMBER() OVER (PARTITION BY OPP_ID ORDER BY MAX(CREATION_DATE) DESC) AS RN
FROM OPPOR
GROUP BY OPP_ID,PRJ_ID
ORDER BY MAX(CREATION_DATE) DESC) OP
WHERE OP.RN = 1
The row_number function can do it's own aggregation and ordering, so no need to use group by or order by in your subquery (order by won't work in subqueries as you've seen). It is a little unclear if you want to partition by opp_id or opp_id and prj_id though. But this should be what you're looking for:
SELECT *
FROM(
SELECT OPP_ID,PRJ_ID,
ROW_NUMBER() OVER (PARTITION BY OPP_ID ORDER BY CREATION_DATE DESC) AS RN
FROM OPPOR
) OP
WHERE OP.RN = 1
I Have a table below and I would like to group the data by Opportunity_Id and pick up the rows with latest CreatedDate for each Opportunity_Id. I use local variable and table type to load in case by case. Is there any other more effienct way like using a set of query only to achieve the same result? Thanks heaps
SELECT *
FROM (
SELECT *,
row_number() over (partition by opportunity_id
order by Created_Date desc) rn
FROM yourTable
) T
WHERE T.rn = 1
I have to create a row_number column ordered by a grouped sum, when using sql:
select Sales.Name, SUM(Sales.Bill) as billsum, ROW_NUMBER() over (order by billsum DESC) as rn
from Sales group by Sales.Name
It reports error because row_number over cannot parse the "billsum" alias, I have to write:
select Sales.Name, SUM(Sales.Bill) as billsum, ROW_NUMBER() over (order by SUM(Sales.Bill) DESC) as rn
from Sales group by Sales.Name
so here I write SUM(Sales.Bill) twice, is there anyway to use the alias here?
The MSDN docs for the T-SQL OVER clause say:
value_expression cannot refer to expressions or aliases in the select list.
As already stated out by other member you either have to use CTE or SubQuery.
Not only Row_Number() function but in tsql you can not reference alias in same query, so either you have to use one of the mentioned way above or the expression you used in your post. I hope it makes sense!! :)
Possible work-arounds are to use CTE or a subquery:
SELECT Name, billsum, ROW_NUMBER() OVER (ORDER BY billsum DESC) AS rn
FROM
( SELECT Sales.Name, SUM(Sales.Bill) AS billsum
FROM Sales
GROUP BY Sales.Name
) tmp
-- Reorder after cutting out qty = 0.
SELECT *,ROW_NUMBER() OVER (partition by claimno ORDER BY itemno) as 'alias name'
from dbo.OrderCol
where QTY <> 0