how to do group by on certain fields only in sql - sql

I have the following query
select a,b,sum(c) from(
select a,b,c from table 1
union
select a,b,c from table 2
)
group by a,b
result of above sql is as follows:
A B C
Apple Red 100
Apple null 100
Pear Green 200
Pear null 300
instead of above result how can i get the following result:
A B C
Apple Red 200
Pear Green 500

Given your desired results, you want to GROUP BY your first column (A) and have a single value for your second column (B). You can use another aggregate such as max on the second column and remove it from the GROUP BY:
select a,max(b),sum(c) from(
select a,b,c from table1
union
select a,b,c from table2
) t
group by a
SQL Fiddle Demo
Note, if you have multiple distinct non-null values per group, this will sum them all to a single max group.

Related

SQL select query not as intended

I want to select the rows that column a is the same, but column b has different values (while removing duplicates in column b, so only one row represent the rows that have the same value in column b)
Table:
a b
-- --
1 111
1 111
1 222
1 333
1 222
2 523
5 323
I tried this query:
SELECT * FROM table GROUP BY b HAVING a = 1;
which returns what i want with current inputs:
output
1 111
1 222
1 333
however if the result is one row, nothing is selected.
for example:
SELECT * FROM table GROUP BY b HAVING a = 2;
doesn't return anything, and I want it to return one row.
2 523
what is the problem with the query?
Thanks.
It looks like all you really need is distinct, eg
select distinct a, b
from t
where a=2;
Your query is invalid in most databases because you are using a non-aggregated value in the HAVING clause.
Instead, you should check if each group contains only 1 distinct vale:
SELECT MAX(a) a, b
FROM tablename
GROUP BY b
HAVING COUNT(DISTINCT a) = 1 AND MAX(a) = ?;
Replace ? with the value that you search for.
See the demo.

Compare one column value between row number 1 and row number 2

I have the following table basket for example.
basket fruit quantity
1 mango 2
1 apple 2
2 banana 2
2 banana 3
2 banana 3
Now I have to find the baskets which have more than 1 row and in the basket the types are different to each other. So basket number 1 should come out.
I have written the following SQL:
select count(*),c.basket from baskets c group by c.basket having count(*)>1;
But after this how can I get the baskets where the fruit types are different to each other among the rows? It should be basket number 1 in this case.
Just add to the HAVING clause:
select count(*), c.basket
from baskets c
group by c.basket
having count(*)>1
AND COUNT(DISTINCT fruit)>1;
I would use min() and max():
select b.basket
from baskets b
group by b.basket
where min(b.fruit) <> max(b.fruit);
I would use exists :
select b.*
from baskets b
where exists (select 1
from baskets b1
where b1.basket = b.basket and
b1.fruit <> b.fruit
);

Get duplicate on single column after distinct across multiple columns in SQL

I have a table that looks like this:
name | id
-----------
A 1
A 1
B 2
C 1
D 3
D 3
F 2
I want to return id's 1 and 2 because they are duplicate on names. I don't want to return 3, because it is distinct for D 3.
Basically, I'm thinking of doing a query to first get a distinct pairing, so the above reduces to
name | id
-----------
A 1
B 2
C 1
D 3
F 2
And then doing a duplicate find on the id column. However, I'm struggling to find the correct syntax to construct that query.
You should be able to get the result you want by using a GROUP BY along with a HAVING clause that counts the distinct names. The HAVING clause will filter for those ids that have more than one distinct name:
select id
from Table1
group by id
having count(distinct name) > 1
Here is a demo

Find Duplicate in column b for each value in column A Oracle

I have Table like Below:
Sr_No C_A C_B
-------------------
1 100 A
2 100 A
3 100 B
4 101 A
5 102 A
6 102 B
7 103 A
8 103 A
9 103 B
And I want select query to get below
C_A
----
100
103
I want to know how many records are in C_A column having duplicate values in C_B column.
The query below checks, for each C_A group, that the number of distinct C_B values is less than the total number of values. This condition implies that all C_B values are not unique for a given C_A group, and that there are duplicates.
SELECT C_A
FROM yourTable
GROUP BY C_A
HAVING COUNT(DISTINCT C_B) < COUNT(*)
If I understand well, you may need:
select distinct C_A
from
(select C_A, C_B, count(1) over ( partition by C_A, C_B) as cnt
from test
)
where cnt > 1
The nested query counts the number of duplicates for each couple of values in C_A, C_B, while the external one simply filters this result to only get couples with duplicates

SQL query for selecting sum of multiple column based on other column

i have in database like this
say a table X with columns A and B
X
--
A B
1 300
1 400
1 100
2 200
2 400
and i want to write sql query to get result in format
P Q
1 800
2 600
Where Q is sum of all having same value of A.
I thought of selecting the data in same format and then using the logic display it
is there a way to do with SQL query?
SELECT A as P, SUM(B) AS Q
FROM X
GROUP BY A
select A as P
, sum(B) as Q
from X
group by
A
SELECT A AS P, SUM(B) AS Q
FROM TableA
GROUP BY A