SQL select query not as intended - sql

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.

Related

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

SQL combining of a COUNT with a WHERE in single query

Here is the data, call it table T
A B
-- --
1 14
2 15
3 16
4 1
4 3
4 6
4 9
4 12
4 15
I would like to get the value of A that has only one value and a B value of 15.
There are two rows where B=15 but there are 6 rows where A=4 and only one row where A=2.
So the correct SQL should return me the 2.
I have tried this but it returns both rows.
select A from T group by A,B having Count(A) = 1 and B = 15
This similarly fails:
select A from T where B = 15 group by A having count( A ) = 1
Try this:
select A
from T
group by A
having Count(A) = 1 and Max(B) = 15;
Your problem seems to be that you are grouping by both columns. You only want to group by A.
Admittedly, your query has group by A, T, but I think that is a typo, based on the described behavior.
You can check the count of B after grouping by A.
select A
from T
group by A
having Count(B) = 1 and max(B) = 15

how to do group by on certain fields only in 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.

Get rows with single values using SQlite

By using SQlite, I'd like to get all rows that show in a specific column only one single distinct value. Like from following table:
A B
1 2
2 1
3 2
4 3
5 1
6 1
7 2
8 4
9 2
Here I'd like to get only row Nr. 4 an 8 as there values (3 and 4) occur only once in the entire column.
You could use a query like this:
SELECT *
FROM mytable
WHERE B IN (SELECT B FROM mytable GROUP BY B HAVING COUNT(DISTINCT A)=1)
Please see fiddle here.
Subquery will return all B values that are present only once (you could also use HAVING COUNT(*)=1 in this case), the outer query will return all rows where B is returned by the subquery.

Count number of not exist in child table

Essentially what I'm trying to do is count the number of rows something doesn't exist in an audit/history table. I'd like the following query to return a count of one per detail. Currently it gives me one per row in the history table.
--Detail Table
ID DETAIL_GROUP
1 A
2 B
3 B
--Detail History Table
DETAIL_ID_FK VALUE1
1 NOT_MATCH
1 NOT_MATCH
2 MATCH
2 NOT_MATCH
3 MATCH
3 NOT_MATCH
SELECT D.DETAIL_GROUP, COUNT(*)
FROM DETAIL D
WHERE (NOT EXISTS(
SELECT NULL
FROM DETAIL_HISTORY HI
WHERE HI.D_ID_FK = D.ID
AND HI.VALUE1 = 'MATCH'))
GROUP BY D.DETAIL_GROUP;
I'd like to see the following result:
DETAIL_GROUP COUNT(*)
A 1
but I'm receiving the following result:
DETAIL_GROUP COUNT(*)
A 2
Thank you in advance for any assistance provided.
Assuming that your detail table is as follows:
D_ID VALUE1
1 MATCH
1 NOT_MATCH
2 MATCH
2 NOT_MATCH
3 MATCH
3 NOT_MATCH
The below query:
SELECT d.detail_group, count(*)
FROM detail d
JOIN detail_history dh ON dh.d_id = d.id
WHERE dh.value1 = 'MATCH'
GROUP BY d.detail_group
Would produce:
DETAIL_GROUP COUNT(*)
A 1
B 2
The above query creates the groups matching the ids and then goes into each group and restricts the items based on value1.