I don't know if this is possible but I'm trying to make a select queries with a couple of JOINS with a WHERE statement. What I need is a field based on a particular WHERE and another field with different WHERE.
Current SQL query:
SELECT SUM(a.totaltime), b.user_name, MONTHNAME(a.date)
FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '1' OR g.cf_990 = '1')
group by user_name,MONTHNAME(a.date);
This works fine but I need to include another field this time WHERE would be:
WHERE c.deleted != "1" AND YEAR(DATE(NOW())) AND ( f.cf_991 = '0' OR g.cf_990 = '0')
Any help is greatly appreciated thanks
EDIT:
CURRENT OUTPUT IS:
total_time username MONTHNAME(a.date)
22.5 admin April
21 admin June
15 max April
So the above is based on f.cf_991 = '1' OR g.cf_990 = '1'
What I need is this:
total_time username MONTHNAME(a.date) total_time_2
22.5 admin April 5
21 admin June 9
15 max April 13
total_time_2 is based on f.cf_991 = '0' OR g.cf_990 = '0'.
SELECT SUM(CASE WHEN f.cf_991 = '1' OR g.cf_990 = '1' THEN a.totaltime END) as total_time
, b.user_name, MONTHNAME(a.date)
, SUM(CASE WHEN f.cf_991 = '0' OR g.cf_990 = '0' THEN a.totaltime END) as total_time_2
FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1"
AND YEAR(CURDATE()) = a_column_containing_a_date
AND (
( f.cf_991 = '1' OR g.cf_990 = '1')
OR
( f.cf_991 = '0' OR g.cf_990 = '0')
)
group by user_name,MONTHNAME(a.date);
Just try this
SELECT
SUM(if(f.cf_991 = '1' || g.cf_990 = '1',a.totaltime,0)),
SUM(if(f.cf_991 = '0' || g.cf_990 = '0',a.totaltime,0)),
b.user_name, MONTHNAME(a.date) FROM a
JOIN c on c.id = a.id
JOIN b on b.userid = c.userid
LEFT JOIN d on a.projid = d.projectid
LEFT JOIN e on a.account_id = e.salesorderid
LEFT JOIN f on e.salesorderid = f.salesorderid
LEFT JOIN g on d.projectid = g.projectid
WHERE c.deleted != "1" AND YEAR(DATE(NOW()))
and (( f.cf_991 = '1' OR g.cf_990 = '1') or( f.cf_991 = '0' OR g.cf_990 = '0') )
group by ser_name,MONTHNAME(a.date);
Related
i have this query:
select b.observation, b.created_date (datetime field)
FROM logistics_wms.reception_documents a
join logistics_wms.reception b on a.reception_id = b.id
join logistics_wms.document c on a.documents_id = c.id
join logistics_wms.document_type d on d.id = c.document_type_id
join logistics_wms.document_line e on c.id = e.document_id
join logistics_wms.product g on e.product_id = g.id
where b.status = 'SUBMITTED'
and b.created_date >= '2020-04-01 00:00:00'
i used '2020-04-01' too
And the result is:
but if i used:
select b.observation, b.created_date (datetime field)
FROM logistics_wms.reception_documents a
join logistics_wms.reception b on a.reception_id = b.id
join logistics_wms.document c on a.documents_id = c.id
join logistics_wms.document_type d on d.id = c.document_type_id
join logistics_wms.document_line e on c.id = e.document_id
join logistics_wms.product g on e.product_id = g.id
where b.status = 'SUBMITTED'
and b.created_date >= '2020-04-15 00:00:00'
i used '2020-04-15' too
the result is:
why when i select 2020-04-01 the query doesn´t get the last two records of the second query??!!
Please help me
Thank you
Given the fact that time has no importance in your case (00:00:00), it is much simpler to use MySQL DATE function in your filter. Can you try using :
where b.status = 'SUBMITTED' and DATE(b.created_date) >= '2020-04-01'
and
where b.status = 'SUBMITTED' and DATE(b.created_date) >= '2020-04-15'
I'm trying to join a different table depended on the value of a "task_type" column
What is wrong with the syntax?
Thanks.
SELECT t.*,s.task_name
CASE
WHEN t.task_type = 0 THEN
LEFT JOIN scheduler_tasks s ON s.scheduler_task_id = t.task_id
WHEN t.task_type = 1 THEN
LEFT JOIN invoice_tasks s ON s.uid = t.task_id
END
FROM task_timings t
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND t.start_date <= DATE(?)
I believe you want this:
SELECT t.*,
COALESCE(s.task_name, i.task_name) as task_name
FROM task_timings t LEFT JOIN
scheduler_tasks s
ON s.scheduler_task_id = t.task_id AND
t.task_type = 0 LEFT JOIN
invoice_tasks i
ON i.uid = t.task_id AND t.task_type = 1
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND
t.start_date <= DATE(?);
Can you try like this :
SELECT t.*,s.task_name FROM task_timings t
CASE
WHEN t.task_type = 0 THEN
LEFT JOIN scheduler_tasks s ON s.scheduler_task_id = t.task_id
WHEN t.task_type = 1 THEN
LEFT JOIN invoice_tasks s ON s.uid = t.task_id
END
WHERE t.account_id = ? AND t.start_date >= DATE(?) AND t.start_date <= DATE(?)
Using this query will get the result as in shown in below image link
select c.property_title,b.property_title as building,d.property_title as floor,e.house_name as house,
f.room_no as room,g.bed_no as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
I have written the query for fetching the count of parent and child .will get the building count as 7 floor count as 7 house count as 5 room count as 4, instead the count of building should be 2, floor = 2, house = 4, Rooms = 5 and Bed 1.
select c.property_title,count(b.property_title)as building,count(d.property_title)as floor,count(e.house_name)as house,
count(f.room_no)as room,count(g.bed_no) as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.house_id = g.id and g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
Thank you in advance
Try this:
SQL SERVER
select count([building]) over (order by building) as [bulding count],
count([floor]) over (order by [floor],[building]) as [floor count],
count([house]) over (order by building,[floor],[house]) as [house count],
count([room]) over (order by building,[floor],[house],[room]) as [room count],
count([bed]) over (order by building,[floor],[house],[room],[bed]) as [room count]
I need help understanding WHERE the completion state of a resource (file) in Moodle is stored.
Please see attached images for more information.
The flag next to the file is marked as completed once the user viewed
This is setup in the Completion settings of this file/resource.
I need to generate a SQL report showing the file and the completion state.
I already have the "difficult part" of the query, I just need to select the completion state from "table-X" and wha-la!
Thank you.
SELECT r.id, r.name, r.course, cmc.userid, cmc.completionstate, cmc.viewed
FROM mdl_course_modules_completion cmc
JOIN mdl_course_modules cm ON cm.id = cmc.coursemoduleid
JOIN mdl_modules m ON m.id = cm.module AND m.name = 'resource'
JOIN mdl_resource r ON r.id = cm.instance
The completionstate and viewed constants are in /lib/completionlib.php
eg:
COMPLETION_INCOMPLETE = 0
COMPLETION_COMPLETE = 1
COMPLETION_COMPLETE_PASS = 2
COMPLETION_COMPLETE_FAIL = 3
COMPLETION_COMPLETE_RPL = 4 // This is used in Totara.
COMPLETION_NOT_VIEWED = 0
COMPLETION_VIEWED = 1
Okay, so I found the table and column.
Table: mdl_course_modules_completion
Column: Viewed
I will post my report code below, hopefully this might help the next guy.
Take Note: I joined all the module types to the query and filter only for type quiz, lesson and resource in the where statement. I did this because I am only interested in these 3 types. I however did not remove the joins because someone else might need the code.
SELECT DISTINCT
u.firstname AS 'Firstname'
,u.lastname AS 'Lastname'
,u.institution AS 'Institution'
,u.department AS 'Department'
,u.city AS 'City/Site'
,cc.name AS 'Course'
,c.fullname AS 'Module'
,CASE
WHEN mf.name IS NOT NULL THEN mf.name
WHEN mb.name IS NOT NULL THEN mb.name
WHEN mr.name IS NOT NULL THEN mr.name
WHEN mu.name IS NOT NULL THEN mu.name
WHEN mq.name IS NOT NULL THEN mq.name
WHEN mp.name IS NOT NULL THEN mp.name
WHEN ml.name IS NOT NULL THEN ml.name
ELSE NULL
END AS activityname
,CASE WHEN mdl.name = 'lesson' THEN CASE WHEN mlg.id IS NOT NULL AND mlg.completed IS NOT NULL THEN 'Complete' ELSE 'Incomplete' END
WHEN mdl.name = 'quiz' THEN CASE WHEN mqg.id IS NOT NULL AND mqg.timemodified IS NOT NULL THEN 'Complete' ELSE 'Incomplete' END
WHEN mdl.name = 'resource' THEN CASE WHEN cmc.viewed = 1 THEN 'Complete' ELSE 'Incomplete' END
END AS Status
FROM
mdl_user u
JOIN mdl_user_enrolments ue ON ue.userid = u.id
JOIN mdl_enrol E on E.id = ue.enrolid
JOIN mdl_course c ON c.id = E.courseid
JOIN mdl_course_categories cc ON c.category = cc.id
JOIN mdl_course_modules cm ON cm.course = c.id
JOIN mdl_course_modules_completion cmc ON cmc.coursemoduleid = cm.id
JOIN mdl_context AS ctx ON ctx.contextlevel = 70 AND ctx.instanceid = cm.id
JOIN mdl_modules AS mdl ON cm.module = mdl.id
LEFT JOIN mdl_forum AS mf ON mdl.name = 'forum' AND cm.instance = mf.id
LEFT JOIN mdl_book AS mb ON mdl.name = 'book' AND cm.instance = mb.id
LEFT JOIN mdl_resource AS mr ON mdl.name = 'resource' AND cm.instance = mr.id
LEFT JOIN mdl_url AS mu ON mdl.name = 'url' AND cm.instance = mu.id
LEFT JOIN mdl_quiz AS mq ON mdl.name = 'quiz' AND cm.instance = mq.id
LEFT JOIN mdl_quiz_grades mqg ON mqg.quiz = mq.id
LEFT JOIN mdl_page AS mp ON mdl.name = 'page' AND cm.instance = mp.id
LEFT JOIN mdl_lesson AS ml ON mdl.name = 'lesson' AND cm.instance = ml.id
LEFT JOIN mdl_lesson_grades mlg ON mlg.lessonid = ml.id
LEFT JOIN mdl_files AS f ON f.contextid = ctx.id
LEFT JOIN mdl_files_reference fr ON fr.id = f.referencefileid
WHERE mdl.name in ('quiz','lesson','resource')
ORDER BY firstname,Lastname,cc.name,c.fullname
Having an issue with a specific section of a query using DATEDIFF:
select 1 as NumApps,
LenderInfo.Name as LenderName,
CASE WHEN ApplicationInfo.AEType IS NULL OR ApplicationInfo.AEType = 0 THEN 'Unknown' ELSECONCAT (AEContact.FirstName, ' ', AEContact.LastName) END As AEName,
CASE WHEN b.createdby > 0 THEN CONCAT (contactinfo.firstname, ' ', contactinfo.lastname) END AS LogActionBy,
CASE WHEN ApplicationInfo.RecertificationById IS NULL THEN CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale' ELSE 'Correspondent' END ELSE CASE WHEN ApplicationInfo.IsCorrespondent IS NULL OR ApplicationInfo.IsCorrespondent = 0 THEN 'Wholesale - Recert' ELSE 'Correspondent- Recert' END END As AppType,
Applicationinfo.SubmissionDate,
ApplicationInfo.ApplicationID,
b.status AS LogStatus,
b.CreatedOn as LogDate,
companyinfo.Name,
companyinfo.NMLSEntityID,
applicationinfo.CreatedDate,
ApplicationInfo.Status as ApplicationStatus,
ApplicationInfo.StatusChangeDate,
DATEDIFF ((Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved'),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval')) AS DaysToApprove
from applicationinfo
INNER JOIN CompanyInfo ON(ApplicationInfo.CompanyId = CompanyInfo.CompanyId)
left join CompanyInfo As LenderInfo ON (ApplicationInfo.LenderId = LenderInfo.CompanyId)
LEFT JOIN UserInfo ON (UserInfo.UserId = ApplicationInfo.LastModifiedById)
LEFT JOIN ContactInfo ON UserInfo.ContactId = ContactInfo.ContactId
LEFT JOIN ContactInfo AS Comergence ON(ApplicationInfo.ComergenceRepId = Comergence.ContactId)
LEFT JOIN UserInfo AS AEUser ON(AEUser.UserId = ApplicationInfo.AEType)
left join paymentinfo on applicationinfo.applicationid = paymentinfo.applicationid
Inner join applicationstatuschangelog b on applicationinfo.applicationid = b.applicationid
LEFT JOIN ContactInfo AS AEContact ON(AEContact.ContactId = AEUser.ContactId)
LEFT JOIN UserInfo AS StatusUser ON(StatusUser.UserId = ApplicationInfo.StatusChangeById)
LEFT JOIN ContactInfo AS StatusContact ON(StatusContact.ContactId = StatusUser.ContactId)
LEFT JOIN ApprovalStatus ON(ApplicationInfo.ApplicationId = ApprovalStatus.ApplicationId AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyHQId
AND ApplicationInfo.CompanyId = ApprovalStatus.CompanyId)
Where ApplicationInfo.Status NOT In ('Approved Monitor Only')
AND LenderInfo.ActiveLenderContract = 1
AND COALESCE(ApplicationInfo.IsDeleted,0) <> 1
AND b.Status NOT IN ('Incomplete', 'Not Submitted')
Group BY b.ApplicationID`
I've seen some people mentioning using IN instead of = within the subquery, but I can't seem to get it to work. Any ideas out there? Thanks in advance.
I was able to get the query working by adding 'Limit 1' after the identified status:
(Select CreatedOn From ApplicationStatusChangeLog a where a.ApplicationId = b.ApplicationId
And a.status = 'Approved' LIMIT 1),(Select CreatedOn From ApplicationStatusChangeLog a Where a.ApplicationId = b.ApplicationId And a.status = 'Pending Approval' LIMIT 1)