Mysql - Display ID which occurs more than once - mysql

I have a column called "job_processing_id", "Description", "Who_marked_it"
My task is to find out any job_processing_id occuring more than once and display it.
I have written the below query :
Select cdh.job_processing_id, cdh.test_counter, tt.test_type, cd.description, u.name, cdh.added_at from call_description_history as cdh left join test_type as tt on tt.id = cdh.test_type_id left join call_description as cd on cd.id = cdh.call_description_id left join user as u on u.id = cdh.added_by where job_processing_id in (SELECT job_processing_id FROM call_description_history group by job_processing_id having count(job_processing_id)>1) and u.id NOT IN (7652, 5225) and added_a
But it also displays ID's that occure only once , which i don't need it.
Can someone help me how to get rid of it please?

Try this
SELECT job_processing_id, count(job_processing_id) FROM call_description_history group by job_processing_id having count(job_processing_id)>1;

Related

Display a record although the condition return zero record

I have MySql query like this:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id
WHERE u.role = 'student' AND hr.homework_id = 8
This query work just fine, but not like what I expected. I want to display all the student (users) record is displayed although they don't have a record on homework_replies that match the homework_targets is it possible? if it possible how can I accomplish it? thanks.
Move the hr.homework_id condition from WHERE to ON to get true LEFT JOIN result:
SELECT name, id_number, hr.id, file, created_at, updated_at
From users u
LEFT JOIN homework_targets ht on u.class_id = ht.class_id
LEFT JOIN homework_replies hr on u.id = hr.user_id AND hr.homework_id = 8
WHERE u.role = 'student'
(If you have it in the WHERE clause, you'll get regular INNER JOIN result.)

Query inside NOT IN statement Doctrine

I found this sentence in the code:
$dql
= <<<DQL
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
AND
r.profile = :sales_profile
AND
r.company = :company
AND
u.onlyStatus NOT IN (:status)
DQL;
I don't know how to works that query inside NOT IN sentence, help me please.
I need to know:
What return the query inside of NOT IN, (data types, so on...)
How to works a query inside of NOT IN (is posible ?)
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
this means take all the users that don't currently have 'objective' in the 'Goal' table.
Is this what you needed ?

Issue understanding the Inner Join Or Left join

I am having an issue understanding the inner and left join
I am having the below query in the outsystems
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
The issue is the Select is pulling all the CLD elements without respect to the Project, I am trying to select CLD's whose Project id = Project.Id. I tried both the joins but it keep pulling all the values
Below how the structure looks like
Please try the following: First get the CLDProjects matching with Project then get the CLD from matched records.
FROM {CLD} INNER JOIN (
{CLDProjects} INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
) ON {CLD}.[Id] = {CLDProjects}.[CLDId]
You're probably missing the inner join to CLDProjects on the ProjectParticipant subquery. Add
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId])
on the second EXISTS join conditions, otherwise it will the second results will match the exists for every project the user is in, ignoring the other conditions over CLDProjects. Try the following:
SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
, {CLD}.[LastUpdateOn],min({Project}.[Number])
,count({Project}.[Number])
FROM {CLD}
INNER JOIN {Project} ON {Project}.[Id] = {CLDProjects}.[ProjectId]
INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
WHERE
(
#IsJAXPM =1
or EXISTS (SELECT 1
FROM {CLDParticipant}
WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id]
AND {CLDParticipant}.[UserId] = #UserId)
or EXISTS (SELECT 1
FROM {ProjectParticipantWidget}
INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] = {ProjectParticipant}.[Id]
INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] = {CLDProjects}.[ProjectId]
WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id]
AND {ProjectParticipant}.[UserId] = #UserId)
)
GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]
Also.. make sure you're not passing #IsJAXPM as 1... otherwise it will definitely return all records.
Let us know if that works. Otherwise, please extend the diagram to show the ProjectParticipant and ProjectParticipantWidget tables as well.
I think you just need to reorder your joins, I tried to reproduce your case (with incorrect joins order), but it gives me error when test the query,
but if you reorder the the joins, it works. note that I am using OutSystems 10

Why does a LEFT JOIN break my MySQL query?

SELECT value AS $point_fld, COUNT(value) as cnt
FROM ?_data d LEFT JOIN
?_user u
ON d.`table` = 'user' AND d.`id` = u.user_id
WHERE `key` = ? AND `value`<>'' AND `value`<>'Blank' AND
u.added BETWEEN ? AND ?
/* ----- I added this bit */
LEFT JOIN ?_invoice_payment p
ON d.'id' = p.user_id
SUM(p.amount) as moneysum
/* End ---- */
GROUP BY $point_fld
I am trying to edit this query to add in a sum value for payments based on which users are returned in the first part. The commented text is what I added. It is working just as intended but when I add in my part to get the values it breaks it.
Any suggestions would be amazing thanks. I'm pretty new to joins and stuff in SQL.
SELECT value AS $point_fld,
COUNT(value) as cnt,
SUM(p.amount) as moneysum
FROM ?_data d
INNER JOIN ?_user u ON d.`table` = 'user' AND d.`id` = u.user_id
LEFT JOIN ?_invoice_payment p ON d.'id' = p.user_id
WHERE `key` = ? AND `value`<>'' AND `value`<>'Blank' AND
u.added BETWEEN ? AND ?
GROUP BY value
OK this is what finally worked. Thanks for the help!
SELECT value AS $point_fld,
COUNT(DISTINCT u.user_id) as cnt,
SUM(p.amount) as moneysum
FROM ?_data d
LEFT JOIN ?_user u ON d.table = 'user' AND d.id = u.user_id
LEFT JOIN ?_invoice_payment p ON d.id = p.user_id
WHERE key = ? AND value<>'' AND value<>'Blank' AND
u.added BETWEEN ? AND ?
GROUP BY value

Updating users table from complex SQL query, users.id not recognised?

So the other day, I asked this question about how to combine three complex queries and found a way to do it. Now I'm trying to use those queries to update a field in the users table and can't find a way to make it work. Here's the query:
update users set field_sum =(
select sum(field_sum) from (
select sum(field_one) as field_sum
from t_a join t_b on (t_a.bid = t_b.id) where t_b.user_id=users.id
union all
select sum(field_two) as field_sum
from t_c join t_d on (t_c.did = t_d.id) where t_d.user_id=users.id
union all
select sum(field_three) as field_sum
from t_e where t_e.user_id=users.id
) as field_sumT
)
When I try to run it, I get the following error: ERROR 1054 (42S22): Unknown column 'users.id' in 'where clause'. When I try removing the .user_id=users.id bit from each where clause, it will run but ends up with the total sum of field_sum, not just the field_sum for that user. Is there any way to accomplish this?
Use:
UPDATE USERS u
LEFT JOIN (SELECT t_b.user_id,
SUM(field_one) as field_sum
FROM t_a
JOIN t_b on t_a.bid = t_b.id
GROUP BY t_b.user_id) a ON a.user_id = u.id
LEFT JOIN (SELECT t_d.user_id,
SUM(field_two) as field_sum
FROM t_c
JOIN t_d on t_c.did = t_d.id
GROUP BY t_d.user_id) b ON b.user_id = u.id
LEFT JOIN (SELECT t_e.user_id,
SUM(field_three) as field_sum
from t_e
GROUP BY t_e.user_id) c ON c.user_id = u.id
SET field_num = COALESCE(a.field_sum, 0) + COALESCE(b.field_sum, 0) + COALESCE(c.field_sum, 0)
Caveat
This will set any users with no records in the supporting rows to have a field_sum value of zero. Do you only want to update those with a record in at least one of those tables?