Group by problem - sql

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

Related

How to select the total count?

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;

Oracle - Join two tables by concatenating repeating rows

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;

SQL Server : join all left table and repeat right

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'

SQL to get distinct records with PK

Here is my table data...lets call this table TABLEX
ID COL1 COL2
------------------------------
100 a b
101 x y
102 a b
103 c d
104 e f
105 a b
106 c d
107 x y
I want following records to be retrieved from this table
ID COL1 COL2
------------------------------
100 a b
101 x y
103 c d
104 e f
In other words I want to retrieve distinct values from COL1 + COL2 but also show along with records' ID.
select min(id) as id, col1, col2
from Tablex
group by col1, col2
I have observed the you return the lowest ID for every the same col and col2.
SELECT MIN(ID) `ID`, col1, col2
FROM tableName
GROUP BY col1, col2
You haven't said how you want to select which record ID to display for a given set of values. Your sample implies you want the lowest one.
SELECT MIN(id) as id, col1, col2
FROM your_table
GROUP BY col1,cold2
ORDER BY MIN(id)

Select columns from different tables, without creating a combination of different rows?

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