Find multiple records with same Id in SQL server - sql

Find multiple records with same Id in SQL server
I have following value in my table
PK Id Value1 Value2
1 1 5 10
2 1 10 10
3 2 5 20
4 3 20 25
5 3 5 5
6 4 10 10
7 1 5 20
8 4 5 10
9 2 25 30
10 5 25 30
11 5 5 20
12 5 5 5
I want to get 'Id' column which have two record with some values like (First Record) Value1 = 5, Value2 = 20 and (Second Record) Value1 =25, Value2 = 30.
In Above table my expected result is
PIds Id Value1 Value2
3 2 5 20
9 2 25 30
10 5 25 30
11 5 5 20
Thanks in Advance.

Please try:
select * from YourTable
where
(Value1=5 and Value2=20) OR
(Value1 =25 and Value2 = 30)

All other query shows 1, 2 and 5 Ids
I have found this query for my problem
select T1.Id,* from TestData T1 inner join TestData T2
on T1.Id = T2.Id
where
(T2.Value1=5 and T2.Value2=20) and
(T1.Value1=25 and T1.Value2 = 30)

Related

In DolphinDB, how to group a table by value of a certain column to achieve the same operation as qcut in pandas?

testData = table(1 2 3 4 5 6 7 8 9 10 as id,1 2 3 4 5 6 7 8 9 10 as val)
Divide val column into 2 groups: val<=5, 5<val<=10, and then obtain the sum of each group.
Here you are:
testData = table(1 2 3 4 5 6 7 8 9 10 as id,1 2 3 4 5 6 7 8 9 10 as val)
select sum(val) from testData group by val <= 5

Update rows based on range around values without changing rows that are not initially within range

In a local SQLite (vs 3.29.0) database, there is a table with 3 columns (excluding the rowID). Two contain values, and one contains categories. I want to update the category based on a range around the values of one specific category. It needs to be possible that the category that is SET is the same category as the one that determines the range.
Example:
id
Value
Value2
Category
1
20
20
2
2
30
30
2
3
40
40
2
4
70
70
2
5
5
5
1
6
19
19
1
7
26
26
1
8
42
42
1
9
49
49
1
10
52
52
1
11
71
71
1
12
90
90
1
13
17
17
1
I want rows to be changed to category 2, based on a range of 4 around value and a range of 2 around value2. This should change only rows 6, 9 and 11:
id
Value
Value2
Category
1
20
20
2
2
30
30
2
3
40
40
2
4
70
70
2
5
5
5
1
6
19
19
2
7
26
26
1
8
42
42
2
9
49
49
1
10
52
52
1
11
71
71
2
12
90
90
1
13
17
17
1
My current SQL statement is:
UPDATE tablename
SET Category = 2
WHERE (Category != 2
AND EXISTS (
SELECT *
FROM tablename t
WHERE t.Category = 2
AND tablename.Value BETWEEN t.Value - 4 AND t.Value + 4
AND tablename.Value2 BETWEEN t.Value2 -2 AND t.Value2 +2)
);
of which the result is:
id
Value
Value2
Category
1
20
20
2
2
30
30
2
3
40
40
2
4
70
70
2
5
5
5
1
6
19
19
2
7
26
26
1
8
42
42
2
9
49
49
1
10
52
52
1
11
71
71
2
12
90
90
1
13
17
17
2
What appears to be happening is that due to row 6 changing to category 2, row 13 is now within range of the values of a row that is in category 2, and therefore is also assigned category 2. How do I change the statement so that the SET is only applied to the values that were within range initially?
See the demo for the example.
If your version of SQLite is 3.33.0+ you can use the join-like UPDATE...FROM syntax to perform a self join in the UPDATE statement:
UPDATE tablename AS t1
SET Category = t2.Category
FROM tablename AS t2
WHERE t2.Category = 2
AND t1.Category <> t2.Category
AND t1.Value BETWEEN t2.Value - 4 AND t2.Value + 4
AND t1.Value2 BETWEEN t2.Value2 - 2 AND t2.Value2 + 2;
For previous versions of SQLite, first create a temporary table with all the rows of the table with Category = 2:
CREATE TEMPORARY TABLE t AS
SELECT * FROM tablename WHERE Category = 2;
and then update the table:
UPDATE tablename
SET Category = 2
WHERE Category <> 2
AND EXISTS (
SELECT 1
FROM t
WHERE tablename.Value BETWEEN t.Value - 4 AND t.Value + 4
AND tablename.Value2 BETWEEN t.Value2 -2 AND t.Value2 + 2
);
See the demo.

pandas: get top n including the duplicates of a sorted column

I have some data like
This is a table sorted by score column and also then by cat column
score cat
18 B
18 A
17 A
16 B
16 A
15 B
14 B
13 A
12 A
10 B
9 B
I want to get the top 5 of score including the duplicates and also add the rank
i.e
rank score cat
1 18 B
1 18 A
2 17 A
3 16 B
3 16 A
4 15 B
5 14 B
How can i get this using pandas
Since the data frame is ordered, try factorize
df['rnk'] = df.score.factorize()[0]+1
out = df[df['rnk'] <= 5]
out
score cat rnk
0 18 B 1
1 18 A 1
2 17 A 2
3 16 B 3
4 16 A 3
5 15 B 4
6 14 B 5

Fill in sequence numbers in sql oracle

I have a table that includes two columns, these columns have ranges i.e
Batch from _serial_no to_serial_no
a 1 5
b 2 7
I want to create another column to fill in the gaps for a abd b separately
Something like this
Batch from _serial_no to_serial_no seq_number
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
Is there an sql I could use?
I tried something like this but it didn't work
select *
from (
select a.*,rownum n
from my_table a connect by level <= TO_SERIAL_NO
)
where n >= FROM_SERIAL_NO;
SQL> with
2 data as (select 'a' batch, 1 from_serial_number, 5 to_serial_number from dual
3 union all
4 select 'b' batch, 2 from_serial_number, 7 to_serial_number from dual),
5 seq as (select rownum n# from dual connect by level <= (select max(to_serial_number) from data))
6 select
7 data.*,
8 seq.n#
9 from
10 data,
11 seq
12 where
13 seq.n# between data.from_serial_number and data.to_serial_number
14 order by
15 1, 2, 4;
BATCH FROM_SERIAL_NUMBER TO_SERIAL_NUMBER N#
----- ------------------ ---------------- ----------
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
11 rows selected
Yet another option:
SQL> with test (batch, from_serial_no, to_serial_no) as
2 (select 'a', 1, 5 from dual union
3 select 'b', 2, 7 from dual
4 )
5 select
6 batch,
7 from_serial_no,
8 to_serial_no,
9 froM_serial_no + column_value - 1 seq_number
10 from test,
11 table(cast(multiset(select level from dual
12 connect by level <= to_serial_no - from_serial_no + 1
13 ) as sys.odcinumberlist))
14 order by batch, seq_number;
B FROM_SERIAL_NO TO_SERIAL_NO SEQ_NUMBER
- -------------- ------------ ----------
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
11 rows selected.
SQL>
Using a join
select d.*, t.seq_number
from data d
join
(
SELECT from_serial_no + level - 1 seq_number
FROM (select min(from_serial_no) from_serial_no,
max(to_serial_no) to_serial_no
from data) t
CONNECT BY from_serial_no + level - 1 <= to_serial_no
) t on d.from_serial_no <= t.seq_number and
d.to_serial_no >= t.seq_number
order by d.batch, t.seq_number;
dbfiddle demo

mysql count from same table and data from the other table

I want to display the name of the registered users with the count of regid by supplying replyid, I don't know what will be the correct query to get the results
Here are the tables.
details_table
id regid replyid
-------------------
1 1 2
2 1 3
6 2 4
5 3 4
8 2 5
9 3 5
10 4 5
11 5 5
12 2 6
13 6 6
14 4 6
15 7 7
16 8 7
17 9 7
18 10 8
19 2 9
20 2 10
21 11 10
22 12 10
reg_table
id regname
---------------
1 Sam
2 Ash
3 Tina
4 Rohny
5 Martin
6 Natasha
7 Natalia
8 Kim
9 Alex
10 John
11 Neil
12 Peter
So if replyid i.e. (10) is select from details_table by where clause, it's suppose to display the 2,11,12 i.e. (Ash,Neil,Peter) from reg_table with the count of Ash=5,Neil=1,Peter=1
SELECT a.id, a.regname, COUNT(1)
FROM reg_table a, details_table b,
details_table c
WHERE b.replyid=10
AND b.regid = a.id
AND c.regid = a.id
GROUP BY a.id, a.regname
SELECT r.regid, r.regname, count(*)
FROM (
SELECT DISTINCT regid
FROM details_table
WHERE replyid = 10
) rg
JOIN reg_table r ON rg.regid = r.regid
JOIN details_table d ON r.regid = d.regid
GROUP BY r.regid
try this
select count(dt.regid) as cnt, regname from details_table dt, reg_table rt where dt.regid = rt.Regid and dt.replyid = 10 group by rt.Regid
SELECT reg_table.regname, count(*) from reg_table, details_table where details_table.regid = reg_table.id GROUP BY reg_table.id