writing SQL query to show result in specific order - sql

I have this table
+----+--------+------------+-----------+
| Id | day_id | subject_id | period_Id |
+----+--------+------------+-----------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 8 | 2 | 6 | 1 |
| 9 | 2 | 7 | 2 |
| 15 | 3 | 3 | 1 |
| 16 | 3 | 4 | 2 |
| 22 | 4 | 5 | 1 |
| 23 | 4 | 5 | 2 |
| 24 | 4 | 6 | 3 |
| 29 | 5 | 8 | 1 |
| 30 | 5 | 1 | 2 |
to something like this
| Id | day_id | subject_id | period_Id |
| 1 | 1 | 1 | 1 |
| 8 | 2 | 6 | 1 |
| 15 | 3 | 3 | 1 |
| 22 | 4 | 5 | 1 |
| 29 | 5 | 8 | 1 |
| 2 | 1 | 2 | 2 |
| 2 | 1 | 2 | 2 |
| 16 | 3 | 4 | 2 |
| 23 | 4 | 5 | 2 |
| 30 | 5 | 1 | 2 |
+----+--------+------------+-----------+
SO, I want to choose one period with a different subject each day and doing this for number of weeks. so first subject dose not come until all subject have been chosen.

You can ORDER BY period_id first and then by day_id:
SELECT *
FROM your_table
ORDER BY period_Id, day_Id
LiveDemo

Related

Theil–Sen estimator using Hive

I would like to calculate the Theil–Sen estimator per ID for the value column in the sample table below using hive. The Theil–Sen estimator is defined here https://en.wikipedia.org/wiki/Theil%E2%80%93Sen_estimator, I tried to use arrays but could not figure out a solution. Any help is appreciated.
+----+-------+-------+
| 1 | 1 | 10 |
| 1 | 2 | 20 |
| 1 | 3 | 30 |
| 1 | 4 | 40 |
| 1 | 5 | 50 |
| 2 | 1 | 100 |
| 2 | 2 | 90 |
| 2 | 3 | 102 |
| 2 | 4 | 75 |
| 2 | 5 | 70 |
| 2 | 6 | 50 |
| 2 | 7 | 100 |
| 2 | 8 | 80 |
| 2 | 9 | 60 |
| 2 | 10 | 50 |
| 2 | 11 | 40 |
| 2 | 12 | 40 |
+----+-------+-------+

dense_rank over boolean column

Good day. I have a permutated table with condition and I am running redshift DB. This is a table with events log and I splitted it into session start (bool = 1) and session continue (bool = 0) like this:
=======================
| ID | BOOL |
=======================
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 0 |
| 9 | 0 |
| 10 | 0 |
| 11 | 1 |
| 12 | 0 |
| 13 | 0 |
| 14 | 1 |
| 15 | 0 |
| 16 | 0 |
=======================
I need to create sesssion_id column with something like dense_rank:
================================
| ID | BOOL | D_RANK |
================================
| 1 | 0 | 1 |
| 2 | 1 | 2 |
| 3 | 0 | 2 |
| 4 | 0 | 2 |
| 5 | 0 | 2 |
| 6 | 0 | 2 |
| 7 | 0 | 2 |
| 8 | 0 | 2 |
| 9 | 0 | 2 |
| 10 | 0 | 2 |
| 11 | 1 | 3 |
| 12 | 0 | 3 |
| 13 | 0 | 3 |
| 14 | 1 | 4 |
| 15 | 0 | 4 |
| 16 | 0 | 4 |
================================
Is there any option to do this? Would appreciate any help.
Use a cumulative sum. Assuming that bool is the start of a new session:
select t.*,
sum(bool) over (order by id) as session_id
from t;
Note: This will start at 0. You can add 1 if you need.

Find relation level from text field

I have a table containing geographical structure of units. There are parent-child relation columns but I want to use the existing text field (instead of recursion) to find the relation level between the items.
(here's a table creation script)
drop table if exists #temp_structure
create table #temp_structure
(org_id int,
parent_org_id int,
org_name nvarchar(255),
search_tree nvarchar(255))
insert into #temp_structure
values
(1,null,'World','| 1 |'),
(2,1,'Europe','| 1 | 2 |'),
(3,1,'North America','| 1 | 3 |'),
(4,1,'South America','| 1 | 4 |'),
(5,1,'Asia','| 1 | 5 |'),
(6,1,'Africa','| 1 | 6 |'),
(7,1,'Australia','| 1 | 7 |'),
(8,2,'Spain','| 1 | 2 | 8 |'),
(9,2,'Germany','| 1 | 2 | 9 |'),
(10,2,'Italy','| 1 | 2 | 10 |'),
(11,2,'France','| 1 | 2 | 11 |'),
(12,8,'Madrid ','| 1 | 2 | 8 | 12 |'),
(13,8,'Barcelona ','| 1 | 2 | 8 | 13 |'),
(14,9,'Berlin','| 1 | 2 | 9 | 14 |'),
(15,9,'Munich','| 1 | 2 | 9 | 15 |'),
(16,10,'Rome','| 1 | 2 | 10 | 16 |'),
(17,10,'Milano','| 1 | 2 | 10 | 17 |'),
(18,11,'Paris','| 1 | 2 | 11 | 18 |'),
(19,11,'Marseille','| 1 | 2 | 11 | 19 |')
The expected result I would like to achieve is presented below (I listed only one 4th level example):
+--------+-------------+------------+
| org_id | search_item | nest_level |
+--------+-------------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
| 3 | 1 | 2 |
| 4 | 4 | 1 |
| 4 | 1 | 2 |
| 5 | 5 | 1 |
| 5 | 1 | 2 |
| 6 | 6 | 1 |
| 6 | 1 | 2 |
| 7 | 7 | 1 |
| 7 | 1 | 2 |
| 8 | 8 | 1 |
| 8 | 2 | 2 |
| 8 | 1 | 3 |
| 9 | 9 | 1 |
| 9 | 2 | 2 |
| 9 | 1 | 3 |
| 10 | 10 | 1 |
| 10 | 2 | 2 |
| 10 | 1 | 3 |
| 11 | 11 | 1 |
| 11 | 2 | 2 |
| 11 | 1 | 3 |
| 12 | 12 | 1 |
| 12 | 8 | 2 |
| 12 | 2 | 3 |
| 12 | 1 | 4 |
.....................................
+--------+-------------+------------+
I was able to pull the org_id-search_item relation using STRING_SPLIT, but I still miss the tricky level part (I wonder about enumerating the '|' characters)
SELECT t.org_id
--,substring(replace(search_tree, ' ', ''), 2, len(replace(search_tree, ' ', '')) - 2)
,ss.value as search_item
FROM #temp_structure t
CROSS APPLY string_split(substring(replace(search_tree, ' ', ''), 2, len(replace(search_tree, ' ', '')) - 2),'|') ss
I have not thoroughly tested this, but you could try something like the following:
-- Table mock-up.
DECLARE #temp TABLE ( org_id int, parent_org_id int, org_name nvarchar(255), search_tree nvarchar(255) )
-- Insert sample data...
INSERT INTO #temp VALUES
(1,null,'World','| 1 |'),(2,1,'Europe','| 1 | 2 |'),
(3,1,'North America','| 1 | 3 |'),(4,1,'South America','| 1 | 4 |'),
(5,1,'Asia','| 1 | 5 |'),(6,1,'Africa','| 1 | 6 |'),
(7,1,'Australia','| 1 | 7 |'),(8,2,'Spain','| 1 | 2 | 8 |'),
(9,2,'Germany','| 1 | 2 | 9 |'),(10,2,'Italy','| 1 | 2 | 10 |'),
(11,2,'France','| 1 | 2 | 11 |'),(12,8,'Madrid ','| 1 | 2 | 8 | 12 |');
-- Select data in a nested level...
SELECT
org_id,
search_item,
ROW_NUMBER() OVER ( PARTITION BY org_id ORDER BY org_id, parent_org_id, search_item DESC ) AS nest_level
FROM #temp AS tmp
CROSS APPLY (
SELECT CAST ( [value] AS INT ) AS search_item FROM STRING_SPLIT ( tmp.search_tree, '|' )
WHERE NULLIF ( [value], '' ) IS NOT NULL
) AS tree
ORDER BY
org_id, parent_org_id, search_item DESC;
Returns
+--------+-------------+------------+
| org_id | search_item | nest_level |
+--------+-------------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 2 | 1 | 2 |
| 3 | 3 | 1 |
| 3 | 1 | 2 |
| 4 | 4 | 1 |
| 4 | 1 | 2 |
| 5 | 5 | 1 |
| 5 | 1 | 2 |
| 6 | 6 | 1 |
| 6 | 1 | 2 |
| 7 | 7 | 1 |
| 7 | 1 | 2 |
| 8 | 8 | 1 |
| 8 | 2 | 2 |
| 8 | 1 | 3 |
| 9 | 9 | 1 |
| 9 | 2 | 2 |
| 9 | 1 | 3 |
| 10 | 10 | 1 |
| 10 | 2 | 2 |
| 10 | 1 | 3 |
| 11 | 11 | 1 |
| 11 | 2 | 2 |
| 11 | 1 | 3 |
| 12 | 12 | 1 |
| 12 | 8 | 2 |
| 12 | 2 | 3 |
| 12 | 1 | 4 |
+--------+-------------+------------+

sql (oracle) counting number of overlapping intervals

I have the following problem:
Given the following table test in an oracle sql database:
+----+------+-------+------+
| id | name | start | stop |
+----+------+-------+------+
| 1 | A | 1 | 5 |
+----+------+-------+------+
| 2 | A | 2 | 6 |
+----+------+-------+------+
| 3 | A | 5 | 8 |
+----+------+-------+------+
| 4 | A | 9 | 10 |
+----+------+-------+------+
| 5 | B | 3 | 6 |
+----+------+-------+------+
| 6 | B | 4 | 8 |
+----+------+-------+------+
| 7 | B | 1 | 2 |
+----+------+-------+------+
I would like to find the number of overlapping intervals (endpoints included) [start, stop] n_overlap, for all id having the same name, i.e.:
+----+------+-------+------+-----------+
| id | name | start | stop | n_overlap |
+----+------+-------+------+-----------+
| 1 | A | 1 | 5 | 3 |
+----+------+-------+------+-----------+
| 2 | A | 2 | 6 | 3 |
+----+------+-------+------+-----------+
| 3 | A | 4 | 8 | 3 |
+----+------+-------+------+-----------+
| 4 | A | 9 | 10 | 1 |
+----+------+-------+------+-----------+
| 5 | B | 3 | 6 | 2 |
+----+------+-------+------+-----------+
| 6 | B | 4 | 8 | 2 |
+----+------+-------+------+-----------+
| 7 | B | 1 | 2 | 1 |
+----+------+-------+------+-----------+
One method uses a correlated subquery:
select t.*,
(select count(*)
from test t2
where t2.name = t.name and
t2.start < t.end and
t2.end > t.start
) as num_overlaps
from test t;

how to write a query to get multilevel data

I have four tables as below:
tblAccount
Id i sprimary key
+----+-----------------+
| Id | AccName |
+----+-----------------+
| 1 | AccountA |
| 2 | AccountB |
+----+-----------------+
tblLocation
Id is primary key.
+----+---------------+
| Id | LocName |
+----+---------------+
| 1 | LocationA |
| 2 | LocationB |
| 3 | LocationC |
+----+---------------+
tblAccountwiseLocation
Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+
| Id | LocId | AccId |
+----+---------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 1 | 2 |
| 5 | 2 | 2 |
| 6 | 3 | 2 |
+----+---------------+---------------+
tblRSCMaster
Id i sprimary key.LocId and AccId are foreign key.
+----+---------------+---------------+----------------+------------------+
| Id | LocId | AccId | RSCNo | DateOfAddition |
+----+---------------+---------------+----------------+------------------+
| 1 | 1 | 1 | Acc1_Loc1_1_14 | 15/01/2014 |
| 2 | 2 | 1 | Acc1_Loc2_1_14 | 15/01/2014 |
| 3 | 3 | 1 | Acc1_Loc2_1_14 | 15/01/2014 |
| 4 | 1 | 2 | Acc2_Loc1_1_14 | 15/01/2014 |
| 5 | 2 | 2 | Acc2_Loc2_1_14 | 15/01/2014 |
| 6 | 3 | 2 | Acc2_Loc3_1_14 | 15/01/2014 |
| 7 | 1 | 1 | Acc1_Loc1_2_14 | 15/02/2014 |
| 8 | 2 | 1 | Acc1_Loc2_2_14 | 15/02/2014 |
| 9 | 3 | 1 | Acc1_Loc3_2_14 | 15/02/2014 |
| 10 | 1 | 2 | Acc2_Loc1_2_14 | 15/02/2014 |
| 11 | 2 | 2 | Acc2_Loc2_2_14 | 15/02/2014 |
| 12 | 3 | 2 | Acc2_Loc3_2_14 | 15/02/2014 |
| 13 | 1 | 1 | Acc1_Loc1_3_14 | 15/03/2014 |
| 14 | 2 | 1 | Acc1_Loc2_3_14 | 15/03/2014 |
| 15 | 3 | 1 | Acc1_Loc3_3_14 | 15/03/2014 |
| 16 | 1 | 2 | Acc2_Loc1_3_14 | 15/03/2014 |
| 17 | 2 | 2 | Acc2_Loc2_3_14 | 15/03/2014 |
| 18 | 3 | 2 | Acc2_Loc3_3_14 | 15/03/2014 |
| 19 | 1 | 1 | Acc1_Loc1_4_14 | 15/04/2014 |
| 20 | 2 | 1 | Acc1_Loc2_4_14 | 15/04/2014 |
| 21 | 3 | 1 | Acc1_Loc3_4_14 | 15/04/2014 |
| 22 | 1 | 2 | Acc2_Loc1_4_14 | 15/04/2014 |
| 23 | 2 | 2 | Acc2_Loc2_4_14 | 15/04/2014 |
| 24 | 3 | 2 | Acc2_Loc3_4_14 | 15/04/2014 |
| 25 | 1 | 1 | Acc1_Loc1_5_14 | 15/05/2014 |
| 26 | 2 | 1 | Acc1_Loc2_5_14 | 15/05/2014 |
| 27 | 3 | 1 | Acc1_Loc3_5_14 | 15/05/2014 |
| 28 | 1 | 2 | Acc2_Loc1_5_14 | 15/05/2014 |
| 29 | 2 | 2 | Acc2_Loc2_5_14 | 15/05/2014 |
| 30 | 3 | 2 | Acc2_Loc3_5_14 | 15/05/2014 |
+----+---------------+---------------+----------------+------------------+
Acc1_Loc1_1_14 resembles RSC for LocationA of AccountA for Jan 2014.
I need to get a output as below from tblRSCMaster.
+---------------+---------------+----------------+------------------+
| LocId | AccId | RSCNo | DateOfAddition |
+---------------+---------------+----------------+------------------+
| 1 | 1 | Acc1_Loc1_3_14 | 15/03/2014 |
| 1 | 1 | Acc1_Loc1_4_14 | 15/04/2014 |
| 1 | 1 | Acc1_Loc1_5_14 | 15/05/2014 |
| 2 | 1 | Acc1_Loc2_3_14 | 15/03/2014 |
| 2 | 1 | Acc1_Loc2_4_14 | 15/04/2014 |
| 2 | 1 | Acc1_Loc2_5_14 | 15/05/2014 |
| 3 | 1 | Acc1_Loc3_3_14 | 15/03/2014 |
| 3 | 1 | Acc1_Loc3_4_14 | 15/04/2014 |
| 3 | 1 | Acc1_Loc3_5_14 | 15/05/2014 |
+---------------+---------------+----------------+------------------+
Each account has multiple locations and each location has multiple RSCs.
I need to get last three RSCs for each location for AccountA.
I have tried the below query:
SELECT tblAccountwiseLocation.LocId,tblAccountwiseLocation.AccId,tblRSCMaster.RSCNo,tblRSCMaster.DateOfAddition FROM tblAccountwiseLocation
INNER JOIN tblRSCMaster ON tblAccountwiseLocation.LocId= tblRSCMaster.LocId
where tblRSCMaster.AccId=1
But not getting the proper output.
Please help me out.
Thank you all in advance.
You can wrap the existing query inside a common table expression, and use ROW_NUMBER() to get only the last 3 (by tblRSCMaster.DateOfAddition) entries per tblAccountwiseLocation.LocId.
WITH cte AS (
SELECT tblAccountwiseLocation.LocId,
tblAccountwiseLocation.AccId,
tblRSCMaster.RSCNo,
tblRSCMaster.DateOfAddition,
ROW_NUMBER() OVER (PARTITION BY tblAccountwiseLocation.LocId
ORDER BY tblRSCMaster.DateOfAddition DESC) rn
FROM tblAccountwiseLocation
INNER JOIN tblRSCMaster
ON tblAccountwiseLocation.LocId = tblRSCMaster.LocId
AND tblAccountwiseLocation.AccId = tblRSCMaster.AccId
WHERE tblRSCMaster.AccId=1
)
SELECT LocId, AccId, RSCNo, DateOfAddition
FROM cte
WHERE rn <= 3
ORDER BY LocId, AccId, DateOfAddition
An SQLfiddle to test with.
Is this what you need?
select m.*
from (select m.*, row_number() over (partition by accID
order by DateOfAddition desc) as seqnum
from tblRSCMaster
where m.locid = 1
) m
where seqnum <= 3
order by AccId, DateOfAddition;
I think you need to filter on the locid rather than on the AccId to get what you want.