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')
)
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
I have a table having three columns:
A B C
1 2 2
2 2 2
3 1 1
4 1 2
I want the count of those values which have C equal to 2 but with distinct values of B
So in this case for C = 2, count = 2 (B=2 and B=1)
I used the following command:
Select count(*) from mytable where C=2 group by (B)
but it yields:
count(*)
3
I have tried using "distinct" but it can't be use to select from one column
Have you tried
SELECT COUNT(DISTINCT B) FROM mytable WHERE C = 2;
Use sub query like this:
Select count(*) from (
select distinct B where c=2
)
TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.
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;