MySQL count on Grouping - mysql

I want to count the number of times a grouping occurs in a query.
SELECT COL1,
COL2,
*(count COL1)*
FROM TABLE
GROUP BY COL1, COL2
So I'm expecting to see the results like so:
COL1, COL2, Count
A, A, 1
A, B, 2
B, A, 1
B, B, 2
B, C, 3
Count(*) will give me the sum of the grouped row which is what I don't want, but I just cant seem to get my head round it or find a way to do it. Anyway, thanks in advance.

You should add auto_increment column to make an order. After that, try something like this:
SELECT a.col1, a.col2,
(SELECT Count(*) From T1 c
WHERE c.col1 = a.col1
AND c.ID <= a.ID) as count
FROM T1 a;
SQL Fiddle
OR Without auto_increment Try this:
SELECT col1, col2,
(select count(*) from T1 t2
where t2.col2 <= t1.col2 and
t2.col1 = t1.col1
) as Enumeration
FROM T1 t1
GROUP BY col1, col2;
SQL Fiddle

Your query (with the parentheses appropriately placed) does what you want:
SELECT COL1, COL2, count(*)
FROM TABLE
GROUP BY COL1, COL2;
If you want the sum of a column, then use the sum() function.
EDIT:
If you are trying to enumerate the groups (despite what the question is asking in rather clear English), you can do:
SELECT col1, col2,
(select count(*)
from table t2
where t2.col1 = t.col1 and
t2.col2 <= t.col2
) as Enumeration
FROM table t1
GROUP BY col1, col2;

Related

With As Clause Not recognized MySQL

I am trying to use a with as clause in a query.
WITH NAME AS
(
SELECT col1,
col2
FROM TABLE1
)
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (NAME.col1)
The query placed in the with as clause works on it's own but I get this error:
Unknown column NAME.'col1' in 'where clause'
I am more familiar with oracle so this may have caused some error by carrying something over.
Is the order of the clauses wrong and With As comes after where?
I think you do not need the CTE.
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (SELECT col1 FROM TABLE1)
should work
If you want to use CTE (for what reason ever), you have to select from the table
WITH NAME AS
(
SELECT col1,
col2
FROM TABLE1
)
SELECT col2,
col3
FROM TABLE2
WHERE col2 in (select col1 from name)

MySQL: update columns in the table based on criteria

I have this query
SELECT *, min(id)
FROM mytable
WHERE col2='specval'
GROUP BY col3
that selects rows I need.
I would like to set
UPDATE mytable
set col4 = col3+1000
for the rows selected above respectively.
So, how to update that rows by setting col4 in that table as col4=col3+1000 only for each selected row respectively?
Thank you.
you can use a query like this:
UPDATE mytable
set col4 = col3+1000
WHERE id in (
SELECT * FROM (
SELECT min(id)
FROM mytable
WHERE col2='specval'
GROUP BY col3
) as myids
);
You can use UPDATE with JOIN:
UPDATE mytable t1
INNER JOIN
(SELECT
col3, MIN(id) id
FROM
mytable
GROUP BY col3) t2 ON t1.col3 = t2.col3 AND t1.id = t2.id
SET
t1.col4 = t1.col3 + 1000;
You can use WHERE in update clause too. So depending on what "selected" means to you ( I'm guessing from your SELECT ), you might do something like this:
UPDATE mytable
SET col4 = col3+1000
WHERE col2 = 'specval';

mysql storing subquery in column and performing where condition

i am performing a sub query in mysql which is like
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
how should i get data in where clause. IS it POSSIBLE
One way to do this is :
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
As you see there I have created on derived table t for your query, now result of your query is treated as Table(temp table) and having columns as col1,col2 and col3, Using this result set we can able to access col3 in where clause .
Note - assuming that select col3 from table2 returns single value as per OP's comments
Use cross join:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;

How can I compare if 2 tables have the same data?

If I have 2 tables and want to find if they have the same data, what is the most straightforward way to do it in MySQL?
I have read about doing a correlated subquery and UNION ALL but this query is about 2 pages (!) and can not really follow what it is doing. There must be an easier way.
Even if it is e.g. make MySQL copy the table data to files and do a vimdiff (I am not sure that this is even possible -is it?- just thinking out loud).
UPDATE
I am interested only in the table data and not structure. This is to clarify due to an ambiguous comment I made
If you just want to tell whether the tables are identical or not as efficiently as possible, use this query:
SELECT 1 FROM (
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
) t
GROUP BY col1, col2, col3
HAVING count(*) = 1
LIMIT 1
List all the columns in GROUP BY to compare the entire table.
If the result is an empty set, the two tables are identical.
If you want to see the differences, use this query:
SELECT * FROM (
SELECT 'table1' tname, col1, col2, col3 FROM table1
UNION ALL
SELECT 'table2' tname, col1, col2, col3 FROM table2
) t
GROUP BY col1, col2, col3
HAVING count(*) = 1
List the same columns in the inner SELECT as in the GROUP BY, plus a column to distinguish the two tables.
Just throwing this out there, you could emulate a full outer join and then return the rows where just the right or the left side is null.
select t1.*
from table1 t1
LEFT OUTER JOIN table2 t2
ON t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND ...
WHERE t2.id is null
UNION
select t2.*
from table2 t2
LEFT OUTER JOIN table1 t1
ON t2.col1 = t1.col1
AND t2.col2 = t1.col2
AND ...
WHERE t1.id is null
With the FULL OUTER JOIN you can show all rows where the other row is not available in the other table.
Use the following query:
SELECT c1 = cjoin AND c2 = cjoin equiv
FROM (SELECT COUNT(*) c1 FROM Table1) t1,
(SELECT COUNT(*) c2 FROM Table2) t2,
(SELECT COUNT(*) cjoin
FROM Table1 t1
JOIN Table2 t2
ON t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.col3 = t2.col3 ...) tjoin
Assuming the tables have a unique key, this will return equiv = 1 if the tables are equal. It doesn't show the differences, it's just a binary test.
I was reading SQL Cookbook from A.Molinaro, when I came across a solution.
It is based on to tables
emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
and a view
V
which has the same columns but different rows. The columns mgr and comm might be NULL, other columns not.
The solution in the book is very long and it does not show all differences, although this was the stated problem in 3.7.
I made up my solution which is shorter and shows all differences (means all rows which have different counts in the two tables).
select * from
# those which are contained in the (distinct) union of (col1,col2,...,coln, count) of both tables:
( select empno,ename,job,mgr,hiredate,comm,deptno, count(*) cnt from emp group by empno,ename,job,mgr,hiredate,comm,deptno
union
select empno,ename,job,mgr,hiredate,comm,deptno, count(*) cnt from V group by empno,ename,job,mgr,hiredate,comm,deptno
) as unionOfBoth
where (empno,ename,job,mgr,hiredate,comm,deptno,cnt)
not in
# those which are contained in the intersection of both tables with the equal number of counts:
( select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.comm,e.deptno,e.cnt
from
(select empno, ename,job,mgr,hiredate,comm,deptno, count(*) cnt from emp group by empno,ename,job,mgr,hiredate,comm,deptno) e,
(select empno, ename,job,mgr,hiredate,comm,deptno, count(*) cnt from V group by empno,ename,job,mgr,hiredate,comm,deptno) v
where
e.empno = v.empno
and e.ename = v.ename
and e.job = v.job
and ifnull(e.mgr,0) = ifnull(v.mgr,0)
and e.hiredate = v.mgr
and e.deptno = v.deptno
and ifnull(e.comm,0) = ifnull(v.comm,0)
and e.cnt = v.cnt
);
Basically you count the distinct rows in both tables and do a union (not union all) to get the tmp.table unionBoth. Then you remove those rows, which both tables have in common.
Here two rows r1 from table t1 and r2 from table t2 are considered the same, if
(r1,count of r1 in t1) = (r2, count of r2 in t2), which is equivalent to r1=r2 (on all columns) and (count of r1 in t1) = (count of r2 in t2).
If the tables are small enough, you can export both tables as csv files and then copy one of the tables and paste them side-by-side with the other table. You can just go row by row and see if the outputs are the same that way.

MySQL Query - not interested in using inner query

I have a query say,
select col1,col2 from table1;
which return 2 columns of multiple rows. I want to use these two values in where condition of another query. Something like
select col3,col4 from table2 where col5=col1 and col6=col2;
where col1 and col2 are the resultant values of the first query.
Currently I have used inner query something like
select col3,col4 from table2
where col5 in (select col1 from table1)
and col6 in (select col2 from table1);
But I dont want to use inner query like the one shown above as it slows down bring results.
Please suggest.
JOIN them instead of using IN's like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN
(
SELECT col1, col2
FROM table1
) t1 ON t2.col5 = t1.col1 AND t2.col6 = t1.col2
Note that, you didn't need to select specific columns in the second table. You can JOIN the second table table1 directly like so:
SELECT t2.col3, t2.col4
FROM table2 t2
INNER JOIN table1 t1 ON t2.col5 = t1.col1
AND t2.col6 = t1.col2