sql-Select rows with same id but different value in another column - mysql

Input:
Column A
Column B
1
A
1
B
1
C
2
A
2
B
3
A
3
B
3
c
4
A
5
A
5
B
6
A
Output
ColumnA
4
6.
for columnA for every first of values is A, i need to fetch columnA those values have only A not in B,C

You can use a correlated subquery with NOT EXISTS to exclude column A values that have matching rows with the disallowed values in column B.
SELECT columnA
FROM yourTable AS t1
WHERE columnB = 'A'
AND NOT EXISTS (
SELECT *
FROM yourTable AS t2
WHERE t1.columnA = t2.columnA
AND t2.columnB IN ('B', 'C')
)

Related

Get count of all types of values in a column obtained in the same SELECT SQL query

MySQL Version: 5.7.36
I'm attempting to minimize the amount of queries I have to execute.
Right now, I'm executing a query similar to this:
SELECT
TABLE 1.column1 as "A",
TABLE 1.column2 as "B"
FROM
TABLE 1
WHERE
CONDITION
I can obtain the results I need from this query, however I would also like to obtain the count of what type of values show up in the same query.
For example, if the following query retrieves this table
A B
- -
1 a
1 b
1 c
2 d
3 e
4 f
4 g
I would also like to, for each row, obtain the count of all rows retrieved with its column "A" that matches its value.
Would it be more efficient to execute another query to get that result or can I modify my obtaining query to get this statistic?
Desired result:
A B C
- - -
1 a 3 # 3 rows with "1" in Column "A"
1 b 3
1 c 3
2 d 1
3 e 1
4 f 2
4 g 2
UPDATE:
The closest query I could find goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column1
Results in this:
A B C
- - -
1 a 3
2 d 1
3 e 1
4 f 2
However, it removes any other rows with the same value in column "A". Even if it has different values in column "B". I would like to have all rows present in my SQL query with its corresponding row count.
The next closest query I found goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column2
Results in this:
A B C
- - -
1 a 1
1 b 1
1 c 1
2 d 1
3 e 1
4 f 1
4 g 1
Which also isn't achieving the desired result.
You have to join with the subquery that gets the counts.
SELECT t1.column1 AS A, t1.column2 AS B, t2.count
FROM Table1 AS t1
JOIN (
SELECT column1 AS A, COUNT(*) AS count
FROM Table1
GROUP BY column1
) AS t2 ON t1.A = t2.A
SELECT
c,
COUNT(*) OVER (PARTITION BY c)
FROM t
ORDER BY c

MySQL query for selecting distinct rows with all possible values in a column

Here's my DB table named 'test_tbl':
id
type
1
A
1
B
1
C
1
D
2
A
2
B
2
C
2
D
3
A
3
B
4
A
4
D
Here every 'id' can have at most 4 possible values (A,B,C,D) for 'type' column. I want to find out those ids who don't have all four values in 'type' column. So my expected output should be ids (3,4). I have tried as following:
select DISTINCT id
from test_tbl
where id NOT IN
(SELECT id FROM test_tbl
where
type='A' and type='B' and type='C' and type='D');
But this is giving output all the ids from table.
Use aggregation:
select id
from test_tbl
group by id
having count(distinct type) <> 4;
If you can have types other than A, B, C, and D, then add:
where type in ('A', 'B', 'C', 'D')

MySQL Add interger of multiple columns together as result [duplicate]

I have a table with 3 columns (A,B,C). I want to select some rows from the table and then the MySQL to return a single row having the values added on each column.
A B C
1. 2 2 2
2. 4 4 4
3. 6 7 8
MySQL should return in this case, if I select all the three rows:
A B C
1. 12 13 14
select sum(A),sum(B),sum(C) from mytable where id in (1,2,3);
select
sum(a) as atotal,
sum(b) as btotal,
sum(c) as ctotal
from
yourtable t
where
t.id in (1, 2, 3)
Try this:
select sum(a), sum(b), sum(c)
from your_table

Insert rows from one table to another but only those rows that have no duplicates

I've seen this, this, this, this and this but my question is different.
I have a Table1:
id c a b rc bid
1 12 4 6 35 4
2 12 4 6 67 7
3 12 4 6 88 8
4 23 4 7 49 3
5 23 5 8 59 8
Table2 also has the same columns but does not have bid column.
A row is considered a duplicate if it has the same values of columns c, a and b. So rows 1, 2 and 3 are considered duplicates because they have 12, 4 and 6.
I want to insert rows of Table1 to Table2, but only those rows that are not duplicates. Which means that rows 1, 2 and 3 won't get inserted to Table2. Only rows 4 and 5 will get inserted because they have no duplicates.
So Table2 will look like this after the inserts:
id c a b rc
1 23 4 7 49
2 23 5 8 59
I know I can get which rows have no duplicates using this query:
select distinct c,a,b,count(*) from Table1 group by c,a,b having count(*) > 1
But am not able to figure out how to insert these to Table2 because the insertion requires specific columns to be specified.
Tried something like this which obviously doesn't work:
insert into Table2 (c, a, b, rc) select distinct c,a,b,count(*) from Table1 group by c,a,b having count(*) > 1
You can use also not in in subselect
INSERT INTO Table2(c, a, b, rc, bid)
SELECT c, a, b, rc, bid
FROM Table1 t1
WHERE (c,a,b) not in ( SELECT c,a,b
FROM Table1 t2
GROUP BY c, a, b
HAVING COUNT(*) > 1
)
You can use NOT EXISTS to exclude duplicate rows:
INSERT INTO Table2(c, a, b, rc, bid)
SELECT
c, a, b, rc, bid
FROM Table1 t1
WHERE NOT EXISTS(
SELECT 1
FROM Table1 t2
WHERE
t2.c = t1.c
AND t2.a = t1.a
AND t2.b = t1.b
HAVING COUNT(*) > 1
)
The HAVING COUNT(*) > 1 will check if there are duplicates.
insert into table2 (c,a,b,rc)
select c,a,b,rc from table1
where id in (select distinct id
from Table1 group by c,a,b having count(*) = 1)
There are many ways to do that. You have already got so many correct answers. Here, I am giving the query based on the way you approached.
INSERT INTO Table2 (c, a, b, rc)
SELECT
c,
a,
b,
rc
FROM
Table1
GROUP BY c, a, b
HAVING count(*) = 1;

MySQL sum elements of a column

I have a table with 3 columns (A,B,C). I want to select some rows from the table and then the MySQL to return a single row having the values added on each column.
A B C
1. 2 2 2
2. 4 4 4
3. 6 7 8
MySQL should return in this case, if I select all the three rows:
A B C
1. 12 13 14
select sum(A),sum(B),sum(C) from mytable where id in (1,2,3);
select
sum(a) as atotal,
sum(b) as btotal,
sum(c) as ctotal
from
yourtable t
where
t.id in (1, 2, 3)
Try this:
select sum(a), sum(b), sum(c)
from your_table