I have a query which I have found on Sopocosy sport feed documentation. I have change this query to meet my need. Here is my query
SELECT
e.id as eid,
ts.name AS stage_name,
DATE_FORMAT(e.startdate, '%d.%m.%Y') AS startdate,
DATE_FORMAT(e.startdate, '%H:%i') AS starttime,
MIN(IF( ep.number = 1, p.name, NULL)) AS home_team,
IF(e.status_type = 'notstarted', '-',(MIN(IF(ep.number = 1, r.value, NULL)))) AS home_score,
IF(e.status_type = 'notstarted', '-',(MIN(IF(ep.number = 2, r.value, NULL)))) AS away_score,
MIN(IF(ep.number = 2, p.name, NULL)) AS away_team,
es.name AS status_text
FROM
tournament_template AS tt INNER JOIN
tournament AS t ON t.tournament_templateFK = tt.id INNER JOIN
tournament_stage AS ts ON t.id = ts.tournamentFK INNER JOIN
event AS e ON ts.id = e.tournament_stageFK INNER JOIN
event_participants AS ep ON e.id = ep.eventFK LEFT JOIN
status_desc AS es ON e.status_descFK = es.id LEFT JOIN
participant AS p ON ep.participantFK = p.id LEFT JOIN
result AS r ON ep.id = r.event_participantsFK AND r.result_code = 'runningscore' LEFT JOIN
property AS prop ON e.id = prop.objectFK AND prop.object ='event' AND prop.name = 'Live'
WHERE
tt.sportFK = '1'
GROUP BY
e.id
ORDER BY
ts.id, e.startdate, e.id
Now my question is
Can I use home_team and away_team in where clause. If yes then how. Like theoretically I want this in where clause.
WHERE home_team = 'Navibank Saigon' OR away_team = 'Navibank Saigon' ;
But it is giving Unknown Column error.
Please Help
Looks like p.name is the true column name of what is being returned as home_team so make your WHERE clause like
WHERE p.name = 'Navibank Saigon'
The reason why you can't use WHERE home_team = 'Navibank Saigon' OR away_team = 'Navibank Saigon' is that you assign these 'column names' (alias) on the result set after the search query is executed. The WHERE statement is part of the search query and needs the real column name, in this case p.name. On the other hand the ORDER BY statement is executed on the resultset and can thus use the column aliases like 'home_team'.
For the query part: you're using both teams in an aggregate function (MIN) on a GROUP BY statement. Filtering on a GROUP BY statement has to be done with HAVING instead of WHERE.
this results in:
GROUP BY
e.id
HAVING
MIN(IF( ep.number = 1, p.name, NULL)) = 'Navibank Saigon'
OR MIN(IF( ep.number = 1, p.name, NULL)) = 'Navibank Saigon'
Related
I have this query: (apologies for complexity, I'm not certain what I can remove without impacting the question)
SELECT COUNT(*) AS total,
SUM(o.total) AS total_loss,
SUM((SELECT SUM(cost_price) FROM `orders_items` WHERE orders_id = o.orders_id)) AS cost_total ,
SUM((SELECT COUNT(*) FROM refunds AS r1 WHERE r1.order_id = r.order_id AND NOT r.reason IS NULL)) AS refund_count ,
SUM((SELECT COUNT(*) FROM exchanges AS e1 WHERE e1.order_id = e.order_id AND e.type = :countResend AND NOT e.reason IS NULL)) AS resend_count ,
SUM((SELECT COUNT(*) FROM exchanges AS e2 WHERE e2.order_id = e.order_id AND e.type = :countExchange AND NOT e.reason IS NULL)) AS exchange_count
FROM orders AS o
JOIN sales_channel_config AS s ON o.sales_channel = s.sales_channel AND o.sub_sales_channel = s.sub_sales_channel
JOIN courier_service AS cs ON o.courier_service = cs.code
LEFT JOIN refunds AS r ON o.orders_id = r.order_id
JOIN orders_items AS oi ON o.orders_id = oi.orders_id
JOIN third_party_config AS tc ON SUBSTRING(oi.product_id_new, 3, 2) = tc.code
LEFT JOIN exchanges AS e ON o.orders_id = e.order_id
WHERE 1 = 1
AND o.tracking_num NOT IN (:cancelStatus)
AND (o.order_date >= :startDate AND o.order_date <= :endDate)
AND o.courier_service = :courier
AND SUBSTRING(oi.product_id_new, 3, 2) = :supplier
AND (NOT r.reason IS NULL OR NOT e.reason IS NULL)
The problem I'm having is that the various SUM((query)) clauses are counting duplicate orders, which is proving difficult to resolve. For example:
SUM((SELECT COUNT(DISTINCT r1.order_id) FROM refunds AS r1 WHERE r1.order_id = r.order_id AND NOT r.reason IS NULL)) AS refund_count ,
And
SUM((SELECT COUNT(*) FROM refunds AS r1 WHERE r1.order_id = r.order_id AND NOT r.reason IS NULL GROUP BY r1.order_id)) AS refund_count ,
Do not lower the resulting SUM at all. I have confirmed that the data returned will contain duplicates via another structurally identical query that returns rows from the parent query. When the other query is run without duplicate filtering, the counts match correctly so I'm confident that my problem query is accurate aside from including duplicated order ids.
So can anyone suggest another approach I might try?
For anyone who might benefit:
I removed most of the select logic and grouped on orders_id, which gives me an entirely accurate list of relevant orders:
SELECT o.orders_id AS order_id, r.id AS refund_id, e.id AS exchange_id, e.type AS exchange_type
FROM orders AS o
JOIN sales_channel_config AS s ON o.sales_channel = s.sales_channel AND o.sub_sales_channel = s.sub_sales_channel
JOIN courier_service AS cs ON o.courier_service = cs.code
LEFT JOIN refunds AS r ON o.orders_id = r.order_id
JOIN orders_items AS oi ON o.orders_id = oi.orders_id
JOIN third_party_config AS tc ON SUBSTRING(oi.product_id_new, 3, 2) = tc.code
LEFT JOIN exchanges AS e ON o.orders_id = e.order_id
WHERE 1 = 1
AND o.tracking_num NOT IN (:cancelStatus)
AND (o.order_date >= :startDate
AND o.order_date <= :endDate)
AND o.courier_service = :courier
AND SUBSTRING(oi.product_id_new, 3, 2) = :supplier
AND (NOT r.reason IS NULL OR NOT e.reason IS NULL)
GROUP BY (o.orders_id)
I've bitten the bullet here. I'm going to do some post processing to get the counts themselves, which is at least possible for me now.
Still don't understand why getting distinct values in the sub selects failed though.
I have a query that works well, where i would like an assistance is how to incorporate a condition to determine the value displayed on a given column. That is if a column of a table has a value say "Authorization of COA" then the column date added should show the date else the column should show null
Here is my query so far
SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' , t.date_added (//should show the value of date_added else NULL based on a condition for this column),
FROM request r
LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
WHERE r.client_id='$cid'
AND r.created_at BETWEEN '$start' AND '$end'
AND a_s.department_id = '$dept'
GROUP BY r.request_id
ORDER BY `r`.`created_at` DESC "
A simple CASE statement should be sufficient in this case, I believe.
SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id,
CONCAT(u.fname,' ',u.lname) 'analyst',
CASE WHEN ConditionColumn = 'Authorization of COA' THEN t.date_added
ELSE NULL END AS 'date_added'
FROM request r
LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
WHERE r.client_id='$cid'
In both mysql and ms sql you can use case expression (mysql case; ms sql case) to assign value to a field conditionally:
SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' ,
CASE WHEN Column_To_Test = 'Authorization of COA' THEN t.date_added
ELSE null
END AS date_added
FROM request r
LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
WHERE r.client_id='$cid'
AND r.created_at BETWEEN '$start' AND '$end'
AND a_s.department_id = '$dept'
GROUP BY r.request_id
ORDER BY `r`.`created_at` DESC "
This way the solution is more portable between the databases. Mysql offers if() function, ms sql iif() to achieve the above, but those are product specific solutions.
Try to use the Case statements like.
CASE WHEN ( Condition_1 AND Condition_2 OR ... Condition_N) THEN (Value_1) ELSE (Value_1) END
Here Condition_1 can be column record value and its combination of Column record values
and Value_1 can be single value [Ex: Null or "some string"] or Returned value from nested sql statement like [Ex: (select count(*) from ...)]
Give this a try:
SELECT r.request_id, r.product_name, r.created_at, r.can,a_s.stat, r.client_id, CONCAT(u.fname,' ',u.lname) 'analyst' ,
IFF(nameOfColumnToTest = 'Authorization of COA', t.date_added, null) AS DateAdded
FROM request r
LEFT OUTER JOIN assigned_samples a_s ON r.request_id = a_s.labref
LEFT OUTER JOIN user u ON a_s.analyst_id = u.id
LEFT OUTER JOIN tracking_table t ON r.request_id = t.labref
WHERE r.client_id='$cid'
AND r.created_at BETWEEN '$start' AND '$end'
AND a_s.department_id = '$dept'
GROUP BY r.request_id
ORDER BY `r`.`created_at` DESC "
Hi I'm having an issue with a query that was once working. My SQL skills aren't all that great, not sure what I'm missing. Or if this is the correct approach. Maybe use a temp table instead?
The basic gist is given a certain time frame, I need to calculate the highest aggregate of points over 5 classes.
trialScores - keeps scores/points,
trials, dog, people and member tables are just meta data
classId 5 requires a different date range
Here is my Query on MySQL
select
t.id,
d.id,
p.id,
p.firstname,
p.lastname,
d.id dogId,
d.dogName,
c.id,
c.class,
t.trialStartDate,
s.points,
if(c.id = 5, '2012-08-01', '2012-11-18') as startDate,
if(c.id = 5, '2013-07-31', '2013-12-31') as endDate,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
GROUP BY p.id, ts.dogId, ts.classId
having t.trialStartDate between startDate and endDate
order by ts.classId, pointsAggregate desc
Looks like this fixed it, too much in the select and not in Group by?:
select
d.dogName,
c.class,
p.firstName,
p.lastName,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')
GROUP BY ts.dogId, ts.classId
order by ts.classId, pointsAggregate desc
Can you try below query and let if it's work or not ?
select
d.dogName,
c.class,
p.firstName,
p.lastName,
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')
GROUP BY ts.classId,p.firstName,p.lastName
order by ts.classId, pointsAggregate desc
I am trying to bring back a string based on an IF statement but it is extremely slow.
It has something to do with the first subquery but I am unsure of how to rearrange this as to bring back the same results but faster.
Here is my SQL:
SELECT IF
(
(
SELECT COUNT(*)
FROM
(
SELECT DISTINCT enquiryId, type
FROM parts_enquiries, parts_service_types AS pst
WHERE parts_enquiries.serviceTypeId = pst.id
) AS parts
WHERE parts.enquiryId = enquiries.id
) > 1, 'Mixed',
(
SELECT DISTINCT type
FROM parts_enquiries, parts_service_types AS pst
WHERE parts_enquiries.serviceTypeId = pst.id AND enquiryId = enquiries.id
)
) AS partTypes
FROM enquiries,
entities
WHERE enquiries.entityId = entities.id
How can I make it faster?
I have modified my original query below, but I am getting the error that subquery returns more than one row:
SELECT
(SELECT
CASE WHEN COUNT(DISTINCT type) > 1 THEN 'Mixed' ELSE `type` END AS type
FROM parts_enquiries
INNER JOIN parts_service_types AS pst ON parts_enquiries.serviceTypeId = pst.id
INNER JOIN enquiries ON parts_enquiries.enquiryId = enquiries.id
INNER JOIN entities ON enquiries.entityId = entities.id
GROUP BY enquiryId) AS partTypes
FROM enquiries,
entities
WHERE enquiries.entityId = entities.id
Please have a look if this query yields the same results:
SELECT
enquiryId,
CASE WHEN COUNT(DISTINCT type) > 1 THEN 'Mixed' ELSE `type` END AS type
FROM parts_enquiries
INNER JOIN parts_service_types AS pst ON parts_enquiries.serviceTypeId = pst.id
INNER JOIN enquiries ON parts_enquiries.enquiryId = enquiries.id
INNER JOIN entities ON enquiries.entityId = entities.id
GROUP BY enquiryId
But N.B.'s comment is still valid. To see if and index is used and other information we need to see the EXPLAIN and the table definitions.
This should get you what you want.
I would first pre-query your parts enquiries and parts service types looking for both the count and MINIMUM of the part 'type', grouped by the enquiry ID.
then, run your IF() against that result. If the distinct count is > 0, then 'Mixed'. If only one, since I did the MIN(), it would only have the description of that one value that you desire anyhow.
SELECT
E.ID
IF ( PreQuery.DistTypes > 1, 'Mixed', PreQuery.FirstType ) as PartType
from
Enquiries E
JOIN ( SELECT
PE.EnquiryID,
COUNT( DISTINCT PE.ServiceTypeID ) as DistTypes,
MIN( PST.Type ) as FirstType
from
Parts_Enquiries PE
JOIN Parts_Service_Types PST
ON PE.ServiceTypeID = PST.ID
group by
PE.EnquiryID ) as PreQuery
ON E.ID = PreQuery.EnquiryID
I am trying to group by a substring. I only get the one result:
Region = Coastal, Value (R) = 1144900.
I am supposed to get 2 results.
This is my code:
SELECT
DISTINCT SUBSTRING_INDEX(`team_name`, '-', 1) AS 'Region',
SUM(`opportunities`.`value`) AS 'Value (R)'
FROM
`opportunities`
LEFT JOIN cf_type_link_data ON cf_type_link_data.sourceid = opportunities.id
LEFT JOIN cf_lu_type_fields ON cf_type_link_data.fieldid = cf_lu_type_fields.id
LEFT JOIN cf_lu_types_fields_dropdown ON cf_type_link_data.`value` = cf_lu_types_fields_dropdown.id AND cf_lu_type_fields.id = cf_lu_types_fields_dropdown.fieldid
LEFT JOIN `lu_teams` ON `lu_teams`.`contactid` = `opportunities`.`user_allocation`
LEFT JOIN `teams` ON `teams`.`id` = `lu_teams`.`teamid`
LEFT JOIN `lu_opportunity_status` ON `lu_opportunity_status`.`id` = `opportunities`.`status`
WHERE 1
AND `cf_lu_types_fields_dropdown`.`values` = 'Building Project'
AND cf_lu_type_fields.fieldname = 'Scaffolding Segment'
AND (`opportunities`.`expecteddate` >= '2012-01-01' AND `opportunities`.`expecteddate` <= '2012-07-24')
GROUP BY 'Region'
ORDER BY cf_lu_types_fields_dropdown.`values`;
Any help is appreciated.
You can not use same alias from select in GROUP BY clause
try
SELECT DISTINCT SUBSTRING_INDEX(team_name, '-', 1) AS 'Region',
SUM(opportunities.value) AS 'Value (R)'....
GROUP BY SUBSTRING_INDEX(team_name, '-', 1)
ORDER BY cf_lu_types_fields_dropdown.values;
Try not to use quotes for field identifiers -
'Region' -> Region
In your case you see only one record in result data set because you group by string 'Region', but you should group by value(s).