I have following table in Oracle:
Id Acct Name
==================
1 123 Anyone
1 234 Anyone
2 435 Someone
2 675 Someone
2 732 Someone
3 765 Anonymous
4 987 Hidden
4 987 Hidden
and I need the following output:
Id Acct1 Acct2 Name
==========================
1 123 234 Anyone
2 435 675 Someone
2 435 732 Someone
So, I only need to show the records with non-matching Acct#, but both the account numbers in one line as above.
Can someone help ?
Use a self join:
select t1.id, t1.name, t1.acct, t2.acct
from t t1 join
t t2
on t1.id = t2.id and t1.name = t2.name and t1.acct <> t2.acct;
Or, if you can have more than two accounts, then perhaps listagg() is more appropriate:
select t.id, t.name, listagg(acct, ',') within group (order by acct)
from t
group by t.id, t.name
having min(acct) <> max(acct);
Related
This is my data:
id
key
nbr of subs
1
ABC
10
1
XXX
3
2
MNO
120
3
ABC
5
3
FGH
110
I need the key for the record (ID) that has the max nbr of subscriptions:
id
key
nbr of subs
1
ABC
10
2
MNO
120
3
FGH
110
I don't mind deleting the extra records, or electing the ones I need and insert them into other table. Any ideas?
SELECT P.Key, MAX(P.[Nbr of Subcriptions])
FROM P
GROUP BY P.Key;
Thank you very much
You need correlated subquery. Try below SQL-
SELECT t1.ID, t1.Key, t1.NBR FROM Table1 as t1
INNER JOIN (SELECT Table1.ID, Max(Table1.NBR) AS MaxOfNBR
FROM Table1 GROUP BY Table1.ID) as t2 ON (t1.NBR = t2.MaxOfNBR) AND (t1.ID = t2.ID);
I have 3 tables as shown below.
t1:
id runs
001 1200
020 600
123 1500
t2:
id wickets
008 4
030 7
123 0
020 6
t3:
id catches
007 4
030
123 2
040 6
I would like to perform FULL OUTER JOIN of all the three tables and prepare the as shown below.
Expected output:
id runs wickets catches
001 1200
020 600 6
123 1500 0 2
008 4
030 7
007 4
040 6
I tried below code and did not works.
SELECT *
FROM t1
FULL OUTER JOIN t2
ON t1.id = t2.id
FULL OUTER JOIN t2.id = t3.id
I did the same using pandas using following code and it worked well.
from functools import reduce
dfl=[t1, t2, t3]
df_merged = reduce(lambda left,right: pd.merge(left,right,on=['id'],
how='outer'), dfl)
You can select the expected columns you want to obtain from each table:
SELECT coalesce(t1.id,t2.id, t3.id), t1.runs, t2.wickets, t3.catches
FROM t1
FULL OUTER JOIN t2 ON t1.id = t2.id
FULL OUTER JOIN t3 ON COALESCE(t1.id, t2.id) = t3.id
You could use UNION ALL in a sub-query and then GROUP BY.
This would give you zeros where there is no value. If this is a problem we could modify the presentation.
If you have another table with all the players we could us LEFT JOIN onto the three tables with
WHERE runs <>'' OR wickets <> '' OR catches <> ''
select
sum(runs) "runs",
sum(wickets) "wickets",
sum(catches) "catches)
from(
select id, runs, 0 wickets, 0 catches from t1
union all
select id, 0, wickets,0 from t2
union all
select id, 0,0, catches from t3
)
group by id,
order by id;
I have a table T1 as below
product_id val
123,567 5
999 4
999 3
and another table T2,
t_product_id // this maps to product_id in above table
123
999
In the final output, for t_product_id in table T2 I have to get value for it from T1. For duplicate product_ids (999) I want to get the min value, and for 123 I want to get 5
This is how output should look like
product_id value
123 5
999 3
My query ->
select t1.product_id, min(t1.value)
from T1 t1
group by t1.product_id
I am not sure what needs to be done next. How to separate comma separated values and check if 123 from T2 exists in T1 and get the value for it
it's not possible to keep only one product_id per row in table T1?
I think this would simplify matters for you. T1 would be:
123 | 5
567 | 5
999 | 4
999 | 3
Use join to only select the ids that exist in T2
select t1.product_id, min(t1.value)
from T1 t1 join T2 on (t1.product_id = t2.t_product_id)
group by t1.product_id
I have two tables:
table1
ID | Name | Code
1 Joe 123
2 Sam 674
3 Mike 321
table2
ID | User Name| Code
1 Joe 123
2 Sam 674
3 Mike 321
4 John 457
5 Tim 235
Desired result:
4|John|457
5| Tim|235
Tabe1 and table2 code is identical. Table 1 code is a new field added thus contains no data for any record. Using the IDs as keys I took the codes from table2 and populated them in table1. However table1 has considerably less IDs then table2 so table2 has more codes then table1. I want to query which codes did not get transferred to table1. I thought it would be as simple as:
select *
from table2 t2
where t2.Code is not null and
t2.Code not in (select t1.Code from table1 t1 where t1.Code is not null);
This returns nothing which is strange to me. What do I need to adjust in this query? This is for oracle.
You might try the following:
SELECT t2.*
FROM table2 t2
WHERE NOT EXISTS ( SELECT 1 FROM table1 t1
WHERE t1.code = t2.code );
A particular query of mine results data this way.
Id Size
123 1
123 1
123 2
123 2
134 1
134 1
134 2
I want the results get me the count eliminating the duplicate size like
Id Size
123 1
123 2
134 1
134 2
Above was result of joining two tables. Problem is I cant use distinct in this case.
Here is how tables are
Table1:
Id Created ... .. .. ..
123 date1 ....
134 date2 ....
Table2:
Id Size
123 1
123 2
134 1
134 2
I have my query that select from Table1 based on CreatedDate, its like this
select count(*)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''.
How do you get the distinct sizes.
If I use select count(distinct table2.size), it only returns 1 and 2 for all rows.
SELECT DISTINCT Id, Size
FROM table1
This should give you a list of distinct Id and Size combinations.
select count(distinct table1.id, table2.size)
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
see it working live in an sqlfiddle
Sometimes the solution is so obvious... :)
UPDATE: another way
select count(*) from (
select distinct table1.id, table2.size
from table1
join table2
on table1.id = table2.id
where table1.creates between '' and ''
) sq