MySQL Query needs to bring back rows, not empty results - mysql

OK, I've voted to delete my earlier question due to stupidity on my part...
I have the following code:
SELECT qnum, id, name, total_staff AS StaffCount, COUNT( q61g ) AS TotalResp,
(COUNT( q61g ) / total_staff * 100) AS Perc
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q61g
WHERE qnum = 'q61g' AND q60p = '1'
GROUP BY name
ORDER BY name
Now, the first part of this query brings back rows from the tdemog table, for example it will bring back 5 rows of data each row has an id from 1 to 5. What I need the query to do is then bring back data from the tresults table WHERE q60p = 1 for each of the 5 rows brought back in the first part - like a normal `LEFT JOIN'.
Make sense?
H.

Try moving part of your WHERE clause into your JOIN condition:
SELECT ...
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q61g AND q60p = '1'
WHERE qnum = 'q61g'
GROUP BY name
ORDER BY name
If you have a field from your second table in your WHERE clause, it will restrict the entire record... but if you put it into your JOIN condition, the record from the first table should still be returned even when the record in the second table doesn't meet the additional criteria...
I'm not sure which column belongs to which table... but move whatever columns are in your second table into your JOIN.

Related

MYSQL query to get project details and last MAX() action details from log

How can I write a MYSQL query to get project details and the entire last row of the activity log? I want a list of all the projects, with the data from each project's most recent row from the action log, all of it ordered by the most recent action log date DESC. Sorry, I know that this is a common query and the answer must be very easy. But I can't find the solution. I searched with every possible word combination. I found examples that need only one field such as MAX(id) from the joined table. I found solutions with COALESCE but can't seem to make them work. My problem is that I need many fields from the 'parent' table row PL_PROJECTS as well as many fields from the joined table PL_LOG row, not to mention people's names from the same table joined twice.
Everything I try either gives me all the rows of the PL_LOG, repeating rows from PL_PROJECTS. Or, I get just one row from PL_LOG for just one project if I put a LIMIT in the sub query. Here's my query that doesn't work:
SELECT
PJ.pj_id, PJ.pj_title, PJ.pj_location, PJ.pj_desc, PJ.pj_request, PJ.pj_date_start, PP1.pp_name AS supervisor_name, PP2.pp_name AS customer_name, ST.st_desc, logDate, logDesc
FROM PL_PROJECTS PJ
INNER JOIN PL_PEOPLE PP1 ON PJ.pj_spst_member = PP1.pp_id
INNER JOIN PL_PEOPLE PP2 ON PJ.pj_pp_id = PP2.pp_id
INNER JOIN PL_STATUS ST ON PJ.pj_status = ST.st_id
LEFT OUTER JOIN (
SELECT MAX(lg_pj_id) MaxLogID, lg_date AS logDate, lg_desc AS logDesc, lg_pj_id
FROM PL_LOG PL
ORDER BY lg_id DESC
)
LR ON LR.lg_pj_id = PJ.pj_id
GROUP BY PJ.pj_id
ORDER BY logDate DESC
LIMIT 9999999
I think you problem is, that your subselect only generates one row as you are using max() while you need one row per project (lg_pj_id i think).
You only need to rewrite the subselect to generate one row per project with the informations from the recent activity. Do you have an activity_ID in your action log? Because it looks like
lg_pj_id is the project_ID. The meaning of lg_desc is also unknown (or is that the action_log_id ?). Try to group by project_ID in you subselect and depending on your needs either select the max values from the corresponding rows or select the row with the maximum values per group (project_ID)
Thanks for the suggestion of GROUP BY to get one row per project. I tried changing the sub-query like so:
SELECT MAX(lg_id) AS MaxLogID, lg_desc, lg_pj_id
FROM PL_LOG PL
GROUP BY lg_pj_id
Now, I get one row from the log, but it gives me the max id, but not the lg_desc from the same row! If I try the sub-query by itself:
SELECT lg_id, lg_pj_id, lg_date, lg_desc
FROM `PL_LOG`
WHERE lg_pj_id = 33
ORDER BY lg_date DESC
I get these rows. You can see the max row, 68 has a description "30 minute skype call."
68,33,2018-06-10 00:00:00","30 minute skype call."
61,33,"2018-06-02 00:00:00","Sent email to try to elicit a response."
52,33,"2018-05-10 00:00:00","sent follow up email"
47,33,"2018-03-26 00:00:00","sent initial email"
46,33,"2018-03-26 00:00:00","sent initial email"
But when I try to get just that row, using GROUP BY, it gives me the max lg_id, but the first lg_desc. I need the data all from the max(lg_id) row:
SELECT MAX(lg_id) AS MaxLogID, lg_pj_id, lg_date, lg_desc
FROM PL_LOG
WHERE lg_pj_id = 33
GROUP BY lg_pj_id
ORDER BY MaxLogID DESC
Returns:
68, 33, "2018-03-26 00:00:00", "sent initial email"
Try this as mentioned in my comment:
SELECT
PJ.pj_id, PJ.pj_title, PJ.pj_location, PJ.pj_desc, PJ.pj_request,
PJ.pj_date_start, PP1.pp_name AS supervisor_name, PP2.pp_name AS
customer_name, ST.st_desc, logDate, logDesc
FROM PL_PROJECTS PJ
INNER JOIN PL_PEOPLE PP1 ON PJ.pj_spst_member = PP1.pp_id
INNER JOIN PL_PEOPLE PP2 ON PJ.pj_pp_id = PP2.pp_id
INNER JOIN PL_STATUS ST ON PJ.pj_status = ST.st_id
LEFT JOIN (SELECT lg_id, lg_date AS logDate, lg_desc AS logDesc, lg_pj_id
FROM PL_LOG AS PL
WHERE PL.lg_id=(SELECT MAX(lg_id) FROM PL_LOG AS PL_2
WHERE PL_LOG.lg_pj_id = PL_2.lg_pj_id )
LR ON LR.lg_pj_id = PJ.pj_id
GROUP BY PJ.pj_id
ORDER BY logDate DESC
LIMIT 9999999

INNER JOIN selects only one row from second table

I would like to get last value of the second table using mysql inner join.
This is my first table name 'tb_reg'
Second table 'tb_stud_qulification'
I want to get the last date of the 'candidate_no' where first table 'id' equqal to the second table 'candidate_no'.
I wrote inner join code like this but i'm getting error
SELECT reg.*, quli.course, quli.total_per
FROM tb_reg AS reg
INNER JOIN tb_stud_qulification AS quli ON reg.stage = '2' AND reg.id = quli.candidate_no AND
ORDER BY quli.id
LIMIT 1
I would like to get the result like this
you can show:
`http://stackoverflow.com/questions/8821920/sql-sqlite-select-with-inner-join`
example:
SELECT doctors.doctor_id,doctors.doctor_name,visits.patient_name
FROM doctors
INNER JOIN visits
ON doctors.doctor_id=visits.doctor_id
WHERE doctors.degree='MD';
I would write it like this (watch the different ON and WHERE clauses)
SELECT reg.*, quli.course, quli.total_per
FROM tb_reg AS reg
INNER JOIN tb_stud_qulification AS quli ON reg.id = quli.candidate_no
WHERE reg.stage = '2'
ORDER BY quli.id
LIMIT 1
but apart from that, I don't see a stage field in your tb_reg table...

order, group and search query. too many selects

i am running this query, witch consists of 3 recursive selects.
select idigorUserFields
from ( select *
from ( select *
from igorUserFields f
where f.idigorUsers = 1
order by f.idigorUserFields desc) tbl
group by tbl.idigorUserFieldTemplates ) tbl2
where value="qf" and idigorUserFields = 28
what I am trying to do is simple:
get all fields, order by insert date ( i am using primary key for that )
get the last inserted value for a field (idigorFieldTemplates)
compare the last inserted field with the one i am about to insert, to save some space on the database
some relevant info:
idigorUserFieldTemplates is the primary key for a "html user fields table"
the last inserted value is the one displayed on the program
also, I have a sqlfiddle! with some data to test.
my question is: can I make this query better? and what do I need to use to do that.
Alright so it looks like you want to get the last inserted value for a given user and value and then compare that with the data you are about to insert. Here's how I would tackle that
SELECT a.idigorUserFieldTemplates
FROM igorUserFields AS a
INNER JOIN (SELECT MAX(idigorUserFields) as max_id FROM igorUserFields WHERE idigorUsers = 1 AND value="qf") AS b
ON a.idigorUserFields= b.max_id
The subquery is giving me the largest idigorUserFields for which idigorUsers = 1 AND value="qf." For this to work, you have to assume that that the primary key (idigorUserFields) is incrementing with date. You indicated in your post that it does, so hopefully this assumption is okay.
Once we've got that last updated record, we then join back with igorUserFields to get the corresponding value of idigorUserFieldTemplates
edit:
I want to get the last inserted for a given user and field. then compare it with what i am about to insert.
I am sorry if I was not clear enough about what I wanted.
I modified the above sql and i got what i wanted:
SELECT a.* FROM igorUserFields
AS a INNER JOIN
(SELECT MAX(idigorUserFields) as max_id
FROM igorUserFields WHERE idigorUsers = 1 and idigorUserFieldTemplates =6) AS b
ON a.idigorUserFields= b.max_id
AND value="qf"
thanks! now I got a more efficient solution :)
if all you want is the largest id by specific data just specify that order it and limit it like so
SELECT idigorUserFields
FROM igorUserFields
WHERE idigorUsers = 1 AND value="qf"
ORDER BY idigorUserFields DESC
LIMIT 1
DEMO
if you are trying to get the last inserted value then you can get the largest idigorUserFields assuming its auto incremented.
SELECT MAX(idigorUserFields) FROM idigorUserFields
and then if you want a specific column from that field you could use it as a subquery
SELECT f.idigorUserFieldTemplates
FROM idigorUserFields f
WHERE f.idigorUserFields =
( SELECT MAX(idigorUserFields)
FROM idigorUserFields
WHERE idigorUsers = 1 AND value="qf"
)

SubQuery Join Failed

I am trying to find out the missing record in the target. I need the employee whose record are missing.
Suppose I have input source as
1,Jack,type1,add1,reg3,..,..,..,
2,Jack,type2,add1,reg3,..,,.,..,
3,Jack,type3,add2,reg4,..,.,..,.,
4,Rock,,,,,,,,
and I have output as
1,Jack,type1,add1,reg3,..,..,..,
4,Rock,,,,,,,,
I have 1000 numbers of rows for other employees and in target i don't have any duplicate records.
I need the employee who are present in source and target having different occurance
means for e.g in above sample data I have 3 entries of jack and 1 entry of Rock in source
and in target I have only on entry of Jack and one for Rock
I am running below query and required output is Jack,3
How can I get it. I am getting error in below query
select A.EMP_NUMBER,A.CNT1
from
(select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER ) as A
INNER JOIN
(select B.EMP_NUMBER,B.CNT2
from (select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER )as B )
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 != B.CNT2
Please help.
Why don't get the employee that have different number of rows in the two table when grouped by their name (I suppose Emp_Number is the field that contain the name if that what the query in the question return)
SELECT s.Emp_Number, Count(s.Emp_Number)
FROM EMPLOYEE_SOURCE s
LEFT JOIN EMPLOYEE_TARGET t ON s.Emp_Number = t.Emp_Number
GROUP BY s.Emp_Number
HAVING Count(s.Emp_Number) != Count(t.Emp_Number)
It would be really helpful if you specified the exact error you get.
If this is you actual query there are two things: There's no alias name for the 2nd Derived Table (btw, you don't need it at all) and at least in Teradata !=is not valid, this is SQL and not C.
select A.EMP_NUMBER,A.CNT1
from
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER
) as A
INNER JOIN
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER
) as B
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 <> B.CNT2
If an employee is missing in the 2nd table you might have to use an Outer Join as Serpiton suggested and add an additional WHERE-condition:
where A.CNT1 <> B.CNT2
or b.CNT2 IS NULL

COUNT evaluate to zero if no matching records

Take the following:
SELECT
Count(a.record_id) AS newrecruits
,a.studyrecord_id
FROM
visits AS a
INNER JOIN
(
SELECT
record_id
, MAX(modtime) AS latest
FROM
visits
GROUP BY
record_id
) AS b
ON (a.record_id = b.record_id) AND (a.modtime = b.latest)
WHERE (((a.visit_type_id)=1))
GROUP BY a.studyrecord_id;
I want to amend the COUNT part to display a zero if there are no records since I assume COUNT will evaluate to Null.
I have tried the following but still get no results:
IIF(ISNULL(COUNT(a.record_id)),0,COUNT(a.record_id)) AS newrecruits
Is this an issue because the join is on record_id? I tried changing the INNER to LEFT but also received no results.
Q
How do I get the above to evaluate to zero if there are no records matching the criteria?
Edit:
To give a little detail to the reasoning.
The studies table contains a field called 'original_recruits' based on activity before use of the database.
The visits tables tracks new_recruits (Count of records for each study).
I combine these in another query (original_recruits + new_recruits)- If there have been no new recruits I still need to display the original_recruits so if there are no records I need it to evalulate to zero instead of null so the final sum still works.
It seems like you want to count records by StudyRecords.
If you need a count of zero when you have no records, you need to join to a table named StudyRecords.
Did you have one? Else this is a nonsense to ask for rows when you don't have rows!
Let's suppose the StudyRecords exists, then the query should look like something like this :
SELECT
Count(a.record_id) AS newrecruits -- a.record_id will be null if there is zero count for a studyrecord, else will contain the id
sr.Id
FROM
visits AS a
INNER JOIN
(
SELECT
record_id
, MAX(modtime) AS latest
FROM
visits
GROUP BY
record_id
) AS b
ON (a.record_id = b.record_id) AND (a.modtime = b.latest)
LEFT OUTER JOIN studyrecord sr
ON sr.Id = a.studyrecord_id
WHERE a.visit_type_id = 1
GROUP BY sr.Id
I solved the problem by amending the final query where I display the result of combining the original and new recruits to include the IIF there.
SELECT
a.*
, IIF(IsNull([totalrecruits]),consents,totalrecruits)/a.target AS prog
, IIf(IsNull([totalrecruits]),consents,totalrecruits) AS trecruits
FROM
q_latest_studies AS a
LEFT JOIN q_totalrecruitment AS b
ON a.studyrecord_id=b.studyrecord_id
;