I have a Date/Time parameter to my report:
But when I run my query, I get no results:
SELECT HD_QUEUE.NAME as qname, HD_TICKET.ID, HD_TICKET.CREATED, HD_TICKET.TIME_CLOSED, CUSTOMER.FULL_NAME as custfullname,
HD_STATUS.NAME as statname, HD_TICKET.TITLE, left(ASSIGNEE.FULL_NAME, 40) as assignee,
HD_PRIORITY.NAME as pname, HD_CATEGORY.NAME as catname
FROM HD_TICKET
INNER JOIN HD_QUEUE
ON HD_TICKET.HD_QUEUE_ID = HD_QUEUE.ID
INNER JOIN USER CUSTOMER
ON HD_TICKET.SUBMITTER_ID=CUSTOMER.ID
INNER JOIN USER ASSIGNEE
ON HD_TICKET.OWNER_ID=ASSIGNEE.ID
INNER JOIN HD_STATUS
ON (HD_TICKET.HD_STATUS_ID=HD_STATUS.ID)
AND (HD_TICKET.HD_QUEUE_ID=HD_STATUS.HD_QUEUE_ID)
INNER JOIN HD_PRIORITY
ON HD_TICKET.HD_PRIORITY_ID = HD_PRIORITY.ID
and HD_TICKET.HD_QUEUE_ID = HD_PRIORITY.HD_QUEUE_ID
INNER JOIN HD_CATEGORY
ON HD_TICKET.HD_CATEGORY_ID = HD_CATEGORY.ID
and HD_TICKET.HD_QUEUE_ID = HD_CATEGORY.HD_QUEUE_ID
left join ASSET on ASSET.ID = HD_TICKET.ASSET_ID
left join ASSET_DATA_6 on ASSET.ASSET_DATA_ID = ASSET_DATA_6.ID
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < #date_param);
What am I doing wrong?
MySQL does not allow named parameters. Use '?' instead of '#date_param' in the query.
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < ?;
Then check the Dataset Properties and make sure the '?' is associated with the value of your parameter:
Related
I'm trying to join several tables in my database.
I need to get account information from the 'accounts' table with the latest meter history on it.
And if an account has no meter history, I want it to show 'meter' related fields as NULL.
Here's my query so far:
SELECT
accounts.id,
accounts.account_order,
acc.id AS accounts_class_id,
acc.zone,
acc.book,
acc.service_class,
acc.size,
acc.account_no AS series_no,
accounts.status,
application_address.address_line,
concessionaires.firstname,
concessionaires.middlename,
concessionaires.lastname,
mb.brand_name,
m.meter_no,
ms.meter_status
FROM
accounts
INNER JOIN
applications
ON accounts.application_id = applications.id
LEFT JOIN
application_address
ON applications.application_no = application_address.application_no
LEFT JOIN
concessionaires
ON applications.concessionaire_no = concessionaires.concessionaire_no
INNER JOIN
accounts_classifications acc
ON accounts.id = acc.account
INNER JOIN meter_history mh
ON mh.id = (SELECT id FROM meter_history mh2
WHERE mh2.account_id = accounts.id
ORDER BY mh2.status_date DESC
LIMIT 1)
LEFT JOIN
meter_status ms
ON mh.meter_status = ms.id
INNER JOIN
meter m
ON mh.meter = m.id
LEFT JOIN
meter_brand mb
ON m.meter_brand = mb.id
WHERE
acc.book = 1 AND acc.zone = 20 AND applications.status = '6' AND acc.status = '1'
This would return only accounts with meter history on it.
Where should I put my IF condition so I get accounts with no history as well, or if that is even possible with my query. Thank you!
$sql = "SELECT cc.name AS c_name, ev.name AS event_name, ev.eventModeId AS event_mode, ev.carClassHash, cc.carClassHash
FROM EVENT_DATA e
INNER JOIN PERSONA p ON e.personaId = p.ID
INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId
INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
INNER JOIN USER u ON u.ID = p.USERID
LEFT JOIN BAN b ON b.user_id = u.ID
INNER JOIN EVENT ev ON ev.ID = e.EVENTID
WHERE (p.name = ?
AND ev.carClassHash = cc.carClassHash)";
This query works for me except I'd also like to display any carClassHash with the value '607077938'. Is there a way I could somehow keep the above query to check if the event hash matches the car class hash but also still display values where the carClassHash (cc.carClassHash) is equal to '607077938'?
Thanks! :)
Why not use OR in the WHERE clause?
[...]
WHERE ((p.name = ? AND ev.carClassHash = cc.carClassHash) OR (cc.carClassHash = 607077938))
This should work if that value is an integer. If it's a string, use quotes around it.
First, the table names and layouts:
Here are my desired results:
Here is the 'got ya' (trick) I guess..
I will only be passing a (dynamic) imis_id/user_id (col names currently not matching on one table)...
So a lookup (select) will need to be done on the relations table for that passed in (dynamic) id.. and where current_org = 1.
This will get/give me the target org_id in which I need to grab all user info (that are associated with the org_id).. and all org details.
Here is one weak/failed attempt: (it uses a hardcoded org_id) which is not valid.. I need to ONLY pass in the user_id/imis_id.. and where current_org = 1 to get the target org_id.
SELECT genealogy_orgs.org_id, genealogy_orgs.org_name,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name
FROM genealogy_orgs
INNER JOIN genealogy_relations ON genealogy_orgs.org_id = genealogy_relations.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_orgs.org_id = '84864';
Here is another failed attempt (which only returns 1 row).. but uses the correct criteria:
SELECT
genealogy_relations.org_id,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name,
genealogy_orgs.org_name
FROM genealogy_relations
INNER JOIN genealogy_orgs ON genealogy_relations.org_id = genealogy_orgs.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_relations.user_id = '00003' AND genealogy_relations.current_org = '1';
At this point, I'm not even sure what I need to search for? Is this where a 'sub-query/sub-select' comes into play?
My MySQL-fu is limited to very direct/plain-jane query types. This is getting to be more advanced than I am used to.
You need to join the genealogy_relations table with itself on org_id:
SELECT o.*, u.*
FROM genealogy_relations r1
JOIN genealogy_relations r2 ON r2.org_id = r1.org_id
JOIN genealogy_orgs o ON o.org_id = r2.org_id
JOIN genealogy_users u ON u.imis_id = r2.user_id
WHERE r1.user_id = '00003'
AND r1.current_org = '1'
Remove the genealogy_relations.user_id = '00003' from the query and it should work
SELECT
genealogy_relations.org_id,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name,
genealogy_orgs.org_name
FROM genealogy_relations
INNER JOIN genealogy_orgs ON genealogy_relations.org_id = genealogy_orgs.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_relations.current_org = '1';
I have an SQL query where in my SELECT part I have a column which is a result of an IF statement. As you can see 'final_vendor_id' contains the value of ioproductrel.vendorid except when it's value is null or 0, because in this case the value of products.vendor_id is used:
SELECT
IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid) AS 'final_vendor_id',
account.accountname AS 'account',
salesorder.duedate
FROM
internalorder LEFT JOIN ioproductrel ON internalorder.internalorderid = ioproductrel.internalorderid
LEFT JOIN products ON ioproductrel.productid = products.productid
LEFT JOIN slip_relations ON internalorder.internalorderid = slip_relations.child_crmid
INNER JOIN salesorder ON slip_relations.parent_crmid = salesorder.salesorderid
LEFT JOIN account ON salesorder.accountid = account.accountid
LEFT JOIN account AS acc2 ON acc2.accountid = final_vendor_id
WHERE
internalorder.internalorderid = 8982
I want to join a table based on this 'final_vendor_id', but I just get an error that no field with such a name exists.
If I write the whole IF statement in the JOIN part again, it seems to work but Im not sure if it is safe and Im wondering if is there any simplier way than this:
SELECT
IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid) AS 'final_vendor_id',
account.accountname AS 'account',
salesorder.duedate
FROM
internalorder LEFT JOIN ioproductrel ON internalorder.internalorderid = ioproductrel.internalorderid
LEFT JOIN products ON ioproductrel.productid = products.productid
LEFT JOIN slip_relations ON internalorder.internalorderid = slip_relations.child_crmid
INNER JOIN salesorder ON slip_relations.parent_crmid = salesorder.salesorderid
LEFT JOIN account ON salesorder.accountid = account.accountid
LEFT JOIN account AS acc2 ON acc2.accountid = IF(ioproductrel.vendorid IS NULL OR ioproductrel.vendorid = 0, products.vendor_id, ioproductrel.vendorid)
WHERE
internalorder.internalorderid = 8982
What if set single quotes in left join section like:
...ON acc2.accountid = 'final_vendor_id'
I have to write an SQL statement which contain a field that contain two different values consecutively but in the way I have wrote it, it return always null because it is interpreted as having the two value in the same time!
My conditions should be : (ci.field = 'Group' and ci.oldString = 'Triage' ) and (ci.field='assignee' and ci.newString is not NULL)
That means calculate time between: when the issue is assigned to group named Triage and when the issue is assigned to a person.
How can I fix it?
My SQL statement:
select TIMEDIFF(a.created,b.created)
from
(select g.created, g.issueid as groupid1
from changegroup g
join changeitem ci on (ci.groupid = g.id)
join jiraissue ji on (ji.id = g.issueid)
join project p on (p.id = ji.project)
join priority pr on (pr.id = ji.priority)
where ci.field = 'Group'
and ci.oldString = 'Triage'
and ci.field='assignee'
and ci.newString is not NULL
and p.pname = 'Test'
and pr.pname='P1'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) a
left join (
select ji.created, ji.id as groupid2
from jiraissue ji
join changegroup g on (g.issueid = ji.id)
join project p on (p.id = ji.project)
where p.pname = 'Test'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) b ON (a.groupid1 = b.groupid2);
This is the table from which I should retrieve data
See my comment about the quality of your question but a hint at how to solve this goes like (assuming you can make sure this doesn't create 1-n joins)
select groupid_orsomething_else, TIMEDIFF(a.created, b.created)
from yourtable
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'Group' and oldstring is 'Triage'
) a
on a.groupid_orsomething_else = yourtable.groupid_orsomething_else
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'assignee' and oldstring is null) b
on b.groupid_orsomething_else = yourtable.groupid_orsomething_else