MySQL_Retrieving stock price based on max and min_date - mysql

I am trying to retrieve closed and opened stock prices from the first and last date per month.
For some reason, the output of the 'end_date_closed_stock_price' is NULL.
Do you know any idea why it is giving this result?
Also, could you tell me the appropriate queries to retrieve the last date of the month?
The followings are my queries and output.
Thanks in advance!
SELECT YEAR(date) AS years
, MONTH(date) AS months
, CASE WHEN date = MAX(date) THEN close END end_date_closed_stock_price
, CASE WHEN date = MIN(date) THEN open END first_date_opened_stock_price
FROM nasdaq_feb_25_1971_feb_25_2021
GROUP
BY 1,2
ORDER
BY 1 DESC;
---OUTPUT---
2020 5 NULL 9382.349609
2019 1 NULL 6947.459961
2019 2 NULL 7266.279785
2019 3 NULL 7582.290039

There is probably a more efficient solution, but this should work:
WITH dates as
(SELECT YEAR(date) as years
,MONTH(date) AS months
,MAX(date) as end_date
,MIN(date) as first_date
FROM nasdaq_feb_25_1971_feb_25_2021
GROUP BY 1, 2)
SELECT dates.years
, dates.months
, price1.close as end_date_closed_stock_price
, price2.open as first_date_opened_stock_price
FROM dates
JOIN nasdaq_feb_25_1971_feb_25_2021 price1
ON price1.date = dates.end_date
JOIN nasdaq_feb_25_1971_feb_25_2021 price2
ON price2.date = dates.first_date
ORDER
BY 1 DESC, 2 DESC;

Related

MySQL ORDER BY FIELD for months

I have a table called months - this contains all 12 months of the calendar, the IDs correspond to the month number.
I will be running a query to retrieve 2 or 3 sequential months from this table, e.g
April & May
June, July, August
December & January
However I want to ensure that whenever December are January and retrieved, that it retrieves them in that order, and not January - December. Here is what I have tried:
SELECT * FROM `months`
WHERE start_date BETWEEN <date1> AND <date2>
ORDER BY
FIELD(id, 12, 1)
This works for December & January, but now when I try to retrieve January & February it does those in the wrong order, i.e "February - January" - I'm guessing because we specified 1 in the ORDER BY as the last value.
Anybody know the correct way to achieve this? As I mentioned this should also work for 3 months, so for example "November, December, January" and "December, January, February" should all be retrieved in that order.
If you want December first, but the other months in order, then:
order by (id = 12) desc, id
MySQL treats booleans as numbers, with "1" for true and "0" for false. The desc puts the 12s first.
EDIT:
To handle the more general case, you can use window functions. Assuming the numbers are consecutive, then the issue is trickier. This will work for 2 and 3-month spans:
order by (case min(id) over () > 1 then id end),
(case when id > 6 1 else 2 end),
id
I'm reluctant to think about a more general solution based only on months. After all, you can just use:
order by start_date
Or, if you have an aggregation query:
order by min(start_date)
to solve the real problem.
This is not "mysql solution" properly :
with cte (id, month) AS (
select id, month from months
union all
select id, month from months
)
, cte1 (id, month, r) as (select id, month, row_number() over() as r from cte )
select * from cte1
where id in (12, 1)
and r >= 12 order by r limit 2 ;
DECLARE
#monthfrom int = 12,
#monthto int = 1;
with months as (select 1 m
union all
select m+1 from months where m<12)
select m
from months
where m in (#monthfrom,#monthto)
order by
case when #monthfrom>#monthto
then
m%12
else
m
end
result:
12
1
Basically in MySQL this can be done the same way:
set #from =12;
set #to =1;
with recursive months(m) as (
select 1 m
union all
select m+1 from months where m<12)
select *
from months
where m in (#from,#to)
order by case when #from>#to then m%12 else m end;

Trouble with SELECT CASE

I want to get some data from my database for the past 31 days. When column 'finalized' is NULL I want to get data from column 'today', but if column 'finalized' isn't NULL I want to get data from column 'finalized'. I always want to get the last entered row.
TABLE MyEarnings
id INT(11) AI
date datetime
today decimal(4,2) NULL
finalized decimal(4,2) NULL
id date today finalized
-----------------------------------------------
6 2016-02-04 04:52:00 0.39 NULL
5 2016-02-03 12:34:00 NULL 19.74
4 2016-02-03 12:33:00 15.96 NULL
3 2016-02-03 12:32:00 12.32 NULL
2 2016-02-02 15:16:00 NULL 9.16
1 2016-02-02 14:29:00 2.20 NULL
SQL
SELECT
date,
CASE
WHEN finalized=NULL
THEN today
WHEN finalized!=NULL
THEN finalized
END
AS earn
FROM MyEarnings
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')
ORDER BY date ASC
LIMIT 0 , 31
This is what I end up with
date earn
---------------------------
2016-02-02 00:00:00 NULL
2016-02-03 00:00:00 NULL
2016-02-04 00:00:00 NULL
What I'm looking to get
date earn
----------------------------
2016-02-02 00:00:00 9.16
2016-02-03 00:00:00 19.74
2016-02-04 00:00:00 0.39
EDIT
I also want to get a summary for each month of all values in 'finalized'-column with the max 'id' for each day.
You can't compare null with !=, you should use is null and is not null like this:
SELECT
date,
CASE
WHEN finalized is null THEN today
ELSE finalized
END AS earn
FROM MyEarnings
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')
ORDER BY date ASC LIMIT 0 , 31
Also, if your first condition is when its null, you don't need to check if its not null , ELSE will be enough since the opposite of null is not null
use CASE WHEN like this:
CASE
WHEN finalized IS NULL
THEN today
ELSE finalized
END
AS earn
Replace query:
SELECT date, (CASE
WHEN finalized IS NULL THEN today
ELSE finalized
END;
) AS earn
FROM MyEarnings
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')
ORDER BY date ASC LIMIT 0,31
You need conditional aggregation, but it is a bit tricky. I think this does what you want:
SELECT DATE_FORMAT(date, '%Y-%m-%d'),
COALESCE(MAX(finalized), MAX(today)) as earn
FROM MyEarnings
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')
ORDER BY date ASC
LIMIT 0 , 31;
This returns the maximum value of today. You may want the most recent value. If so, the simplest method is probably the GROUP_CONCAT()/SUBSTRING_INDEX() method:
SELECT DATE_FORMAT(date, '%Y-%m-%d'),
COALESCE(MAX(finalized),
SUBSTRING_INDEX(GROUP_CONCAT(today ORDER BY date DESC), ',', 1) + 0
) as earn
FROM MyEarnings
GROUP BY DATE_FORMAT(date, '%Y-%m-%d')
ORDER BY date ASC
LIMIT 0 , 31;
It is a bit yucky to convert numbers to strings and back for this purpose. Alternative methods require additional joins or using variables.
Using greatest id to determine last row added (cons: stretching traditional use of id):
SELECT DATE(me.date) date,
COALESCE(me.finalized,me.today) earn
FROM MyEarnings me
JOIN (
SELECT MAX(id) max_id
FROM MyEarnings
GROUP BY DATE(date)
ORDER BY DATE(date)
LIMIT 31
) o
ON o.max_id = me.id
Using greatest date to determine last row added (cons: possible dupes):
SELECT DATE(me.date) date,
COALESCE(me.finalized,me.today) earn
FROM MyEarnings me
JOIN (
SELECT MAX(date) max_date
FROM MyEarnings
GROUP BY DATE(date)
ORDER BY DATE(date)
LIMIT 31
) o
ON o.max_date = me.date

Query with sum case group by

I am facing problem with following query:
SELECT sum(CASE WHEN status.new_reg_yn='n'
AND month(status.visit_date)-1 = 8
AND year(status.visit_date) = 2015 THEN 1 ELSE 0 END)
FROM customer_visit_status_tbl status,
customer_details_tbl cust
WHERE status.customer_id = cust.customer_id
AND cust.client_id=65
GROUP BY status.customer_id
The problem is that this query is returning results for customer with same id though I used group by. For example, in the month of September, if same customer visits 5 times it is returning count as 5 instead of 1 though I used group by.
It is really unclear what you want... Yes, distinct customers for a given time period, but then you are taking the month of the date visited -1 and looking for that equal to 8. Being that current month is 9 (September), Are you just looking for those based on activity the month prior to whatever the current is? So, for example, if Sept, 2015, you want totals for Aug, 2015. In Jan, 2016, you would want Dec, 2015? If that is the case, you can use the current date to subtract 1 month and get that as basis of the query. Then you can have your additional specific client (65 in this case).
My (subselect sqlvars) pre-creates variables applied for the query. It computes one month ago by subtracting 1 month from whatever the current date it. Then uses that as basis of the month representing whatever was the prior month, and similarly for that respective year.
Since this will in essence create a single row return, there is no Cartesian result and you can just run with your original other tables for final counts.
select
count( distinct s.customer_id ) as UniqueCustomers
from
( select #oneMonthAgo := DATE_ADD(CURRENT_DATE, INTERVAL -1 MONTH),
#finalMonth := MONTH( #oneMonthAgo ),
#finalYear := YEAR( #oneMonthAgo ) sqlvars,
customer_visit_status_tbl s
JOIN customer_details_tbl c
on s.customer_id = c.customer_id
AND c.client_id = 65
where
s.new_reg_yn='n'
Update Ans -
Select count(*)
from
( SELECT distinct status.customer_id
FROM customer_visit_status_tbl status
, customer_details_tbl cust
WHERE status.customer_id = cust.customer_id
AND cust.client_id = 65
and status.new_reg_yn = 'n'
AND month(status.visit_date)-1 = 8
AND year(status.visit_date) = 2015
) customer_visited

Include NULL Results as 0 in MySQL

My original query is as follows:
SELECT COUNT(*) AS count, YEAR(created_at) AS "year", MONTH(created_at) AS "month"
FROM quotes WHERE
(YEAR(created_at) = YEAR(CURDATE()) AND MONTH(created_at) = MONTH(CURDATE())
OR YEAR(created_at) = YEAR(CURDATE()) AND MONTH(created_at) = MONTH(CURDATE()) -1)
AND status_id = 1
GROUP BY YEAR(created_at), MONTH(created_at) DESC
This query basically retrieves the COUNT for this month and the previous month and works fine except when there are no results for either month.
I have two similar queries that do the same except for weeks and years.
I've tried to use COALESCE and IFNULL but it doesn't seem to include NULL results.
SELECT IFNULL(COUNT(*), 0) AS count, YEAR(created_at) AS "year", MONTH(created_at) AS "month"
FROM quotes
WHERE
(YEAR(created_at) = YEAR(CURDATE()) AND MONTH(created_at) = MONTH(CURDATE()) OR YEAR(created_at) = YEAR(CURDATE()) AND MONTH(created_at) = MONTH(CURDATE()) -1)
AND status_id = 1
GROUP BY YEAR(created_at), MONTH(created_at) DESC
Actual Result
count | year | month
-----------------------
1 | 2014 | 11
Expected Result
count | year | month
-----------------------
1 | 2014 | 11
0 | 2014 | 10
SQL Fiddle
You need to use a seperate data-set for the months you want to see. Your SQLfiddle simply is grouping the rows on Quotes, and as such shows no aggregate rows for months that don't show up in the result set at all.
Try Creating a temporary table with the months you want, and then do a simple OUTER JOIN to select any quotes that appear.
Just moving the IFNULL inside the SUM function, like a comment suggested, won't be enough.
You can only group by values that are available. If there is no entry for 2014-10 you won't get anything printed.
Otherwise you would see every possible combination.
Note: The where clause only limits the results, it has no effect on what to show. The Select doens't "know" that it has to show the month 10. becuas ethere is no value there.
I don't know what do you want to achieve, but personally in such a case I would just print 0 in the program when there is no matching result at all.
At its simplest...
SELECT x.month
, COUNT(q.id) total
FROM (SELECT 10 month UNION SELECT 11) x
LEFT
JOIN quotes q
ON MONTH(created_at) = month
GROUP
BY month;

Query returns null instead of 0 [duplicate]

This question already has answers here:
How to generate data in MySQL?
(4 answers)
Closed 8 years ago.
I have a query that counts all Work ID Numbers by month and year and then creates a chart for the past 13 months using jpgraph. It works great except there are no Work ID Numbers in July so the chart totally skips July.
Query Results:
5
16
15
11
3
12
4
8
2
9
13
12
Desired Results:
5
16
15
11
3
12
0
4
8
2
9
13
12
As you can see I need the (0) zero in order for my chart to work, however since there are no Work ID Number in July my query simply skips it. Here is my query:
SELECT COUNT( WORK_ID_NUM ) AS count,
DATE FROM SERVICE_JOBS
WHERE (DATE BETWEEN '$lastyear' AND '$date')
AND JOB_TYPE LIKE 'Street Light'
GROUP BY YEAR( DATE ), MONTH( DATE )
ORDER BY DATE
sqlFiddle Demo
SELECT IFNULL(count,0) as count,theDate as Date
FROM
(SELECT #month := #month+INTERVAL 1 MONTH as theDate
FROM service_jobs,(SELECT #month:='$lastyear' - INTERVAL 1 MONTH)as T1
LIMIT 13)as T2
LEFT JOIN
(SELECT COUNT(WORK_ID_NUM)as count,DATE
FROM service_jobs
WHERE (DATE BETWEEN '$lastyear' AND '$date')
AND JOB_TYPE LIKE 'Street Light'
GROUP BY YEAR(DATE), MONTH(DATE)) T3
ON (YEAR(theDate) = YEAR(DATE) AND MONTH(theDate) = MONTH(DATE))
ORDER BY theDate;
To get your query to return a row for July, you need to have a row with July in it. You could create a table with all the dates between $lastyear and $date in it and then outer join from that to SERVICE_JOB.
SELECT COUNT( WORK_ID_NUM ) AS count,
allDates.DATE
FROM AllDates
Left outer join SERVICE_JOB
on AllDates.DATE = SERVICE_JOB.DATE
WHERE (AllDates.DATE BETWEEN '$lastyear' AND '$date') AND
(SERVICE_JOB.WORK_ID_NUM is NULL OR JOB_TYPE LIKE 'Street Light')
GROUP BY YEAR( AllDates.DATE ), MONTH( AllDates.DATE )
ORDER BY AllDates.DATE
In SQL Server it would be pretty easy to make a Common Table Expression that could fill AllDates for you based on $lastyear and $date. Not sure about MySql.