I have the following scenario
Table1
Row
Col1
Col2
1
[12345, 32145, 647834]
abcd
2
[23145, 11111, 12345]
efg
3
[647834, 23145]
zyx
4
[11111]
cba
Table2
Row
valuesforcol1
names
1
12345
Jon
2
32145
Bob
3
647834
Jack
4
23145
Dave
5
11111
Alice
Result
Row
Col1
Names
1
[12345, 32145, 647834]
[Jon, Bob, Jack]
2
[23145, 11111, 12345]
[Dave, Alice, Jon]
3
[647834, 23145]
[Jack, Dave]
4
[11111]
[Alice]
How do I do that?
If you were using BigQuery, then the logic would look like:
select t1.*,
(select array_agg(t2.name order by n) as names
from unnest(t1.col1) el with offset n join
table2 t2
on el = t2.valuesforcol1
) as names
from table1 t1
Related
I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D
I need to join two tables together where the seccond table has one of the first tables in a string.
e.g
Table1 has ID1, ID2, Lang_id, User, Text.
Table2 has ID_a, User, List_ID2.
Additionally, the List_ID2 is comma seperated, so I have to make sure I get it in all cases, so if it is the only number, or the at beginning, middle or end of the string. BUT NOT truncate the values, i.e. (10 is not 100 - '10'0)
The extra kicker is that Lang_id is the language of the Place and I also have to seperate that.
I have the language seperated fine, I can't get the ID_a into my results
i.e
TABLE 1
ID1 ID2 Lang_id User Text
1 2 1 bob Me
1 2 2 bob Mich
1 2 3 bob Mi
2 1 1 bob You
2 1 2 bob Du
2 1 3 bob usted
3 1 1 tim You
3 1 2 tim Dich
3 1 3 tim le
4 3 1 tim Hello
4 3 2 tim Hallo
4 3 3 tim ihola
TABLE 2
ID_a User List_ID2
100 bob 1, 2
200 tim 1, 3
RESULTS
ID_a ID1 ID2 English German Spanish
100 1 1 You Du usted
100 2 2 Me Mich Mi
200 3 1 You Dich le
200 4 3 Hello Hallo ihola
My statement looks a little like this:
SELECT DISTINCT main.ID1, main.ID2, ID_a
(SELECT Text
FROM table1 AS a
WHERE lang_id = 1
AND main.ID1 = a.ID1
AND main.ID2 = a.ID2) AS English,
(SELECT Text
FROM table1 AS b
WHERE lang_id = 2
AND main.ID1 = b.ID1
AND main.ID2 = b.ID2) AS German,
(SELECT Text
FROM table1 AS c
WHERE lang_id = 3
AND main.ID1 = c.ID1
AND main.ID2 = c.ID2) AS Spanish,
FROM table1 AS main
LEFT OUTER JOIN table2 ON table2.User = main.User
AND (table2.List_ID2 LIKE STR(ID2)
OR table2.List_ID2 LIKE (ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2)
The first part is working fine (the language is seperated), but I can't get the ID from table2, I've tried a few differnt methods, and the above comes back without an error, but alot of NULL values.
Found the answer here:
SQL STR() function equality
I needed to specify the Length of the STR()
I have two tables:
Table1
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
Table2
id name qty
1 Tedd 2
2 Jim 2
3 Sally 2
4 Victoria 1
5 Alex 9
I need to select all the rows from Table1. However, if a row exists in Table2 that doesn't exist in Table1, I need to include that in the result set. So, in the end, my query should return this:
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
5 Alex 9
Is there a way I can do this? Thanks.
You can use a FULL OUTER JOIN:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.name, t2.name) name,
coalesce(t1.qty, t2.id) qty
from table1 t1
full outer join table2 t2
on t1.id = t2.id
See SQL Fiddle with Demo
I have a mapping table tableA
key value
-----------
1 "John"
2 "George"
3 "Kate"
4 "loves"
5 "hates"
and another tableB that contains rows based on keys of tableA
col1 col2 col3
------------------
1 5 2
2 4 3
3 4 1
I want to write a selection query which will return rows from table B but replaced with their appropriate values.
e.g. the output has to be:
John | hates | George
George | loves | Kate
Kate | loves | John
Thank you.
SELECT A1.value, A2.value, A3.value
FROM tableB
JOIN tableA as A1 ON tableB.col1 = A1.key
JOIN tableA as A2 ON tableB.col2 = A2.key
JOIN tableA as A3 ON tableB.col3 = A3.key;
You should probably put the last 2 items, 'loves' and 'hates', into a separate table as they represent a different type of data than the other 3.
Here are the tables:
users:
id name
----------
1 John
2 Edward
3 Kate
feelings:
id type
----------
1 love
2 hate
feelings_users:
id user1_id feeling_id user2_id
-----------------------------------
1 1 2 2
2 2 1 3
3 3 1 1
and here's the query:
select `Ua`.`name`, `F`.`type`, `Ub`.`name` from `feelings_users` as `X`
left join `users` as `Ua` on `X`.`user1_id` = `Ua`.`id`
left join `feelings` as `F` on `X`.`feeling_id` = `F`.`id`
left join `users` as `Ub` on `X`.`user2_id` = `Ub`.`id`
and it will output:
name type name
--------------------
John hate Edward
Edward love Kate
Kate love John
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how can i use distinct INNER JOIN RFM ON RFM.RFMID=PV.RFMID
Table MMASTER
MID col1
1 abc
2 xyx
3 pqr
Table AMM
AMMID MID col1 col2
1 1 bnb mfk
2 1 def rwr
3 2 re wrwr
Table PS
UID VTID AMMID SMID col1 col2
1 1 1 1 rkk jdj
2 2 3 3 kdf lfl
6 2 2 4 rgr rtr
Table PV
VTID PMID RFMID
1 2 1
2 2 3
7 2 2
Table RFM
RFMID title name
1 mr john
2 mr jack
3 mr jim
Table PM
PMID col1 col2
1 df ere
2 rwe rwer
3 rwr fwr
Table SM
SMID MMID col1 col2
1 1 fdf efe
2 1 ddf dfdf
3 2 df ef
I get result like this after fire above query
PMID title name PV.RFMID
2 mr. jim 3
2 mr. jim 3
2 mr. jim 3
***BUT RESULT SHOULD BE LIKE THIS***
PMID title name PV.RFMID
2 mr. john 1
2 mr. jim 3
2 mr. jack 2
In my query PV.RFMID getting duplicate value when join with RFM and due to that title and name getting duplicate
This will give you your desired Output:
select pm.pmid, rfm.title, rfm.name, pv.rfmid from PM as PM
inner join PV on pm.pmid = pv.pmid
inner join RFM as rfm on pv.rfmid = rfm.rfmid
WHERE PM.PMID='2'
EDIT
Considering that you need to Join all tables, it's something tricky.
I mean, checking RFMtable, you can see the RFMId, related to PV table, and see that the VTID has a 7 value.. which is not related to anything ! So, if you need to join all tables, you won't get that result
I Am assuming that table "PM" is your transaction table where one "PMID" can have multiple Entries, thus Adding a Distinct Clause Before your Query will Get you the Desired Out Put.
Try This:
SELECT DISTINCT PM.PMID,RFM.TITLE,RFM.NAME ,PV.RFMID FROM MMASTER
INNER JOIN AMM ON MMASTER.MID=AMM.MID
INNER JOIN PS ON AMM.AMMID=PS.AMMID
INNER JOIN PV ON PV.VTID=PS.VTID
INNER JOIN RFM ON RFM.RFMID=PV.RFMID
INNER JOIN PM ON PV.PMID=PM.PMID
INNER JOIN SM ON PS.SMID=SM.SMID
WHERE PM.PMID='2'