How to select the total count? - sql

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;

Related

Count distinct by link on sql?

I'm trying to count distinct by the link between two columns.
Here is the example.
rownum
type
id
1
A
a
2
A
b
3
B
b
4
B
c
5
C
c
6
C
d
If I count distinct by type column, it returns 3. However, what I'd like to do is to consider rownum 2 and 3, 4 and 5 are not distinctive because they got the same value on id column.
To rephrase,
type
array of id
A
a, b
B
b, c
C
c, d
Since A and B got same b, and B and C got same c on their arrays, it would return 1 as a result.
I have no idea where to start. Would appreciate if I can get any hint or something.
Consider below:
you might use STRING_AGG
WITH TMP_TBL AS
(
SELECT 1 AS ROWNUM, 'A' AS TYPE, 'a' AS ID UNION ALL
SELECT 2,'A','b' UNION ALL
SELECT 3,'B','b' UNION ALL
SELECT 4,'B','b' UNION ALL
SELECT 5,'C','c' UNION ALL
SELECT 6,'C','d'
);
SELECT DISTINCT TYPE,N_ID
FROM
(
SELECT TYPE,STRING_AGG(ID)OVER(PARTITION BY TYPE) AS N_ID FROM TMP_TBL
)

How to assign unique ID for group of duplicates?

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;

SQL, count in multiple columns then group by

I am trying to count in multiple columns then group by for a total sum where the same data appears in any column
Source data table:
P1 P2 P3
-----------
a b
a a a
b c
a b b
b a
I want it to show something like this:
Desired query output:
Total
-------------
a | 6
b | 5
c | 1
You can use a union query
SELECT x.f1,Count(x.f1) FROM
(SELECT p1 As F1 FROM table
UNION ALL
SELECT p2 As F1 FROM table
UNION ALL
SELECT p3 As F1 FROM table) x
GROUP BY x.f1
You can union all the records in a subquery and on the outer query, count each value.
SELECT b.a, COUNT(b.a)
FROM
(
SELECT P1 a
FROM tableName
UNION ALL
SELECT P2 a
FROM tableName
UNION ALL
SELECT P3 a
FROM tableName
) b
GROUP BY b.a

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

Group by problem

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