I have
select created_at, sales_flat_order.customer_id,
customer_firstname, customer_lastname, customer_email,
sales_flat_order_address.company,
SUM(grand_total)
from sales_flat_order
left join sales_flat_order_address
on sales_flat_order.entity_id = sales_flat_order_address.entity_id
where created_at >= '2016-06-01 05:00:00' /* 5h difference */
and store_id = 1
and `status` = 'Complete'/*
left join customer_entity
on sales_flat_order.customer_id = customer_entity.entity_id*/
group by customer_email;
Runs fine. Comment content produces error with SQL syntax, code 1064. What's the problem?
Same issue now with this:
select customer.entity_id, customer.group_id,
customer.created_at,
sfo.customer_firstname, sfo.customer_lastname,
sfo.customer_email,
sfo.created_at last_order,
count(sfo.customer_id) num_orders,
group_concat(sfo.grand_total separator '|') grand_totals
from customer_entity customer
inner join sales_flat_order sfo
on customer.entity_id = sfo.customer_id
where sfo.created_at >= '2016-06-01 05:00:00'
and sfo.store_id = 1
and sfo.`status` = 'Complete'
left outer join customer_group
on customer.group_id = customer.customer_group_id
where customer_group.customer_group_id
group by customer.entity_id;
Thanks everyone, that all makes ample sense, definitely getting into the hang of it; how's this? It works but tear it up if anything remains amuck.
select customer.entity_id, customer.group_id, `group`.customer_group_code,
customer.created_at,
`order`.customer_firstname, `order`.customer_lastname,
`order`.customer_email, address.company,
`order`.created_at last_order,
count(`order`.customer_id) num_orders,
group_concat(`order`.grand_total separator '|') grand_totals
from customer_entity customer
inner join sales_flat_order `order`
on customer.entity_id = `order`.customer_id
left outer join customer_group `group`
on customer.group_id = `group`.customer_group_id
left outer join sales_flat_order_address address
on `order`.entity_id = address.entity_id
where `order`.created_at >= '2016-06-01 05:00:00'
and `order`.store_id = 1
and `order`.`status` = 'Complete'
group by customer.entity_id;
I know everyone said to add to the group by clause but there is nothing else I want to group by...?
The where clause must be after all of the joins. There are other issues as well, but that's the issue that really breaks things.
If it were me, I'd also fix the group by clause to match the select list, but MySql lets you write non-standard SQL in this way. You may also have a naming conflict when you add the additional table because you haven't qualified the column names. Aliases would make this easier to do Finally, it's possible for your query logic that you want some of the conditional expressions in the where clause to be part of the first ON clause.
If you format your code properly, it's easy to see why your code doesn't work. Your commented LEFT JOIN that caused the error is after the WHERE clause. Also write proper SQL. Alias your table name because long column names are annoying. Put all non-aggregated columns in the GROUP BY clause. Your code won't even run in any other dbms except for maybe MySQL.
select created_at, sales_flat_order.customer_id
, customer_firstname, customer_lastname
, customer_email
, sales_flat_order_address.company
, SUM(grand_total)
from sales_flat_order
left join sales_flat_order_address on sales_flat_order.entity_id = sales_flat_order_address.entity_id
where created_at >= '2016-06-01 05:00:00' /* 5h difference */
and store_id = 1
and `status` = 'Complete'/*
left join customer_entity on sales_flat_order.customer_id = customer_entity.entity_id*/
group by customer_email;
Related
I am trying to get a left join to work and have an error that says
Unknown column 'sales_order_grid.entity_id' in 'on clause
However I don't know how to re-word the query the column sales_order_grid does contain a column named entity_id? I am trying to run a query that shows results even if the sales_order_tax table has no matching row I want the rest of the data from the other tables to show.
SELECT sales_order_grid.entity_id, sales_order_grid.created_at, sales_order_grid.increment_id AS OrderID, sales_order_grid.status, sales_order_grid.payment_method AS payment_method, sales_order_grid.base_grand_total AS pmt, sales_order_grid.base_grand_total AS charged, t.code AS tax_code, t.title AS taxrate, t.amount AS taxamount,
sales_order_grid.shipping_and_handling AS shipping_invoice, sales_order_grid.grand_total AS total_invoice, customer_address_entity.firstname, customer_address_entity.lastname,
customer_address_entity.city, customer_address_entity.region, customer_address_entity.postcode
FROM sales_order_grid, customer_address_entity LEFT OUTER JOIN sales_order_tax AS t on sales_order_grid.entity_id = t.order_id
WHERE sales_order_grid.created_at >= '2018-02-01 00:00:00'
AND sales_order_grid.created_at <= '2018-02-05 23:59:59'
AND sales_order_grid.status IN('Paid','complete','closed','Processing','on hold')
AND sales_order_grid.customer_id = customer_address_entity.parent_id
GROUP BY sales_order_grid.entity_id
You are mixing up 2 different ways in joining your tables.
Your query should look like this:
SELECT sales_order_grid.entity_id, sales_order_grid.created_at, sales_order_grid.increment_id AS OrderID, sales_order_grid.status, sales_order_grid.payment_method AS payment_method, sales_order_grid.base_grand_total AS pmt, sales_order_grid.base_grand_total AS charged, t.code AS tax_code, t.title AS taxrate, t.amount AS taxamount,
sales_order_grid.shipping_and_handling AS shipping_invoice, sales_order_grid.grand_total AS total_invoice, customer_address_entity.firstname, customer_address_entity.lastname,
customer_address_entity.city, customer_address_entity.region, customer_address_entity.postcode
FROM sales_order_grid
JOIN customer_address_entity on sales_order_grid.customer_id = customer_address_entity.parent_id
LEFT OUTER JOIN sales_order_tax AS t on sales_order_grid.entity_id = t.order_id
WHERE sales_order_grid.created_at >= '2018-02-01 00:00:00' AND sales_order_grid.created_at <= '2018-02-05 23:59:59'
AND sales_order_grid.status IN('Paid','complete','closed','Processing','on hold') GROUP BY sales_order_grid.entity_id
SELECT a.emp_id,s.name, s.department,s.register, z.Compoff_Count as Extra, ifnull(COUNT(DISTINCT TO_DAYS(a.punchdate)),0) as Monthly_Count
FROM machinedata a left join
(SELECT a.emp_id, ifnull(COUNT(DISTINCT TO_DAYS(a.punchdate)),0) as Compoff_Count
FROM machinedata a
RIght JOIN time_dimension c on c.db_date = a.punchdate
where ( year(c.db_date) = 2016 and month(c.db_date) = 8 and (c.holiday_flag = 't' or c.weekend_flag ='t' ))
GROUP BY a.emp_id) Z
on z.emp_id = a.emp_id
RIght JOIN time_dimension c on c.db_date = a.punchdate
left join emp s on s.emp_id = a.emp_id
where (year(c.db_date) = 2016 and month(c.db_date) = 8 and c.holiday_flag = 'f' and c.weekend_flag ='f' )
GROUP BY emp_id
The above query works fine.. but if i add s.department='yes' in the last where the query takes more than 40 seconds.
What shall i do to improve the query performance ?
Your initial query can be simplified I believe by using "conditional aggregates" which places case expressions inside the count() function. This avoids repeated sans of data and unnecessary joins to derived tables.
You should also avoid using functions on data to suit where clause conditions i.e. Instead of YEAR() and MONTH() simply use date boundaries. This allows an index on the date column to be used in the query execution.
I'm not sure if you really need to use TO_DAYS() but I suspect it isn't needed either.
SELECT
a.emp_id
, s.name
, s.department
, s.register
, COUNT(DISTINCT CASE WHEN (c.holiday_flag = 't' OR
c.weekend_flag = 't') THEN c.db_date END) AS Compoff_Count
, COUNT(DISTINCT CASE WHEN NOT (c.holiday_flag = 't' OR
c.weekend_flag = 't') THEN a.punchdate END) AS Monthly_Count
FROM time_dimension c
LEFT JOIN machinedata a ON c.db_date = a.punchdate
LEFT JOIN emp s ON a.emp_id = s.emp_id
WHERE c.db_date >= '2016-08-01'
AND c.db_date < '2016-09-01'
GROUP BY
a.emp_id
, s.name
, s.department
, s.register
If this re-write produces correct results then you could try adding and s.department='yes' into the where clause to assess the impact. If it is still substantially slower then get an explain plan and add it to the question. The most likley cause of slowness is lack of an index but without an explain plan it's not possible to be certain.
Please note that this suggestion is just that; and is prepared without sample data and expected results.
This is my Query I want to get latest record in each group.I have two table t_service_request and t_request_chkpoint
t_service_request
------------
LTS,JFT,CUS_NO,REQUETST_ID...
t_request_chkpoint
------------
LTS ,REQUETST_ID...
Both table match by REQUETST_ID.
I want to group by cus_no in table t_service_request
SELECT S.*, A.ID as CID, A.ENTRY_ID, A.LTS
FROM maintenance.t_service_request S
WHERE JFT IN (
SELECT MAX(JFT)
FROM maintenance.t_service_request
GROUP BY CUS_NO
) LEFT OUTER JOIN maintenance.t_request_chkpoint A
ON S.REQUEST_ID = A.REQUEST_ID where S.COMPANY_ID = '0002' AND S.STATE >= 3 AND A.STATE >= 3
but didn't work any suggestions ?
t_service_request
------------
LTS|JFT|CUS_NO|REQUETST_ID|
t_request_chkpoint
------------
|LTS|REQUETST_ID|
Join above two table(Request_id) and select latest JFT in each CUS_NO
Try this, maybe works;)
SELECT DISTINCT
S.*,
A.ID AS CID,
A.ENTRY_ID,
A.LTS
FROM maintenance.t_service_request S
LEFT JOIN maintenance.t_request_chkpoint A ON A.REQUETST_ID = S.REQUETST_ID AND A.STATE >= 3
WHERE S.JFT = (SELECT MAX(B.JFT)
FROM maintenance.t_service_request B
WHERE B.CUS_NO = S.CUS_NO
GROUP BY B.CUS_NO)
AND S.COMPANY_ID = '0002' AND S.STATE >= 3
I think your sql may have some syntax errors and I am not sure I've misunderstood your requirement or not.
I must admit, I still don't understand what you are asking. Your query, however, is incomplete, and maybe fixing it solves your problem already.
You say you want "to get latest record in each group" and in your query you are looking for the maximum JFT per CUS_NO. Then, however you are only comparing the JFT and not the CUS_NO.
Moreover, your query is syntactically incorrect, as it has two WHERE clauses. Last but not least, (outer) join criteria (state >= 3 here) belongs in the ON clause, not in the WHERE clause.
Here is the corrected query:
select
sr.*,
rc.id as cid,
rc.entry_id,
rc.lts
from maintenance.t_service_request sr
left outer join maintenance.t_request_chkpoint rc on rc.request_id = sr.request_id
and rc.state >= 3
where sr.company_id = '0002' and sr.state >= 3
and (sr.cus_no, sr.jft) in
(
select cus_no, max(jft)
from maintenance.t_service_request
group by cus_no
);
I have the following sql which is not working on the where condition.
SELECT t.PROJID,sum(t.UNITS) AS totalunits, sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t`
left join projects on projects.PROJID = t.PROJID
where lastupdated LIKE '%2014-06-11%'
GROUP BY t.PROJID
You should not use like on dates. The right solution is to simply truncate the date and do the comparison. However, you need to use the field in the data:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
where date(t.date) = date('2014-06-11')
GROUP BY t.PROJID;
Or, if you have an index on lastupdated, using a range allows the use of the index:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
where t.date >= date('2014-06-11') and t.date < date('2014-06-12')
GROUP BY t.PROJID;
You might really be wanting to use having, which is suggested by the use of the column alias instead of the base column. In that case:
SELECT t.PROJID, sum(t.UNITS) AS totalunits,
sum(t.COST) AS totalcost, project, max(t.DATE) as lastupdated, PROJCODE
FROM `projectcosts` `t` left join
projects on projects.PROJID = t.PROJID
GROUP BY t.PROJID
HAVING date(lastupdated) = date('2014-06-11');
Like can never be used with date field.
If you are changing the type to vachar it is possible
I've created sqlfiddle to try and get my head around this http://sqlfiddle.com/#!2/21e72/1
In the query, I have put a max() on the compiled_date column but the recommendation column is still coming through incorrect - I'm assuming that a select statement will need to be inserted on line 3 somehow?
I've tried the examples provided by the commenters below but I think I just need to understand this from a basic query to begin with.
As others have pointed out, the issue is that some of the select columns are neither aggregated nor used in the group by clause. Most DBMSs won't allow this at all, but MySQL is a little relaxed on some of the standards...
So, you need to first find the max(compiled_date) for each case, then find the recommendation that goes with it.
select r.case_number, r.compiled_date, r.recommendation
from reporting r
join (
SELECT case_number, max(compiled_date) as lastDate
from reporting
group by case_number
) s on r.case_number=s.case_number
and r.compiled_date=s.lastDate
Thank you for providing sqlFiddle. But only reporting data is given. we highly appreciate if you give us sample data of whole tables.
Anyway, Could you try this?
SELECT
`case`.number,
staff.staff_name AS ``case` owner`,
client.client_name,
`case`.address,
x.mx_date,
report.recommendation
FROM
`case` INNER JOIN (
SELECT case_number, MAX(compiled_date) as mx_date
FROM report
GROUP BY case_number
) x ON x.case_number = `case`.number
INNER JOIN report ON x.case_number = report.case_number AND report.compiled_date = x.mx_date
INNER JOIN client ON `case`.client_number = client.client_number
INNER JOIN staff ON `case`.staff_number = staff.staff_number
WHERE
`case`.active = 1
AND staff.staff_name = 'bob'
ORDER BY
`case`.number ASC;
Check below query:
SELECT c.number, s.staff_name AS `case owner`, cl.client_name,
c.address, MAX(r.compiled_date), r.recommendation
FROM case c
INNER JOIN (SELECT r.case_number, r.compiled_date, r.recommendation
FROM report r ORDER BY r.case_number, r.compiled_date DESC
) r ON r.case_number = c.number
INNER JOIN client cl ON c.client_number = cl.client_number
INNER JOIN staff s ON c.staff_number = s.staff_number
WHERE c.active = 1 AND s.staff_name = 'bob'
GROUP BY c.number
ORDER BY c.number ASC
SELECT
case.number,
staff.staff_name AS `case owner`,
client.client_name,
case.address,
(select MAX(compiled_date)from report where case_number=case.number),
report.recommendation
FROM
case
INNER JOIN report ON report.case_number = case.number
INNER JOIN client ON case.client_number = client.client_number
INNER JOIN staff ON case.staff_number = staff.staff_number
WHERE
case.active = 1 AND
staff.staff_name = 'bob'
GROUP BY
case.number
ORDER BY
case.number ASC
try this