This is one of my views called xyz:
ID | NAME |......Other Data... | Marks
1 A 100
1 A 100
1 A 99
1 A 95
1 A 94
1 A 94
1 A 94
1 A 91
1 A 87
1 A 86
2 B 100
2 B 94
2 B 93
2 B 90
2 B 89
2 B 89
2 B 87
2 B 86
3 C 100
3 C 98
3 C 98
3 C 97
3 C 92
3 C 91
3 C 90
The query I used to get this is something like this:
create or replace view xyz as
select *
from abc
where id in
(select id
from data)
order by id, mark desc, id;
And based on unique ids I want the top 5 rows:
ID | NAME |......Other Data... | Marks
1 A 100
1 A 100
1 A 99
1 A 95
1 A 94
2 B 100
2 B 94
2 B 93
2 B 90
2 B 89
3 C 100
3 C 98
3 C 98
3 C 97
3 C 92
I tried referring to this:
MySQL: Select top 5 rows based on ID and find Subtotal
But I wasn't able to do it. Could you please help?
You can use a windowed rank(), partitioned by each student name, and ordered by marks descending, to find the top 5 marks for each Student:
WITH cteRankedMarks AS
(
SELECT "ID", "NAME", "Marks",
rank() OVER (PARTITION BY "NAME"
ORDER BY "Marks" DESC) AS rank
FROM MyTable
)
SELECT "ID", "NAME", "Marks"
FROM cteRankedMarks
WHERE rank <= 5
ORDER BY "NAME", "Marks" DESC;
SqlFiddle here
Notes
If two or more marks have the same value vying for 5th place, then , then rank will return all such marks. If you don't want ties, then use row_number() instead of rank(). More about these here
Obviously, if a student doesn't have 5 marks, then fewer rows will be returned.
Related
I'm looking for help with calculating the difference between consecutive ordered rows within groups in SQL (Microsoft SQL server).
I have a table like this:
ID School_ID Enrollment_Start_Date Order
1 56 1/1/2018 10
1 56 5/5/2018 24
1 56 7/7/2018 35
1 103 4/4/2019 26
1 103 3/3/2019 19
I want to calculate the difference between Order, group by ID, School_ID, and order by Enrollment_Start_Date.
so I want something like this:
ID School_ID Enrollment_Start_Date Order Diff
1 56 1/1/2018 10 10 # nothing to be subtracted from 10
1 56 5/5/2018 24 14 # 24-10
1 56 7/7/2018 35 11 # 35-24
1 103 3/3/2019 19 19 # nothing to be subtracted from 19
1 103 4/4/2019 26 7 # 26-19
I have hundreds of IDs, and each ID can have at most 6 Enrollment_Start_Date, so I'm looking for some generalizable implementations.
Use LAG(<column>) analytic function to obtain a "previous" column value specified within the OVER part, then substract current value from it and make it a positive number multiplying it by -1. If previous value isn't present (is null) then take the current value.
Pseudo code would be:
If previous_order_value exists:
-1 * (previous_order_value - current_order_value)
Else
current_order_value
where previous_order_value is based on the same id & school_id and is sorted by enrollment_start_date in ascending order
SQL Code:
select
id,
school_id,
enrollment_start_date,
[order],
coalesce(-1 * (lag([order]) over (partition by id, school_id order by enrollment_start_date ) - [order]), [order]) as diff
from yourtable
Also note, that order keyword is reserved in SQL Server, which is why your column was created with name wrapped within [ ]. I suggest using some other word for this column, if possible.
use lag() analytic function for getting difference of two row and case when for getting orginal value of order column where no difference exist
with cte as
(
select 1 as id, 56 as sclid, '2018-01-01' as s_date, 10 as orders
union all
select 1,56,'2018-05-05',24 union all
select 1,56,'2018-07-07',35 union all
select 1,103,'2019-04-04',26 union all
select 1,103,'2019-03-03',19
) select t.*,
case when ( lag([orders])over(partition by id,sclid order by s_date ) -[orders] )
is null then [orders] else
( lag([orders])over(partition by id,sclid order by s_date ) -[orders] )*(-1) end
as diff
from cte t
output
id sclid s_date orders diff
1 56 2018-01-01 10 10
1 56 2018-05-05 24 14
1 56 2018-07-07 35 11
1 103 2019-03-03 19 19
1 103 2019-04-04 26 7
demo link
Use LAG(COLUMN_NAME)
Query
SELECT id, School_ID, Enrollment_Start_Date, cOrder,
ISNULL((cOrder - (LAG(cOrder) OVER(PARTITION BY id, School_ID ORDER BY Enrollment_Start_Date))),cOrder)Diff
FROM Table1
Samle Output
| id | School_ID | Enrollment_Start_Date | cOrder | Diff |
|----|-----------|-----------------------|--------|------|
| 1 | 56 | 2018-01-01 | 10 | 10 |
| 1 | 56 | 2018-05-05 | 24 | 14 |
| 1 | 56 | 2018-07-07 | 35 | 11 |
| 1 | 103 | 2019-03-03 | 19 | 19 |
| 1 | 103 | 2019-04-04 | 26 | 7 |
SQL Fiddle Demo
I have a table like
id name R_id mgr_id
----------------------------
61 a 22 1
62 a 22 2
62 b 23 1
63 c 24 4
63 b 22 3
64 c 25 3
and I would like to get the following result set
R_id mgr_id
--------------
22 1
23 1
24 4
25 3
I would like select repeating R_ids only once
I tried using this query but with not much success, can anyone help me.
SELECT DISTINCT R_id, mgr_id from DT
Perhaps something like this... WITH TIES clause in concert with Row_NUmber()
Example
Select Top 1 with ties
R_ID
,mgr_id
From #YourTable
Order By Row_Number() over (Partition By R_ID order by Mgr_id)
Returns
R_ID mgr_id
22 1
23 1
24 4
25 3
I'm having two table which contain data for reviler for employee on shift basis
e.g
Table 1
------
ID NAME RELIVERID
------------
20 ABC 56
----------
21 XYZ 57
----------
22 DEF 58
----------
TABLE 2
---------
ID NAME RELIVERID
-------
56 PQR 20
-----
57 STU 21
-----
58 XYZ 21
----
I want result in third table with following data
Result Table
-------
ID NAME RELIVERID
---
20 ABC 56
-
56 PQR 20
-
21 XYZ 57
-
57 STU 21
-
22 DEF 58
-
58 XYZ 21
-
1 row from first table and alternate row from second table
My suggestion is to use the row_number function, multiply it with a factor for the 1st table and for the second table add 1 so it will be greater than the one in the 1st table and perform an union all. I don't a SQL Server instance to test this, but it should be something like this:
SELECT (ROW_NUMBER() OVER (ORDER BY ID ASC)) * 100 AS OrderID, * FROM Table1
UNION ALL
SELECT (ROW_NUMBER() OVER (ORDER BY ID ASC)) * 100 + 1 AS OrderID, * FROM Table2
ORDER BY OrderID
This question already has answers here:
SQL Server : Transpose rows to columns
(5 answers)
Closed 7 years ago.
What's the best way to get from this input to output?
Input:
date id name info price qty
-----------------------------------------------
20140523 10036 ABC B 12 100
20140523 10036 ABC S 13 75
20140523 10034 XYZ B 22 56
20140523 10034 XYZ S 24 41
20151023 10037 PQR B 30 45
20151023 10037 PQR S 5 20
Output:
date id name b_price b_qty s_price s_qty
---------------------------------------------------------
20140523 10036 ABC 12 100 13 75
20140523 10034 XYZ 22 56 24 41
20140523 10037 PQR 30 45 5 20
With conditional aggregation:
select date,
id,
name,
sum(case info = 'b' then price end) as bprice,
sum(case info = 'b' then qty end) as bqty,
sum(case info = 's' then price end) as sprice,
sum(case info = 's' then qty end) as sqty
from tablename
group by date, id, name
I have tables
table1
epid etid id EValue reqdate
----------- ----------- ----------- ------------ ----------
15 1 1 498925307069 2012-01-01
185 1 2 A5973FC43CE3 2012-04-04
186 1 2 44C6A4B776A2 2012-04-05
205 1 2 7A0ED3F1DA13 2012-09-19
206 1 2 77771D65F9C4 2012-09-19
207 1 2 AD74A4AA41BD 2012-09-19
208 1 2 9595ABE5A0C8 2012-09-19
209 1 2 7611D2FB395B 2012-09-19
210 1 2 04A510D6067A 2012-09-19
211 1 2 24D43EC268F8 2012-09-19
table2
PEId Id EPId
----------- ----------- -----------
43 9 15
44 10 15
45 11 15
46 12 15
47 13 15
48 14 15
49 15 15
50 16 15
51 17 15
52 18 15
table3
PLId PEId Id ToPayId
----------- ----------- ----------- -----------
71 43 9 1
72 43 9 2
73 44 10 1
74 44 10 2
75 45 11 1
76 45 11 2
77 46 12 1
78 46 12 2
79 47 13 1
80 47 13 2
I want to get one id whose count is less than 8 in table 3 and order by peid in table 2,
I have written query
SELECT Top 1 ToPayId FROM
(
SELECT Count(pl.ToPayId) C, pl.ToPayId
FROM table3 pl
INNER JOIN table2 pe ON pl.peid = pe.peid
INNER JOIN table1 e ON pe.epid = e.epid
WHERE e.EtId=1 GROUP BY pl.ToPayId
) As T
INNER JOIN table2 p ON T.ToPayId= p.Id
WHERE C < 8 ORDER BY p.PEId ASC
This query executes more than 1000 times in stored procedure depends on the entries in user-defined-table-type using while condition.
But it is very slow as we have millions of entries in each table.
Can anyone suggest better query regarding above?
maybe try with the having clause to get rid of the from select
select table2.id as due
from table3 inner join table2 on table2.PEId=table3.PEId...
group by ...
having count(due) <8
order by ...
-> you have a redundant Id column in table3 : seems pretty useless as the couple PEId and Id appears unique so remove it and reduce the size of table 3 by 25% hence improving performance of db
Will.. since you did not provide enough sample data and I am not sure what exactly your business logic is. So that I can just modify the code in blind.
SELECT ToPayId
FROM (
SELECT TOP 1 Count(pl.ToPayId) C, pl.ToPayId, pe.PEId
FROM table3 as pl
INNER JOIN table2 as pe ON pl.peid = pe.peid AND pl.ToPayId = pe.Id
INNER JOIN table1 e ON pe.epid = e.epid
WHERE e.EtId=1
GROUP BY pl.ToPayId, pe.PEId
HAVING Count(pl.ToPayId) < 8
ORDER BY pe.PEId ASC
) AS T