I have 2 tables like this :
table_a
id col2
1 A
1 B
2 A
2 B
3 B
table_b
id col1
1 X
2 Y
3 Z
result:
id col1 col2
1 X A_B
2 Y A_B
3 Z B
How can I achieve this?
You want listagg():
select b.id, b.col1,
listagg(a.col2, '_') within group (order by a.col2) as col2
from table_b b join
table_a a
on b.id = a.id
group by b.id, b.col1;
Related
I have 3 tables
Table A
col1
col2
A
1
B
2
C
3
Table B
col1
col2
1
x
2
y
3
z
Table C
col1
col2
x
test1
x
test2
x
test3
y
test4
y
test5
z
test6
z
test7
So my requirement is I need to get the result as
col1
col2
A
test1,test2,test3
B
test4,test5
C
test6, test7
can some one help, there is relation between the tables. I am using Oracle 19c
Thank you.
Here is what you are looking for :
SELECT
A.col1,
LISTAGG(C.col2, ',') WITHIN GROUP (ORDER BY C.col2) "col2"
FROM A
JOIN B on A.col2 = B.col1
JOIN C on B.col2 = C.col1
GROUP BY A.col1
SEE DEMO HERE
I am not asking how to delete duplicates.
I want to assign unique ID for group of duplicates:
A A
A A
A B
B B
B B
A A 1
A A 1
A B 2
B B 3
B B 3
You are looking for dense_rank():
select t.*,
dense_rank() over (order by col1, col2) as newcol
from t;
I have to join tables in a following way:
Table A:
1
2
3
4
5
Table B:
A
B
The result table should be:
1 A
2 B
3 A
4 B
5 A
Do you have any ideas how to do this?
Assuming worst case, the column in table A is not a sequence without gaps and the number of rows in table B is not known in advance, you must apply a ROW_NUMBER on both tables and then join on a MODULO:
SELECT col1, col2
FROM
(
SELECT col1,
ROW_NUMBER() OVER (ORDER BY col1) -1 AS rn
FROM tableA
) AS A
JOIN
(
SELECT col2,
ROW_NUMBER() OVER (ORDER BY col2) -1 AS rn
FROM tableB
) AS B
ON A.rn % (SELECT COUNT(*) FROM tableB) = B.rn
Maybe something like this:
select A.nr, case when (A.nr%2=0) then b2.chr else b3.chr end letter
from A, B b2, B b3
where b2.chr = 'A' and b3.chr = 'B'
lets say I have tableA which has a col1 and tableB which has col2 with the following content:
Table A
---------
A
B
C
D
Table B
---------
1
2
3
4
I want a select statement that returns to me:
A, 1
B, 2
C, 3
D, 4
I have tried making this call:
Select tableA.col1, tableB.col2 from tableA, tableB
but it returns the following:
A, 1
B, 1
C, 1
D, 1
A, 2
..
..
etc
how can i get it to just pull back this:
A, 1
B, 2
C, 3
D, 4
Try this:
select b.val1, b.val1, c.val2 from tableA a
inner join (select ROW_NUMBER() OVER () AS RowNumber, col1 as val1 from tableA) on a.col1 = b.val1
inner join (select ROW_NUMBER() OVER () AS RowNumber, col2 as val2 from tableB) c on c.RowNumber = b.RowNumber
I need to group by ,even if is there a difference in values for a column by converting to a text value and needs that text value to be included in select list
How is this possibe?
SELECT col1,col2
FROM
(
SELECT col1,col2 FROM table1
UNION ALL
SELECT col1,col2 FROM table2
UNION ALL
SELECT col1,col2 FROM table3
)tbl
GROUP BY tbl.col1
,tbl.col2
here col2 may or may not be a text value or int, if it is text value i need to convert all other col2 ints to text and do a group by
See this example :
Table 1
-------
A B 3 C
A B var C
Table 2
-------
UNION ALL
B B 3 C
B B var C
Table 3
-------
UNION ALL
B B 3 C
B B 3 C
Result of each table should be
-------
Table 1
-------
A B var C (since there is a var in the any of the row in that column in that table)
Table 2
-------
B B var C (since there is a var in the any of the row in that column in that table)
Table 3
-------
B B 3 C (here it remains 3 since there is not value call var in any of the row in that column)
Result
------
A B var C
B B var C (since there is a var in the any of the row in that column in that table)
This seems a little to easy, maybe i am missing something? Are you just looking to do a CAST on col2?
SELECT col1
,CAST(col2 AS nvarchar(100)) as Col2
...etc
tAKE THIS:
SELECT col1,col2
FROM
(
SELECT col1,col2 FROM table1
UNION ALL
SELECT col1,col2 FROM table2
UNION ALL
SELECT col1,col2 FROM table3
)tbl
where col2 not like '3%'
GROUP BY tbl.col1
The Result look like your question:
A B var C
B B var C