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(?)
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'
SELECT
SUM(CASE
WHEN db1.card_type = 'debit' THEN 1
ELSE 0
END) AS DEBIT,
SUM(CASE
WHEN db1.card_type = 'credit' THEN 1
ELSE 0
END) AS CREDIT,
DATE_FORMAT(Latest, '%Y-%m') AS Dato
FROM
(SELECT
pp.debtor_id, pp.card_type, MAX(pp.created) AS Latest
FROM
capital.credit_card cc
JOIN capital.card_to_debtor ctd ON cc.id = ctd.card_id
AND debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
FROM
analysis.full_settlement fs
WHERE
((reason = 'BANK_PAYMENT'
OR fs.reason = 'CARD_PAYMENT')
AND fs.amount < 0)
OR fs.REASON = 'TRIVIAL_BALANCE'
OR fs.id = 13793327
OR fs.id = 7451808)
JOIN analysis.full_settlement fs ON fs.debtor_id = ctd.debtor_id
JOIN (SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) pp ON fs.debtor_id = pp.debtor_id
AND pp.created <= fs.created
GROUP BY pp.debtor_id , fs.created) db
JOIN
(SELECT
p.debtor_id, ccc.card_type, p.created
FROM
capital.payment p
JOIN capital.card_subscription cs ON cs.id = p.subscription_id
JOIN (SELECT
cc.id, ctd.card_id, cc.card_type, ctd.debtor_id
FROM
capital.card_to_debtor ctd
JOIN capital.credit_card cc ON cc.id = ctd.card_id) ccc ON ccc.card_id = cs.card_id
AND ccc.debtor_id = p.debtor_id) db1 ON db1.debtor_id = db.debtor_id
AND db1.created = db.Latest
GROUP BY YEAR(Latest) , MONTH(Latest)
debtor_id IN (SELECT
fs.debtor_id, fs.created, fs.reason
debtor_id is one column, so you can only compare it to one other column. Change it to
debtor_id IN (SELECT
fs.debtor_id FROM...
I have the following query, which I try to order by c_priority (alias of c.priority_nl) but this is being ignored. I have tried but am not able to come with a solution. Tips that lead to a solution are more than welcome!
SELECT e.id AS events_id,
e.clients_id AS clients_id,
e.leagues_id,
e.title AS e_title,
IF(m1.date_start, m1.date_start, m2.date_start) AS date_time_start,
l.id AS leagues_id,
l.title AS leagues_title,
c.priority_nl AS c_priority,
p1.id AS p1_id,
p1.title AS ml_away_title,
IF(p1.odds_decimal > 0, p1.odds_decimal, 'N/a') AS ml_away_odds,
p2.id AS p2_id,
p2.title AS ml_home_title,
IF(p2.odds_decimal > 0, p2.odds_decimal, 'N/a') AS ml_home_odds,
p3.id AS p3_id,
p3.title AS sp_away_title,
p3.odds_decimal AS sp_away_odds,
p3.handicap AS sp_away_handicap,
p4.id AS p4_id,
p4.title AS sp_home_title,
p4.odds_decimal AS sp_home_odds,
p4.handicap AS sp_home_handicap
FROM fcaopnyp_asp_events e
LEFT JOIN fcaopnyp_asp_markets m1
ON m1.events_id = e.id
AND ( m1.market_id = 960
OR m1.market_id = 1
OR m1.title = 'Money Line'
OR m1.title = 'Money Line (US)'
OR m1.title = 'st'
OR m1.title = 'Head to Head' )
LEFT JOIN fcaopnyp_asp_participants p1
ON p1.markets_id = m1.id
AND p1.title LIKE 'Away%'
LEFT JOIN fcaopnyp_asp_participants p2
ON p2.markets_id = m1.id
AND p2.title LIKE 'Home%'
LEFT JOIN fcaopnyp_asp_markets m2
ON m2.events_id = e.id
AND ( m2.market_id = 1453
OR m2.market_id = 2
OR m2.title = 'Handicap'
OR m2.title = 'Spread'
OR m2.title = 'Point Spread'
OR m2.title = 'ah'
OR m2.title LIKE 'Handicap Match Result%' )
LEFT JOIN fcaopnyp_asp_participants p3
ON p3.markets_id = m2.id
AND p3.title LIKE 'Away%'
LEFT JOIN fcaopnyp_asp_participants p4
ON p4.markets_id = m2.id
AND p4.title LIKE 'Home%'
INNER JOIN fcaopnyp_asp_leagues l
ON e.leagues_id = l.id
INNER JOIN fcaopnyp_asp_clients c
ON e.clients_id = c.id
WHERE e.leagues_id IN( 2 )
AND ( Date_add(m1.date_start, INTERVAL 2 hour) > Now()
OR Date_add(m2.date_start, INTERVAL 2 hour) > Now() )
AND ( ( e.leagues_id != 8
AND ( m1.date_start < Date_add(Now(), INTERVAL 72 hour)
OR m2.date_start < Date_add(Now(), INTERVAL 72 hour) ) )
OR e.leagues_id = 8 )
ORDER BY Date(date_time_start),
Hour(date_time_start),
Time(date_time_start),
e_title,
c_priority
LIMIT 50;
Shouldn't you order by c.priority_nl instead of c.priority
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);
Hi I have run into a dilemma, I am doing this query:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories, pr.promo_name, pr.add_value_text, c.contract_id, c.cmeal_plan, c.cmin_markup, c.civa, c.tax_include, c.hotel_id, hi.hname, hi.hstars, im.image_file, pl.plan_name, ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi ON hi.hotel_id = c.hotel_id AND hi.destination_id = '6460'
INNER JOIN images AS im ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl ON pl.plan_code = c.cmeal_plan AND pl.lang = '1'
INNER JOIN hotel_categories AS hc ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca ON ca.category_code = hc.category_code AND ca.lang = '1'
LEFT JOIN
(SELECT
r.hotel_id, AVG(r.double) AS price
FROM
rates AS r ) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr ON pr.hotel_id = hi.hotel_id AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0 AND pr.book_start <= '2012-11-01' AND pr.book_end >= '2012-11-02' AND travel_start <= '2012-11-23' AND travel_end >= '2012-11-30' AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01' AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id
I am getting all the desired results except for the sub select query.. each hotel has a price but it is only giving me back one result and the rest are all null. Is there an error in my query? If any additional information is needed please let me know and thank you in advance for any help!
You are missing the GROUP BY in your subquery, so MySQL will only return one-value. If you want all hotel_id's then you need to GROUP BY that field:
SELECT GROUP_CONCAT(DISTINCT(ca.category_name) SEPARATOR ', ') AS categories,
pr.promo_name,
pr.add_value_text,
c.contract_id,
c.cmeal_plan,
c.cmin_markup,
c.civa,
c.tax_include,
c.hotel_id,
hi.hname,
hi.hstars,
im.image_file,
pl.plan_name,
ra.price
FROM contracts AS c
INNER JOIN hotel_info AS hi
ON hi.hotel_id = c.hotel_id
AND hi.destination_id = '6460'
INNER JOIN images AS im
ON im.foreign_id = hi.hotel_id
INNER JOIN meal_plan AS pl
ON pl.plan_code = c.cmeal_plan
AND pl.lang = '1'
INNER JOIN hotel_categories AS hc
ON hc.hotel_id = hi.hotel_id
INNER JOIN categories AS ca
ON ca.category_code = hc.category_code
AND ca.lang = '1'
LEFT JOIN
(
SELECT r.hotel_id, AVG(r.double) AS price
FROM rates AS r
GROUP BY r.hotel_id <-- add a GROUP BY hotel_id then you will get avg() for each hotel
) AS ra
ON ra.hotel_id = hi.hotel_id
LEFT JOIN promotions AS pr
ON pr.hotel_id = hi.hotel_id
AND FIND_IN_SET(c.contract_id, pr.contract_id) > 0
AND pr.book_start <= '2012-11-01'
AND pr.book_end >= '2012-11-02'
AND travel_start <= '2012-11-23'
AND travel_end >= '2012-11-30'
AND pr.lang = '1'
WHERE c.cstart <= '2012-11-01'
AND c.cend >= '2012-11-01'
AND hi.status = '1'
AND im.type ='1'
GROUP BY hi.hotel_id