Windowed Functions in SQL Server -
I have a table named order
in which the data looks like this:
EMPId order value order1d1 100 1 2 167 89 .... There are many orders for each empID what I want to get output as thisEmpid Rank VALUETOTAL VALUETHISEMPID 1 1 300 100 4 2 300 50 \ ...If there are many amps, it should be given the same rank with the same value and API id.
I tried
SEP, SUM (val) OVER () as VALUETOTAL, SUM (val) over (Partition by EmpID)
How can I get the RAM and how can I get it by ValueTheEmpID?
First test data: enter #t values (1, 10, 100) in #t values Insert (# 1, 20, 101) into #t values (2, 30, 120) into #t values (3, 10, 130), insert in #t values (3, 10.5, 131) into #t values ( 4, 100, 140)
You need two steps, one to get empIds and their corresponding sequence values. Step two has to get total total and rank:
; With Step 1 (EmpId, ValueTheEmpId) (select empId, sum (OrderValue) from #t group by empId) Value as EmpId, rank () value (ValueThisEmpId desc) as "rank", value (valueTHisEmpId) ) Choose Value as OverTotal, Step 1 from ValueTheEmpId
This output will:
4 1 180.50 100.00 1 2 180.50 30.00 2 2 180.50 30.00 3 4 180.50 20.50 If you do not want to interval in ranking, then use the dense rank: ; With the value of "Rank" by EmpId, dense_rank () value (ValueThisEmpId desc), with the value (Order empId, empId from #t group, by EmpId, ValueTheEmpId), the total value As (valueTHisEmpId), step 1 from ValueTheEmpId
Comments
Post a Comment