SQL Query to find Rank - tsql

I have a table with Child_ID, Test_ID and Test_Date
Child_ID Test_ID Test_Date
1 1 2018-05-21
1 2 2018-05-22
I want to find TestNumber so I am using Rank on fly
Select Child_ID, Test_ID ,Test_Date,RANK() OVER (PARTITION BY t.Child_id order by Test_Date asc,boe.Test_ID asc) AS [TEST_NUMB]
from [Test] T
where t.Child_ID in (1)
which give me result
Child_ID Test_ID Test_Date TEST_NUMB
1 1 2018-05-21 1
1 2 2018-05-22 2
But the issue is when I select only one test ID then the Test_number gives '1' everytime
select * from Test where test_ID =1
Child_ID Test_ID Test_Date TEST_NUMB
2560249 1 2018-05-21 1
select * from Test where test_ID =2
Child_ID Test_ID Test_Date TEST_NUMB
2560249 1 2018-05-21 1
Can I get a query to get the exact test_number according to Test_Date with out inserting to a temp table (I have millions of records)

Place your current query into a CTE, and then query that:
WITH cte AS (
SELECT Child_ID, Test_ID ,Test_Date,
RANK() OVER (PARTITION BY Child_id ORDER BY Test_Date, Test_ID) AS [TEST_NUMB]
FROM [Test]
WHERE Child_ID IN (1)
)
SELECT * FROM cte WHERE test_ID = 1;
This should work because at the time you query by an exact test_ID, the rank values will already have been materialized (sorry, I just wanted to use that word).

Same are CTE from Tim
select *
from ( Select Child_ID, Test_ID , Test_Date
, RANK() OVER (PARTITION BY t.Child_id
order by Test_Date asc, Test_ID asc) AS [TEST_NUMB]
from [Test] T
where Child_ID = 1
) tt
where Test_ID = 1

Related

Delete duplicate rows with different values in columns

I didn't find my case on the Internet. Tell me how i can delete duplicates if the values are in different columns.
I have a table with a lot of values, for example:
|Id1|Id2|
|89417980|89417978|
|89417980|89417979|
|89417978|89417980|
|89417979|89417980|
I need to exclude duplicates and leave in the answer only:
|Id1|Id2|
|89417980|89417978|
|89417980|89417979|
min/max does not work here, as the values may be different.
I tried to union/join tables on a table/exclude results with temporary tables, but in the end I come to the beginning.
Assuming id1 and id2 are primary keys columns you could try this
DECLARE #tbl table (id1 int, id2 int )
INSERT INTO #tbl
SELECT 89417980, 89417978
UNION SELECT 89417980, 89417979
UNION SELECT 89417978, 89417980
UNION SELECT 89417979, 89417980
SELECT * FROM #tbl
;WITH CTE AS (--Get comparable value as "cs"
SELECT
IIF(id1 > id2, CHECKSUM(id1, id2), CHECKSUM(id2,id1)) as cs
, id1
, id2
, ROW_NUMBER() OVER (order by id1, id2) as rn
FROM #tbl
)
, CTE2 AS ( --Get rows to keep
SELECT MAX (rn) as rn
FROM CTE
GROUP BY cs
HAVING COUNT(*) > 1
)
DELETE tbl -- Delete all except the rows to keep
FROM #tbl tbl
WHERE NOT EXISTS(SELECT 1
FROM CTE2
JOIN CTE ON CTE.rn = CTE2.rn
WHERE CTE.id1 = tbl.id1
AND CTE.id2 = tbl.id2
)
SELECT * FROM #tbl

Find rank in SQL table

I have a test table
ID V_ID
1 1
1 2
I want max(V_ID) and resulr should be V_ID 2
select Id,max(V_ID) from test
group by Id,value
I am trying simple query but it's still pulling two records. Is there any other simple query 1) we can try rank 2)?
You should be grouping only by the ID column:
SELECT ID, MAX(V_ID)
FROM test
GROUP BY IdD;
A more general pattern for this type of problem uses ROW_NUMBER to find the entire record for each Id having the max value of V_ID:
SELECT ID, V_ID
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY V_ID DESC) rn
FROM test
) t
WHERE rn = 1;

Recursive CTE to get a Category and all its ancestors [duplicate]

Given a child id, I need to return a query containing all parents of that child as well as their parents till I get to the root parent.
For example, given this data:
ID / Parent ID
1 / 0
2 / 1
3 / 2
4 / 0
5 / 3
So if I passed in ID 5 I would like to get a query with the results:
ID / Parent ID
1 / 0
2 / 1
3 / 2
This table does not work with a hierarchyid type so I suspect that this will need to be done with a CTE, but have no clue how. If it can be done in an SQL query / proc, any help would be appreciated.
Thanks
This is more or less what you want:
-- CTE to prepare hierarchical result set
;WITH #results AS
(
SELECT id,
parentid
FROM [table]
WHERE id = #childId
UNION ALL
SELECT t.id,
t.parentid
FROM [table] t
INNER JOIN #results r ON r.parentid = t.id
)
SELECT *
FROM #results;
Reference:
CTE: Common Table Expression
Working example:
-- create table with self lookup (parent id)
CREATE TABLE #tmp (id INT, parentid INT);
-- insert some test data
INSERT INTO #tmp (id, parentid)
SELECT 1,0 UNION ALL SELECT 2,1 UNION ALL SELECT 3,2
UNION ALL SELECT 4,0 UNION ALL SELECT 5,3;
-- prepare the child item to look up
DECLARE #childId INT;
SET #childId = 5;
-- build the CTE
WITH #results AS
(
SELECT id,
parentid
FROM #tmp
WHERE id = #childId
UNION ALL
SELECT t.id,
t.parentid
FROM #tmp t
INNER JOIN #results r ON r.parentid = t.id
)
-- output the results
SELECT *
FROM #results
WHERE id != #childId
ORDER BY id;
-- cleanup
DROP TABLE #tmp;
Output:
1 | 0
2 | 1
3 | 2

Group by occurrence of row

have a table like below where I have to take sum of col2 based on group by. But it belongs to a chain, if chain breaks then sum will be limited to sequence.
DECLARE #TabVar TABLE
(
ID INT IDENTITY(1,1), col1 varchar(20), Col2 INT
)
INSERT INTO #TabVar
VALUES ('a',2),('a',3),('b',4),('b',2),('a',6),('a',3),('b',3)
SELECT * FROM #TabVar
Expected output:
COL1 SUM(COL2)
A 5
B 6
A 9
B 3
I have tried to do it with Ranking functions but ranking is done using Order by which accumulate total of Col1
You can use a ROW_NUMBER() with PARTITION BY and then a GROUP BY to achieve this.
Whenever your chain breaks, id - ROW_NUMBER()over(partition by col1 order by id) will have a different value for the same col1 value. You can then use this along with col1 to group your data and do a SUM. Something like this
Sample Data
DECLARE #TabVar TABLE
(
ID INT IDENTITY(1,1), col1 varchar(20), Col2 INT
)
INSERT INTO #TabVar
VALUES ('a',2),('a',3),('b',4),('b',2),('a',6),('a',3),('b',3)
Query
SELECT Col1,SUM(Col2) sumcol
FROM
(
SELECT id - ROW_NUMBER()over(partition by col1 order by id) grpcol,Col1,Col2,id
FROM #TabVar
)T
GROUP BY grpcol,Col1
ORDER BY MAX(ID)
Output
Col1 sumcol
a 5
b 6
a 9
b 3
Edit
Incase your IDs are not consecutive in live environment, you can use this
SELECT Col1,SUM(Col2) sumcol
FROM
(
SELECT ROW_NUMBER()over(order by id) - ROW_NUMBER()over(partition by col1 order by id) grpcol, Col1,Col2,id
FROM #TabVar
)T
GROUP BY grpcol,Col1
ORDER BY MAX(ID)

postgres hierarchy - count of child levels and sort by date of children or grandchildren

I would like to know how to write a postgres subquery so that the following table example will output what I need.
id parent_id postdate
1   -1 2015-03-10
2     1 2015-03-11 (child level 1)
3     1 2015-03-12 (child level 1)
4     3 2015-03-13 (child level 2)
5    -1 2015-03-14
6    -1 2015-03-15
7     6 2015-03-16 (child level 1)
If I want to sort all the root ids by child level 1 with a count of children(s) from the parent, the output would be something like this
id count  date
6   2    2015-03-15
1   4    2015-03-10
5   1    2015-03-14
The output is sorted by postdate based on the root's child. The 'date' being outputted is the date of the root's postdate. Even though id#5 has a more recent postdate, the rootid#6's child (id#7) has the most recent postdate because it is being sorted by child's postdate. id#5 doesnt have any children so it just gets placed at the end, sorted by date. The 'count' is the number children(child level 1), grandchildren(child level 2) and itself (root). For instance, id #2,#3,#4 all belong to id#1 so for id#1, the count would be 4.
My current subquery thus far:
SELECT p1.id,count(p1.id),p1.postdate
FROM mytable p1
LEFT JOIN mytable c1 ON c1.parent_id = p1.id AND p1.parent_id = -1
LEFT JOIN mytable c2 ON c2.parent_id = c1.id AND p1.parent_id = -1
GROUP BY p1.id,c1.postdate,p1.postdate
ORDER by c1.postdate DESC,p1.postdate DESC
create table mytable ( id serial primary key, parent_id int references mytable, postdate date );
create index mytable_parent_id_idx on mytable (parent_id);
insert into mytable (id, parent_id, postdate) values (1, null, '2015-03-10');
insert into mytable (id, parent_id, postdate) values (2, 1, '2015-03-11');
insert into mytable (id, parent_id, postdate) values (3, 1, '2015-03-12');
insert into mytable (id, parent_id, postdate) values (4, 3, '2015-03-13');
insert into mytable (id, parent_id, postdate) values (5, null, '2015-03-14');
insert into mytable (id, parent_id, postdate) values (6, null, '2015-03-15');
insert into mytable (id, parent_id, postdate) values (7, 6, '2015-03-16');
with recursive recu as (
select id as parent, id as root, null::date as child_postdate
from mytable
where parent_id is null
union all
select r.parent, mytable.id, mytable.postdate
from recu r
join mytable
on parent_id = r.root
)
select m.id, c.cnt, m.postdate, c.max_child_date
from mytable m
join ( select parent, count(*) as cnt, max(child_postdate) as max_child_date
from recu
group by parent
) c on c.parent = m.id
order by c.max_child_date desc nulls last, m.postdate desc;
You'll need a recursive query to count the elements in the subtrees:
WITH RECURSIVE opa AS (
SELECT id AS par
, id AS moi
FROM the_tree
WHERE parent_id IS NULL
UNION ALL
SELECT o.par AS par
, t.id AS moi
FROM opa o
JOIN the_tree t ON t.parent_id = o.moi
)
SELECT t.id
, c.cnt
, t.postdate
FROM the_tree t
JOIN ( SELECT par, COUNT(*) AS cnt
FROM opa o
GROUP BY par
) c ON c.par = t.id
ORDER BY t.id
;
UPDATE (it appears the OP also wants the maxdate per tree)
-- The same, but also select the postdate
-- --------------------------------------
WITH RECURSIVE opa AS (
SELECT id AS par
, id AS moi
, postdate AS postdate
FROM the_tree
WHERE parent_id IS NULL
UNION ALL
SELECT o.par AS par
, t.id AS moi
-- , GREATEST(o.postdate,t.postdate) AS postdate
, t.postdate AS postdate
FROM opa o
JOIN the_tree t ON t.parent_id = o.moi
)
SELECT t.id
, c.cnt
, t.postdate
, c.maxdate
FROM the_tree t
JOIN ( SELECT par, COUNT(*) AS cnt
, MAX(o.postdate) AS maxdate -- and obtain the max()
FROM opa o
GROUP BY par
) c ON c.par = t.id
ORDER BY c.maxdate, t.id
;
After looking at everyone's code, I created the subquery I needed. I can use PHP to vary the 'case when' code depending on the user's sort selection. For instance, the code below will sort the root nodes based on child level 1's postdate.
with recursive cte as (
select id as parent, id as root, null::timestamp as child_postdate,0 as depth
from mytable
where parent_id = -1
union all
select r.parent, mytable.id, mytable.postdate,depth+1
from cte r
join mytable
on parent_id = r.root
)
select m.id, c.cnt, m.postdate
from ssf.dtb_021 m
join ( select parent, count(*) as cnt, max(child_postdate) as max_child_date,depth
from cte
group by parent,depth
) c on c.parent = m.id
order by
case
when depth=2 then 1
when depth=1 then 2
else 0
end DESC,
c.max_child_date desc nulls last, m.postdate desc;
select
p.id,
(1+c.n) as parent_post_plus_number_of_subposts,
p.postdate
from
table as p
inner join
(
select
parent_id, count(*) as n, max(postdate) as _postdate
from table
group by parent_id
) as c
on p.id = c.parent_id
where p.parent_id = -1
order by c._postdate desc