How do you COUNT outside of the WHERE statement in MYSQL - mysql

I have a table - something like:
A|B
1|1
1|2
1|3
2|1
2|3
2|4
2|5
3|5
My query returns the distinct values in A if they coincide with a value in B of 1 or 2 - so
A
1
2
I am trying to also return the original count of the 1's and 2's in column A - to get something like
A|Count
1|3
2|4
Is there a simple way to get this count please?
However, the COUNT (A) returns the number of A's coinciding with the initial WHERE statement:
A|Count
1|2
2|1
Thanks!

My SQL might be a bit rusty, but I think you can do:
SELECT A, count(*) AS Count FROM MyTable WHERE B IN (1, 2) GROUP BY A;

Another way:
SELECT a, (SELECT count(*) FROM t t2 WHERE t2.a = t.a) a_count
FROM t
WHERE b IN (1,2)
GROUP BY a

SELECT t1.A, COUNT(DISTINCT t1.B)
FROM MyTable t1 JOIN MyTable t2 ON (t1.A = t2.A)
WHERE t2.A = t2.B
GROUP BY t1.A;

Similar to Bill Karwin's answer:
SELECT
A,
Counted.CountOfB
FROM
MyTable
INNER JOIN (
SELECT A, COUNT(B) AS CountOfB
FROM MyTable
GROUP BY A
) Counted ON Counted.A = MyTable.A
WHERE
{your filter for MyTable}

Related

Get count of respective id in single query Mysql

I am using a simple query:
select A,B from Table1 where id in ('');
which gives me output like:
A B
1 X
2 V
3 R
Now i want to know count of value B in whole database:
i.e
A B CountB
1 X 3
2 V 1
3 R 2
This ought to to it:
SELECT *
FROM (SELECT A,
B
FROM Table1
WHERE id in ('')) t1 LEFT JOIN
(SELECT B,
COUNT(B)
FROM Table1
GROUP BY B) t2 ON t1.B = t2.B
May I didn't understand the question but it seems the following query should do:
SELECT A,
B,
COUNT(*)
FROM Table1
WHERE .....
GROUP BY A,
B
ORDER BY A,
B;
You need to make use of group_by if you want certain column, and then add a count the id of it to get count. So I'd do
SELECT A,B,COUNT(B.ID) as CountB FROM Table1 WHERE id IN ('') GROUP BY B;

MySQL IN clause EITHER value

I have this query:
SELECT 1 IN (1,2)
Which returns 1 since 1 is inside (1,2).
What I want to do is check if either one of two values exists in the array. In an imaginary world:
SELECT (3,1) EITHER IN (1,2)
Something like this should return 1 since at least one value was found in the second array. Of course this query is incorrect. Is there a way to do this and avoid this:
SELECT (
3 IN (1,2)
OR
1 IN (1,2)
)
you can use an inner join for that
select a, b form T1
inner join T2 on (T1.a = T2.c or T1.b = T2.c)

SQL Table Counting and Joining

I have Table A, Column 1.
This table has values such as:
1
2
3
3
4
4
4
5
I have Table B, Column 2.
Which lists certain values, like:
1
3
4
I need a query to Count each unique value in Table A, but ONLY if that value is present in Table B.
So with the above, the end result would be:
1 has a quantity of 1,
3 has a quantity of 2,
and 4 has a quantity of 3.
My only problem is that I do not have this ability. Any help out there?
Based on your question, something like the following should solve your problem.
select b.column1,
count(a.column2)
from tableb as b
inner join tablea as a on b.column1 = a.column2
group by b.column1
Since you wanted only records which are in both tables, I am using an inner join. Then I am just grouping by the ID found in tableb, and getting the count of rows in tablea.
Let me know if you have any problems.
For more information regarding inner join, see : http://www.w3schools.com/sql/sql_join_inner.asp, and for group by, see : http://www.w3schools.com/sql/sql_groupby.asp
I would use an INNER JOIN query with GROUP BY aggregate function
SELECT a.column1,
count(a.column1) as total
FROM tablea a
INNER JOIN tableb b
ON a.column1 = b.column2
GROUP BY a.column1
SELECT column1,COUNT(column1)
FROM table1
WHERE column1 IN
(SELECT DISTINCT column2 FROM table2)
GROUP BY column1
Try this
MsSql
Select Distinct Column1,Count(Column1) Over (Partition by Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
Fiddle Demo
MySQl
Select Column1,Count(Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
group by column1
Fiddle Demo

Selecting maximum column and row id

Is there a way to tell MySQL that while making something like this
SELECT id, MAX(seq) FROM t1 GROUP BY ident;
I can also get the id value? I know I shouldn't be using id if it's not in a group by but I feel like its strange to make a multi pass to get the row ids with the maximum seq field when it already passed it. So what is the most effective way to do this? id is the primary key
SELECT a.*
FROM tableName
INNER JOIN
(
SELECT ident, MAX(seq) seq
FROM tableName
GROUP BY ident
) b ON a.ident = b.ident AND
a.seq = b.seq
Mabye:
SELECT MAX(a.seq), (SELECT id FROM t1 as b where b.ident=a.ident AND MAX(a.seq) = b.seq LIMIT 1) as id FROM t1 AS a GROUP BY a.ident;
Fiddle
Try using self-join:
SELECT t1.* FROM MyTable t1
JOIN
(SELECT ident, MAX(seq) AS MAX_Seq
FROM MyTable
GROUP BY ident
) t2
ON t1.seq = t2.MAX_Seq
AND t1.ident = t2.ident
See this sample SQLFiddle
What is seq exactly ?
I guess you can also order your results ?
SELECT id FROM t1 GROUP BY ident ORDER BY seq DESC
Regarding to the others answer, seq is in another table ?

How to fetch two different column values from two different tables inturn one value table is dependent on other table value

The following query is the one I was using previously... but I want to combine these two queries in order to improve the performance
select a, b, c
from table1
where d LIKE 'xxx'
and f like 'yyyy'
order by b desc;
I'm executing the above query and reading values.
For every value of b from above query again executing the below query in a loop.
select count(*)
from table2 where b=? AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)
How can I combine both queries and get count and values at a time?
select a,b,c,
(select count(*)
from table2 where b=a.b AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)) as cnt
from table1 a
Try this:
SELECT t1.a, t1.b, t1.c, COUNT(t2.*)
FROM table1 t1
INNER JOIN table2 t2 ON t1.b = t2.b
INNER JOIN js_initialsignup j ON t2.js_email_id = j.js_email_id
WHERE t1.d LIKE 'xxx'
AND t1.f like 'yyyy'
AND UCase(j.jsaccountstatus) LIKE UCase('live')
AND UCase(j.js_status) LIKE UCase('accepted'))"
GROUP BY t1.a, t1.b, t1.c
ORDER BY by t1.b DESC;