Want the data of Upline and Downline for particular ID - sql

here is the table
and data like:
id name
1 test1
2 test2
3 test3
4 test4
5 test5
6 test6
From above data i want the data like
if i pass the id as parameter and return the data from from up and gown by order
Example if i pass the id as parameter = 4 then it should be return
upline 2 row and downline 2 row for particular id, and it should be like this
id name
2 test2
3 test3
4 test4
5 test5
6 test6
and same for the id = 3
id name
1 test1
2 test2
3 test3
4 test4
5 test5

SELECT TOP 3 id, name
FROM table
WHERE id =< #id
ORDER BY id DESC
UNION
SELECT TOP 2 id, name
FROM table
WHERE id > #id
ORDER BY id ACS

Related

SQL Increment based on another column

I have a table that I need to assign a number sequence to, then have the number sequence restarts to 1 again based on a value in another column.
Here is my table:
Date
Name
Indicator
01/12/2022
Test1
1
02/12/2022
Test2
NULL
03/12/2022
Test3
NULL
04/12/2022
Test4
NULL
05/12/2022
Test5
1
06/12/2022
Test6
NULL
07/12/2022
Test7
1
08/12/2022
Test8
NULL
What I need is to add an "Instance" column to the end, this column simply continues the number sequence until it hits a row with a 1 in the "Indicator" column again then it resets.
So my desired outcome would be:
Date
Name
Indicator
Instance
01/12/2022
Test1
1
1
02/12/2022
Test2
NULL
2
03/12/2022
Test3
NULL
3
04/12/2022
Test4
NULL
4
05/12/2022
Test5
1
1
06/12/2022
Test6
NULL
2
07/12/2022
Test7
1
1
08/12/2022
Test8
NULL
2
I've tried playing aound with DENSE_RANK() over (Order by Date)
But I understandly end up with this:
Date
Name
Indicator
Instance
01/12/2022
Test1
1
1
02/12/2022
Test2
NULL
2
03/12/2022
Test3
NULL
3
04/12/2022
Test4
NULL
4
05/12/2022
Test5
1
1
06/12/2022
Test6
NULL
6
07/12/2022
Test7
1
1
08/12/2022
Test8
NULL
8
The sequence carries on, it does not reset at the 1 (in the indicator column).
You can do:
select x.*, row_number() over(partition by g order by date) as instance
from (select t.*, sum(indicator) over(order by date) as g from t) x
Result:
Date Name Indicator g instance
----------- ------ ---------- -- --------
2022-01-12 Test1 1 1 1
2022-02-12 Test2 null 1 2
2022-03-12 Test3 null 1 3
2022-04-12 Test4 null 1 4
2022-05-12 Test5 1 2 1
2022-06-12 Test6 null 2 2
2022-07-12 Test7 1 3 1
2022-08-12 Test8 null 3 2
See running example at db<>fiddle.

Adding auto incremented column on select join statement

I have requirement to create an auto incremented id based on certain join condition. I have two tables each having identity column. Primary table would have relationship with the secondary table on "trxid" column. Below is the sample data
declare #t1 table (trxid int,data1 varchar(100), data2 varchar(100))
declare #m table (mid int, trxid int, mname varchar(100), mdate date)
insert into #t1
select 1,'Test1','Test1'
union all
select 2,'Test2','Test2'
insert into #m
select 1,1,'Stage Gate1',GETDATE()
union all
select 2,1,'Stage Gate2',GETDATE()
union all
select 3,1,'Stage Gate3',GETDATE()
union all
select 4,1,'Stage Gate4',GETDATE()
union all
select 5,1,'Stage Gate5',GETDATE()
union all
select 6,1,'Stage Gate6',GETDATE()
union all
select 7,2,'Stage Gate1',GETDATE()
union all
select 8,2,'Stage Gate2',GETDATE()
union all
select 9,2,'Stage Gate3',GETDATE()
union all
select 10,2,'Stage Gate4',GETDATE()
union all
select 11,2,'Stage Gate5',GETDATE()
union all
select 12,2,'Stage Gate6',GETDATE()
union all
select 13,2,'Stage Gate7',GETDATE()
Result with below statement
select t.trxid,m.mid,t.data1,t.data2,m.mname,m.mdate from #t1 t inner join #m m on(t.trxid=m.trxid)
trxid mid data1 data2 mname mdate
----------- ----------- ------- ------- --------------- ----------
1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 Test1 Test1 Stage Gate6 2018-06-07
2 7 Test2 Test2 Stage Gate1 2018-06-07
2 8 Test2 Test2 Stage Gate2 2018-06-07
2 9 Test2 Test2 Stage Gate3 2018-06-07
2 10 Test2 Test2 Stage Gate4 2018-06-07
2 11 Test2 Test2 Stage Gate5 2018-06-07
2 12 Test2 Test2 Stage Gate6 2018-06-07
2 13 Test2 Test2 Stage Gate7 2018-06-07
Expected result is
trxid id mid data1 data2 mname mdate
----------- ----------- ----------- ------- ------- --------------- ----------
1 1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 6 Test1 Test1 Stage Gate6 2018-06-07
2 1 7 Test2 Test2 Stage Gate1 2018-06-07
2 2 8 Test2 Test2 Stage Gate2 2018-06-07
2 3 9 Test2 Test2 Stage Gate3 2018-06-07
2 4 10 Test2 Test2 Stage Gate4 2018-06-07
2 5 11 Test2 Test2 Stage Gate5 2018-06-07
2 6 12 Test2 Test2 Stage Gate6 2018-06-07
2 7 13 Test2 Test2 Stage Gate7 2018-06-07
Assuming you don't need to persist this ID and then generate new unique incremental numbers based on the persisted data, you can use window functions to generate a row_number for each group of your data:
select t.trxid
,m.mid
,row_number() over (partition by t.trxid order by m.mid) as id
,t.data1
,t.data2
,m.mname
,m.mdate
from #t1 t
inner join #m m
on(t.trxid = m.trxid)
If you need to base this on data that you have already saved to a table elsewhere, your query will need to be more complex.

Select count of specific rows within WHERE clause in a Postgres SELECT statement

Let's say I have a table called Build and a table called Test. Now, for every build there are 3 tests. And the table tests has build_id as foreign key. Also, when a test fails my test framework retries the test again. Given this, there would be atleast 3 entries in the Test table + more based on how many tests failed and how many times it was retries (max = 3). Also, after retries, if the test passes, then the test is marked as passed.
Sample:
Build Table:
Id No of Tests Failure Count
1 3 0
2 3 0
3 3 1
Tests Table:
build_id test_id test_name test_result
1 1 test1 p
1 2 test2 p
1 3 test3 f
1 3 test3 f
1 3 test3 f
1 3 test3 p
2 1 test1 p
2 2 test2 p
2 3 test3 p
3 1 test1 p
3 2 test2 f
3 3 test3 p
2 2 test2 f
2 2 test2 f
2 2 test2 f
Now, based on that I want to print
test_name retries
test3 3
test2 3
If I understand correctly, you want the number of times that a test is "f":
select test_name, count(*) as retries
from tests
where test_result = 'f'
group by test_name;

Pivot/Transpose in SQL Query in Oracle

I didn't know if my problem is related to Pivoting or Transposing so that's why I wrote both in the title.
Below is my query (using it in an Oracle APEX Report)
SELECT QUESTION_ID,
RESPONDENT,
ANSWER
FROM SURVEY
Here is the result :
Question_ID Respondent Answer
1 A test1
2 A test2
3 A test3
1 B test4
2 B test5
3 B test6
The result I want is this :
Question
Respondant 1 2 3
A test1 test2 test3
B test4 test5 test6
How can this be achieved?
select *
from table_name
pivot ( min(answer) for question_id in (1 as q1, 2 as q2, 3 as q3));

Calculate column in SQL Server database

I have table like this
id buildingId order title
--------------------------
1 2 null test1
2 2 null test2
3 2 null test3
4 3 null test4
5 3 null test5
6 5 null test6
I need calculate order value for every row. I need after execute sql query get this table
id buildingId order title
1 2 0 test1
2 2 1 test2
3 2 2 test3
4 3 0 test4
5 3 1 test5
6 5 0 test6
How can I do that?
WITH recordList
AS
(
SELECT ID, buildingID, [order], title,
ROW_NUMBER() OVER (PARTITION BY buildingID
ORDER BY ID) rn
FROM tableName
)
UPDATE recordList
SET [order] = rn - 1
SQLFiddle Demo