select
pd.state AS StateName,
pd.county AS `County Name`,
CASE pc.close_dt WHEN pc.close_dt >= DATE(NOW() - INTERVAL 3 MONTH)
THEN COUNT(pd.property_id)
ELSE NULL
END AS `3 MONTH`,
CASE pc.close_dt WHEN pc.close_dt >= DATE(NOW() - INTERVAL 6 MONTH)
THEN COUNT(pd.property_id)
ELSE NULL
END AS `6 MONTH`
from resnet.property_details pd
join resnet.property_closings pc
on pd.property_id = pc.Property_id
GROUP BY pd.state,pd.county
I'm trying to get the interval of 3 months from today, 6 months from today for properties that have been closed.
I want it to look like the this:
You need conditional aggregation. In MySQL, you can do this as:
select pd.state AS StateName, pd.county AS `County Name`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `3 MONTH`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 6 MONTH) AS `6 MONTH`
from resnet.property_details pd join
resnet.property_closings pc
on pd.property_id = pc.Property_id
group by pd.state, pd.county;
Notes:
Your case syntax just doesn't make sense. You either have conditions (as case when <condition> or you have constants as case <column> when <value>). But not both.
date(now()) = CURDATE().
You don't need case expressions because MySQL treats booleans as integers in a numeric context. You can just "sum them up" to count the number of true values.
EDIT:
If you want 3-6 months, then you would do:
SUM(pc.close_dt >= CURDATE() - INTERVAL 6 MONTH AND
pc.close_dt < CURDATE() - INTERVAL 3 MONTH) AS `6 MONTH`
Related
for this my query is =
SELECT SalesDate,COUNT(Shape) as pcs,
ROUND(SUM(TotalAmount),2) as amount,
ROUND(SUM(Carat),2) as carat,
ROUND(ROUND(SUM(TotalAmount),2)/ROUND(SUM(Carat),2),2) as avgprice
from `tbl_sales`
WHERE IF((SalesDate = CURDATE() - INTERVAL 1 DAY) = null, SalesDate=CURDATE() - INTERVAL 2 DAY,SalesDate= CURDATE() - INTERVAL 1 DAY)
so this is my response
so in If condition I want to make sure that if the data in yesterday data is null or 0 then it will take day before yesterday
Perhaps something like this:
SELECT SalesDate,COUNT(Shape) as pcs,
ROUND(SUM(TotalAmount),2) as amount,
ROUND(SUM(Carat),2) as carat,
ROUND(ROUND(SUM(TotalAmount),2)/ROUND(SUM(Carat),2),2) as avgprice
FROM `tbl_sales`
GROUP BY SalesDate
HAVING SalesDate = CASE WHEN (SalesDate = CURDATE() - INTERVAL 1 DAY)=0
THEN CURDATE() - INTERVAL 2 DAY
WHEN (SalesDate = CURDATE() - INTERVAL 1 DAY)=1
AND amount IS NULL
THEN CURDATE() - INTERVAL 2 DAY
ELSE CURDATE() - INTERVAL 1 DAY END;
When you do (SalesDate = CURDATE() - INTERVAL 1 DAY) it will return false=0 and true=1. Therefore doing (SalesDate = CURDATE() - INTERVAL 1 DAY) = NULL, although it should be .. IS NULL instead of .. = NULL.. either way, it won't work. Let's inspect the CASE expression in HAVING part.
If it return 0 means there's no matching with date specified, then take 2 days before:
CASE WHEN (SalesDate = CURDATE() - INTERVAL 1 DAY)=0
THEN CURDATE() - INTERVAL 2 DAY
If it has match for the date checking and return 1 BUT with NULL amount, then take 2 days before as well:
WHEN (SalesDate = CURDATE() - INTERVAL 1 DAY)=1
AND amount IS NULL
THEN CURDATE() - INTERVAL 2 DAY
Else take yesterday date:
ELSE CURDATE() - INTERVAL 1 DAY END;
Demo fiddle
You need to cast your column to date:
SELECT SalesDate,COUNT(Shape) as pcs,
ROUND(SUM(TotalAmount),2) as amount,
ROUND(SUM(Carat),2) as carat,
ROUND(ROUND(SUM(TotalAmount),2)/ROUND(SUM(Carat),2),2) as avgprice
from `tbl_sales`
WHERE IF((CAST(SalesDate AS date) = CURDATE() - INTERVAL 1 DAY), CAST(SalesDate AS date) = CURDATE() - INTERVAL 2 DAY, CAST(SalesDate AS date) = CURDATE() - INTERVAL 1 DAY)
I need to get all the Paid and Provisional Entry from the last 7 days but I keep getting everything returned. I am unsure what I am doing wrong I have read through quite a few posts on here and cant fathom what its. MySQL 5.6 if it makes any diffrence to what I have been doing.
SELECT
DATE_FORMAT(FROM_UNIXTIME(ct.entry_date),'%d/%m/%Y') AS booking_date,
cd.field_id_69 AS marriage_date,
cd.field_id_54 AS email_address,
CONCAT(cd.field_id_9, ' ', cd.field_id_10) AS bride_name,
CONCAT(cd.field_id_13, ' ', cd.field_id_14) AS groom_name,
ctco.title AS centre_and_course_date,
(SELECT DATE_FORMAT(FROM_UNIXTIME(col_id_1),'%d/%m/%Y') FROM
exp_channel_grid_field_50 cg WHERE cg.entry_id = ctco.entry_id ORDER BY
cg.row_id DESC LIMIT 1) AS course_end_date,
ct.status AS payment_status
FROM exp_channel_titles ct
JOIN exp_channel_data cd ON cd.entry_id = ct.entry_id
JOIN exp_relationships rco ON rco.parent_id = ct.entry_id AND rco.field_id = 41
JOIN exp_channel_titles ctco ON rco.child_id = ctco.entry_id
WHERE ct.channel_id = 2
AND ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND ct.status = 'Paid'
AND ct.status = 'Provisional';
There is problem in your WHERE statement
AND ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND ct.status = 'Paid' AND ct.status = 'Provisional';
The BETWEEN keyword needs to be followed by 2 dates, so ct.status = 'Paid' will be converted to DATE which return NULL. Hence your WHERE statement turns into.
AND ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND NULL AND ct.status = 'Provisional';
The result is that your query will return all data that have status = 'Provisional'.
You could try to modify the WHERE statement to
AND ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() AND ct.status IN ('Paid', 'Provisional');
The syntax of BETWEEN is BETWEEN startdate AND enddate. You wrote:
ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND ct.status = 'Paid'
So it's using DATE_SUB(CURDATE(), INTERVAL 7 DAY) as the starting date, and ct.status = 'Paid' as the ending date. I'm surprised it's matching anything.
Change it to:
ct.entry_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
Or if you don't have any dates in the future, you can simply write:
ct.entry_date >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
select pd.state AS StateName, pd.county AS `County Name`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `0-3 Months`,
SUM(`0-3 Months` - INTERVAL 3 MONTH) AS `3-6 Months`
from resnet.property_details pd join
resnet.property_closings pc
on pd.property_id = pc.Property_id
group by pd.state, pd.county;
How can I get the 3 month interval from the previous 3 month interval value? So it will be 3-6 months?
You can use BETWEEN..AND, e.g.:
select pd.state AS StateName, pd.county AS `County Name`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `0-3 Months`,
SUM(pc.close_dt BETWEEN CURDATE() - INTERVAL 3 MONTH AND CURDATE() - INTERVAL 6 MONTH) AS `3-6 Months`
from resnet.property_details pd join
resnet.property_closings pc
on pd.property_id = pc.Property_id
group by pd.state, pd.county;
First off, thank you in advance.
I have 2 queries that I am working with as I would like to compare last week to the previous week...
Getting Last Week
SELECT keyword,
SUM(users_desktop) AS desktop_last_week
FROM js
WHERE country='US'
AND day >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND day < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
GROUP BY keyword
ORDER BY desktop_last_week DESC
LIMIT 10;
Getting Previous Week
SELECT keyword,
SUM(users_desktop) AS desktop_previous_week
FROM js
WHERE country='US'
AND day >= curdate() - INTERVAL DAYOFWEEK(curdate())+12 DAY
AND day < curdate() - INTERVAL DAYOFWEEK(curdate())-7 DAY
GROUP BY keyword
ORDER BY desktop_previous_week DESC
LIMIT 10;
What I would like to do is combine these queries so that I can then ORDER BY the division of desktop_last_week/desktop_this_week to find keywords that are trending up (ie: searched a lot more this week than last week)
Any idea on how to combine these together?
This is called conditional aggregation. Use where clauses from your existing queries to do it.
SELECT keyword,
Sum(CASE
WHEN day >= Curdate() - INTERVAL Dayofweek(Curdate())+6 day
AND day < Curdate() - INTERVAL Dayofweek(Curdate())-1 day THEN
users_desktop
ELSE 0
end) AS desktop_last_week,
Sum(CASE
WHEN day >= Curdate() - INTERVAL Dayofweek(Curdate())+12 day
AND day < Curdate() - INTERVAL Dayofweek(Curdate())-7 day THEN
users_desktop
ELSE 0
end) AS desktop_prev_last_week
FROM js
WHERE country = 'US'
GROUP BY keyword
ORDER BY desktop_prev_last_week / desktop_this_week DESC
LIMIT 10
Use Union!
You can get 2 Queries to one with that. If you want to order that then put it into a subquery
You could expand the where clause to contain both time ranges and move the logic of differentiating between them to case expressions inside the sums. By the way, using the between operator instead of a >= and < pair would make the query itself a tad easier to read:
SELECT keyword,
SUM(CASE WHEN day BETWEEN
(CURDATE() - INTERVAL DAYOFWEEK(curdate()) + 6 DAY) AND
(CURDATE() - INTERVAL DAYOFWEEK(curdate()) - 1 DAY)
THEN users_desktop
END) AS desktop_last_week,
SUM(CASE WHEN day BETWEEN
(CURDATE() - INTERVAL DAYOFWEEK(curdate()) + 12 DAY) AND
(CURDATE() - INTERVAL DAYOFWEEK(curdate()) - 7 DAY)
THEN users_desktop
END) AS desktop_previous_week
FROM js
WHERE country = 'US'
GROUP BY keyword
ORDER BY desktop_last_week DESC, desktop_previous_week DESC
LIMIT 10;
SELECT COUNT(*) FROM `table` WHERE `datetime` > SUBDATE(NOW(), INTERVAL 1 DAY)
This will get number of entries during last day. But is it possible to get number of entries for multiple intervals without having to send variation of this query multiple times (INTERVAL 1 DAY, INTERVAL 1 WEEK, INTERVAL 1 MONTH, ...)?
You need CASE WHEN expression to accomplish that.
SELECT
COUNT(CASE WHEN DATE(`datetime`) >= CURDATE() - INTERVAL 1 DAY AND DATE(`datetime`) < CURDATE() THEN 1 END) AS lastDay,
COUNT(CASE WHEN DATE(`datetime`) >= CURDATE() - INTERVAL 7 DAY AND DATE(`datetime`) < CURDATE() THEN 1 END ) AS lastSevenDays,
COUNT(*) AS lastThirtyDays
FROM `table`
WHERE
DATE(`datetime`) >= CURDATE() - INTERVAL 30 DAY
How to use CASE WHEN expression
Note: If your requirement is to get result of last day, last 7 days and last 30 days then go with this query.
EDIT:
If you have an index on datetime field then the above query will fail to use that index. Please use the query given below in order to utilize the index on datetime.
SELECT
COUNT(CASE WHEN DATE(`datetime`) >= CURDATE() - INTERVAL 1 DAY AND DATE(`datetime`) < CURDATE() THEN 1 END) AS lastDay,
COUNT(CASE WHEN DATE(`datetime`) >= CURDATE() - INTERVAL 7 DAY AND DATE(`datetime`) < CURDATE() THEN 1 END ) AS lastSevenDays,
COUNT(*) AS lastThirtyDays
FROM `table`
WHERE
`datetime` >= (NOW() - INTERVAL 30 DAY - INTERVAL HOUR(NOW()) HOUR - INTERVAL MINUTE(NOW()) MINUTE - INTERVAL SECOND(NOW()) SECOND)