SQL Select group where some attribute is same - sql

Lets say I have a table like this
A B C
-----
1 a 12
2 a 23
3 b 43
4 c 25
5 c 44
6 d 34
How to select only rows where B exists in another row?
Result would be:
A B C
-----
1 a 12
2 a 23
4 c 25
5 c 44

I'm not sure what you are expecting but eliminating B & D
we can achieve like this
Select T.A,T.B,T.C from Table T
INNER JOIN (
SELECT B FROM Table
groUP by b
having count(B) > 1 )TT
ON T.B = TT.B

Just use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.b = t.b and t2.a <> t.a
);
With an index on t(b, a), this is likely to be the fastest method.

Related

Countif statement in Postgresql

How can I use countif statement in PostgreSQL?
max(COUNTIF(t1.A1:C10,t2.a1),COUNTIF(t1.A1:C10,t2.b1),COUNTIF(t1.A1:C10,t2.c1))
I have table1 which is more then a million rows
a
b
c
M5
16
27
31
3
7
27
and table2 more then 100 rows including different dates after column c
a
b
c
10
15
16
30
40
50
60
70
80
16
18
37
5
12
16
8
31
28
11
12
13
7
9
31
2
7
21
20
16
27
8
12
17
2
8
14
3
14
15
The outcome should be something like this
a
b
c
M5
16
27
31
3
3
7
27
2
Tried the below query but the outcome is not correct
UPDATE table1 SET m5 = greatest(
case When a in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END,
case When b in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END,
case When c in(select unnest(array[a,b,c]) from (select * from table2 order by date DESC limit 10) foo) then 1 else 0 END)
Assuming your columns are fixed and predictable, I think you could put all possible table values into a single column and then do counts for each occurrence:
with exploded as (
select a from table2
union all
select b from table2
union all
select c from table2
)
select a, count (*) as count
from exploded e
group by a
So for example, the value 7 occurs twice (which would be reflected in this output).
From there, you can just do the updates from the CTE:
with exploded as (
select a from table2
union all
select b from table2
union all
select c from table2
),
counted as (
select a, count (*) as count
from exploded e
group by a
)
update table1 t
set m5 = greatest (ca.count, cb.count, cc.count)
from
counted ca,
counted cb,
counted cc
where
t.a = ca.a and
t.b = cb.a and
t.c = cc.a
The only issue I see is if one of the values does not come up (the inner join fails), but in your example that doesn't seem to happen.
If it is possible, I would think that could be resolved with one more CTE to fill in missing values from table1 in the set of possible occurrences.

assign a new value based on combinations

I have these two tables
the first one has id's and a categorical variable 'code'
table1
id code
1 1 F
2 1 B
3 1 J
4 2 D
5 2 B
6 2 F
7 2 G
8 2 C
9 2 D
10 3 G
11 3 G
12 3 G
13 4 B
14 4 F
15 4 C
16 4 D
17 5 C
18 5 A
19 5 G
20 5 D
and table2
has some combinations of the categorical variable 'code' which are assigned a new category 'code3'
code1 code2 code_3
1 C B O
2 B A K
3 A C L
4 E B N
5 A D J
6 D B L
id's in table1 come with multiple codes, the combinations of those codes result in new codes found on table2.
how to I assign the id's in table1 the values in table2 code3 based on the combinations they have?
desired output
would be something like
id code
1 1 F
2 1 B
3 1 J
5 2 L -- added, while a B and D removed
6 2 F
7 2 G
8 2 C
...
You can get the list of new codes to add by doing a self-join and then joining to table2 to find matches:
select t1.id, t2.code3
from table1 t1 join
table1 tt1
on t1.id = tt1.id and
t1.code < t2.code join
table2 t2
on t2.code1 = t1.code and
t2.code2 = tt1.code;
SELECT id, code, NVL (code3, code)
FROM (SELECT id,
code,
hh,
rr,
gg,
code3
FROM ( SELECT id,
code,
hh,
code || hh rr
FROM --here rr is used as foreign key which refer gg ,which can used as primary key of table2
(SELECT id,
code,
LEAD (code, 1, code)
OVER (PARTITION BY id ORDER BY ROWNUM)
hh
FROM table1)
ORDER BY code, hh) e, --hh gives the code of next row of each code of table1
( SELECT code1 || code2 gg, code3
FROM table2
ORDER BY code1, code2) b
WHERE e.rr = b.gg(+))
ORDER BY id; --here left outer join is used to get desired output
-- ORDER BY code,hh and ORDER BY code1,code2 are used to make sure that SUM(D+B)=L AND SUM(B+D)=L

Combination of group by, order and distinct

My query
SELECT a, b, c
FROM table
WHERE
a > 0 AND a < 4 AND
b IN (
SELECT z FROM table2
WHERE x = y
)
produces the following output:
A B C
1 1 Car
1 1 Keyboard
1 2 Apple
1 3 Frog
2 1 Carrot
2 2 Parrot
3 1 Doll
what I want is the following output
A B C
1 1 Car
2 1 Carrot
3 1 Doll
So basically for every A, the lowest B and associated C (as well as other columns).
I tried various join types, group bys, but I am running out of ideas.
How can I accomplish this?
Use a Top N Apply
SELECT a, b, c
FROM table
CROSS APPLY (SELECT top 1 z
FROM table2
WHERE x = y
order by z ) t2
WHERE a > 0 AND a < 4 AND
Do a join on a subquery:
SELECT a, b, c
FROM table t1
INNER JOIN (SELECT a a2, MIN(b) b2 FROM table GROUP BY a) t2
ON t1.a = t2.a2 AND t1.b = t2.b2
WHERE
a > 0 AND a < 4 AND
b IN (
SELECT z FROM table2
WHERE x = y
)

SQL for set of all in group by

Assume I have table foo
A B C
==============
1 1 1
1 2 3
1 2 4
1 3 6
2 2 6
I want the set of all C where I have a duplicate AB. Something like:
select all(C) from foo group by a, b having count(b) > 1
I want the result to be
all(C)
===
3
4
Is there an easy way to do this in Oracle SQL?
SELECT t1.c
FROM foo t1
JOIN foo t2 ON (t1.a = t2.a AND
t1.b = t2.b AND
t1.rowid != t2.rowid)
should give you what you're after. A bit more efficient would likely be to use an analytic function
SELECT c
FROM (SELECT f.*,
count(*) over (partition by a, b) cnt
FROM foo f)
WHERE cnt > 1
Try this:
SELECT C
FROM
(select C, COUNT(*) OVER(PARTITION BY A, B) AS DUPLICATES
from MY_TABLE) AS RESULTS
WHERE DUPLICATES > 1
I figured it out. I used
select f.c from foo f
join (select a, b from foo group by a, b having count(c) > 1) dupes
on dupes.a = f.a and dupes.b = f.b;

How do I return the sum for this query?

I have the following tables I need to find out the sum.
Table A
ID Name
1 Jason
2 Peter
3 Ravi
Table B
ID ID_SEC
1 11
1 12
1 13
2 21
2 22
2 23
3 31
3 32
3 33
Table C
ID_SEC Value Include_Ind
11 100 Y
12 200 Y
13 300 N
21 10 Y
22 20 N
23 30 N
31 1000 N
32 2000 N
33 3000 N
Output
ID Name Total Include_Ind_count [only count when Y]
1 Jason 600 2
2 Peter 60 1
3 Ravi 6000 0
Use:
SELECT a.id,
a.name,
SUM(c.value) AS total
FROM TABLE_A a
JOIN TABLE_B b ON b.id = a.id
JOIN TABLE_C c ON c.id_sec = b.id_sec
GROUP BY a.id, a.name
The trick to counting INCLUDE_IND only when the flag is set to 'Y' is to use CASE() to test its value:
SQL> select a.id
2 , a.name
3 , sum ( c.val) as total
4 , count( case when c.include_ind = 'Y' then 1
5 else null end ) as inc_ind_cnt
6 from a
7 join b on ( b.id = a.id )
8 join c on ( c.id_sec = b.id_sec )
9 group by a.name, a.id
10 order by a.id
11 /
ID NAME TOTAL INC_IND_CNT
---------- ---------- ---------- -----------
1 Jason 600 2
2 Peter 60 1
3 Ravi 6000 0
SQL>
The ORDER BY is necessary to guarantee sort order since Oracle changed the algorithm it uses for GROUP BY operations in 10g.
You can use inner Joins and SUM for getting the result -
Assuming you tableC.Value is int field. Else you need to cast it.
SELECT tabA.id, tabA.name, SUM(tabC.value)
FROM TABLE_A tabA
INNER JOIN TABLE_B tabB ON tabB.id = tabA.id
INNER JOIN TABLE_C tabc ON tabC.id_sec = tabB.id_sec
GROUP BY tabA.id, tabA.name