How to do two different count in one sql - mysql

How can do two different count in one table?
The two different count functions count different columns.
Simplified Table:
id,creatorId,resolverId
'1','1','2'
'2','1','2'
'3','2','2'
'4','2','1'
What I want to do is putting the creatorId,COUNT(creatorId),resolverId,COUNT(resolverId) into one table. Like:
creatorId,COUNT(creatorId),resolverId,COUNT(resolverId)
'1','2','1','1'
'2','2','2','3'
I only passed the test of putting them in 2 columns by using UNION, and I tried JOIN but it is illegal to MySQL.
SELECT creatorId, COUNT(creatorId)
FROM issue AS a
GROUP BY creatorId
join(
SELECT resolverId, COUNT(resolverId)
FROM issue AS b
GROUP BY resolverId)
WHERE a.creatorId = b.resolverId;
The error info is:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'join( SELECT resolverId, COUNT(resolverId) FROM issue AS b GROUP BY resolverId)' at line 4 0.00034 sec
Can anyone tell me how to deal with it? or give me a example?
Thank you!

select a.creatorId,COUNT(a.creatorId), t.resolverId, count_resolved_id
from issue a
inner join (
SELECT b.resolverId, COUNT(b.resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId;

you could jon this way
select a.creatorId,COUNT(acreatorId), resolverId, count_resolved_id
from issue a
inner join (
SELECT resolverId, COUNT(resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId,COUNT(acreatorId)

If I understand correctly, then one way of doing this is an aggregation after a union all:
select id, sum(creator) as creator_cnt, sum(resolver) as resolver_cnt
from ((select creator_id as id, 1 as creator, 0 as resolver
from issue
) union all
(select resolver_id as id, 0 as creator, 1 as resolver
from issue
)
) cr
group by id;

Related

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

SQL query to retrieve data from 3 tables

I have branch table , inventory table and item table. I want to retrieve item id, item name , qty (quantity) and branch name (branch_add).
SELECT tbl_item.item_name ,
tbl_inventory. tbl_item_item_ID , tbl_inventory.qty , tbl_branch.branch_add
FROM tbl_item,tbl_inventory ,tbl_branch
WHERE (tbl_inventory.tbl_item_item_ID = tbl_item.item_ID) JOIN (tbl_branch.branch_ID=tbl_inventory`.tbl_branch_branch_ID)
That is the code which I have wrote but it gives bellow error.
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'join item_ID.tbl_item =
tbl_item_item_ID.tbl_inventory' at line 3
Could anyone please help me to solve this. And table will displayed on attached png.
You have a missing AND between your WHERE conditions:
select
tbl_branch_branch_ID.tbl_inventory
, item_name
from tbl_inventory
, tbl_item
, tbl_branch
where
branch_ID.tbl_branch=tbl_branch_branch_ID.tbl_i
and item_ID.tbl_item = tbl_item_item_ID.tbl_inventory;
Also, your JOIN conditions are a little bit off.
I think this is the query you're actually looking for:
select
t_inv.*
, t_itm.item_name
from tbl_inventory t_inv
inner join tbl_item t_itm on t_inv.t_item_item_id = t_itm.item_id
inner join tbl_branch t_br on t_br.branch_id = t_inv.tbl_branch_branch_id
Using explicit JOINs is much better and clearer for reading the code and also can result in improved code performance.
SELECT
tbl_item.item_name,
tbl_inventory.tbl_item_item_ID,
tbl_inventory.qty,
tbl_branch.branch_add
FROM
tbl_item,
tbl_inventory,
tbl_branch
WHERE
(
inner join tbl_item ON tbl_inventory.tbl_item_item_ID = tbl_item.item_ID
inner join branch_ID ON tbl_branch.branch_ID = tbl_inventory.tbl_branch_branch_ID
);
The above query gives syntax error.

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

Joining derived tables

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;

Can I count number of rows in joined table?

Like this:
SELECT s.*, count( logs.* ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip
But I get an error with that query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) as ssh_count FROM servers s LEFT JOIN logs ON s.ip_address = logs.server_ip LIMIT' at line 1
I think that's because you can't address a table in the count function.
I can do this using a subquery, but that will likely slowly down the query.
What is a better way of doing this?
You can adress a table column, but you can't address table.*, you can do this for example:
SELECT s.*, count( logs.server_ip ) as ssh_count
FROM servers s
LEFT JOIN logs ON s.ip_address = logs.server_ip