Get count of respective id in single query Mysql - 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;

Related

Using "or" ,"and", "like" several times in same column

If I have a table like this:
id car
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 F
3 A
3 C
3 E
3 F
3 G
what I want is different "id" which have ("A" or "C") and "B" in car. For example:
id car
1 A
1 B
1 C
2 A
2 B
2 C
what I did was
select * from table where (car like "A" or car like"C") and (car like "B")
but it gives me an empty row.
Any clue?
You can use a self-join
SELECT t1.id
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id
WHERE t1.car IN ('A', 'C')
AND t2.car = 'B'
BTW, you should generally only use LIKE when you're doing a pattern match. For exact matches use =, or IN for matching any of multiple items.
Untested, but something like this should work: get all rows that have the ID such that it is found on the list of all IDs that have an A or a C, and also on the list of all IDs that have a B.
SELECT t.*
FROM mytable t
WHERE t.id IN (
SELECT DISTINCT id
FROM mytable t2
WHERE t2.car='A' OR t2.car='C'
)
AND t.id IN (
SELECT DISTINCT id
FROM mytable t3
WHERE t3.car='B'
)

Add a column in SQL query that indicates the number of times that the number is shown in other tables

I need to make a query that selects everything from a table A, and in addition to have a column that indicates the number of times that the value of A.col1 is in B.col2.
Example:
Table A:
id name
1 "y"
2 "z"
3 "w"
Table B:
id name
15 "y"
23 "w"
14 "y"
I want a query that will give me the following:
id name numOfTimes
1 "y" 2 // y is shown twice in table B
2 "z" 0 // z isn't shown in table B
3 "w" 1 // w is shown once in table B
try this:
Select a.id, a.name, count(b.id) as numoftimes from a
left outer join b on a.name = b.name
group by a.id, a.name;
It can be done in multiple ways
select a.id as Id
,a.name as Name
,(select count(1) from b where a.id=b.id) as NumberOfTimes
from a;
or
select a.id as Id
,a.name as Name
,count(b.id) as NumberOfTimes
from a
left join b on a.id=b.id;
Try with this following you will get your expected result.
select a.id,a.col1,count(b.col1) as numOfTimes
from TableA a left join TableB b
on a.col1 = b.col1
group by a.id,a.col1;
Thanks.

get other fields when retrieving max value from a line in mysql

I have this table:
A,B,C
2,5,6
2,8,5
3,4,7
3,4,8
I want to group the results by A, and select the max C value. The query for it is something like
SELECT A, MAX(C) FROM table GROUP BY A
My Question is, how can I retrieve the B value that is next to the max(c) value? Can this be done in the same quesry, or do I need a join query and look for the B field after running the first query?
Thanks!
try this:
select A,(select B from Table1 t1 where t1.A=t2.A and T1.C=T2.C) B ,MAX(c) C
from Table1 T2 group by A;
FIDDLE DEMO
This is known as the groupwise maximum. To obtain it, you must join the result to your table again:
SELECT * FROM table NATURAL JOIN (
SELECT A, MAX(C) AS C FROM table GROUP BY A
) t
You can add it to your selected columns, but must group by it as well. Otherwise the result is indeterminate.
SELECT A, B, MAX(C)
FROM table
GROUP BY A, B
select *
from table1 t
join
(SELECT A, MAX(C) as C
FROM table1 GROUP BY A )a
on t.A=a.A
and t.C=a.C

Selecting two rows in a table which have the same data for a particular column

There is a column in a table(contracts) called service location. I have to show all the rows where the service locations matches any other row in the table.
Table Example
A B C
1 2 3
3 2 1
2 5 3
I require a query where the first and second rows will be returned based on a comparison on the second column. I am assuming I will need to use a HAVING COUNT(B) > 1
I came up with this
SELECT `contract_number`
FROM `contracts`
WHERE `import_id` = 'fe508764-54a9-41f7-b36e-50ebfd95971b'
GROUP BY `service_location_id`
HAVING COUNT(`service_location_id` ) >1
But it doesn't generate what I exactly need.
Having would do it, but you would need to use it like this
SELECT *
FROM Contracts
INNER JOIN
( SELECT B
FROM Contracts
GROUP BY B
HAVING COUNT(*) > 1 -- MORE THAN ONE ROW WITH THE SAME VALUE
) dupe
ON dupe.B = Contracts.B
Depending in your indexing you may find a self join performs better though:
SELECT DISTINCT t1.*
FROM contracts t1
INNER JOIN contract` t2
ON t1.B = t2.B
AND t1.A <> t2.A
SELECT *
FROM sheet1
WHERE C
IN (
SELECT C
FROM sheet1
GROUP BY C
HAVING COUNT( C ) >1
)
ORDER BY C
LIMIT 0 , 5000

How do you COUNT outside of the WHERE statement in 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}