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
Related
I have the following two tables (postgresql)
tableA
a b
----------
1 A
2 B
table B
c b
----------
1 A
3 B
I want to find out the same number of columns b, but if column a and column c are the same, count one.
So the final result should be
b count
----------
A 1
B 2
How should I write sql?
You need union all for the 2 tables and then group by b to count distinct values of a:
select t.b, count(distinct t.a) counter
from (select * from tablea union all select * from tableb) t
group by t.b
Aggregate by column b and take the distinct count of column a:
SELECT b, COUNT(DISTINCT a) AS count
FROM yourTable
GROUP BY b
ORDER BY b;
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;
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'
I have following tables;
A B A B
_____ _____
1 t 7 a
2 r 5 d
3 e 3 e
4 f
5 d
6 s
7 a
And, output should be ;
A B
_____
1 t
2 r
4 f
6 s
In other words I want difference of these two tables . I want region A in this figure.
How can I do that ?
Try this:
SELECT t1.*
FROM t1
LEFT JOIN t2 USING (A, B)
WHERE t2.A IS NULL
without using JOIN
SELECT A, B
FROM tableA
WHERE A NOT IN
(SELECT Distinct A FROM tableB)
This should do the trick:
SELECT
A,
B
FROM Table_1
WHERE NOT EXISTS
(
SELECT Table_2.A AS Test
FROM Table_2
INNER JOIN Table_1 AS T ON Table_2.A = Table_1.A
)
SELECT A, B
FROM Table1
EXCEPT
SELECT A, B
FROM Table2;
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