Am trying to return the sum of each day of a week in mysql but it returns nothing despite having values for the 3rd Week of March 2010
SELECT SUM(expense_details_amount) AS total
FROM expense_details
WHERE YEAR(expense_details_date) = '2010'
AND MONTH(expense_details_date) = '03'
AND WEEK(expense_details_date) = '3'
GROUP BY DAY(expense_details_date)
How do I go about this?
According to the MySQL docs for WEEK:
This function returns the week number for date. The two-argument form of WEEK() allows you to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If the mode argument is omitted, the value of the default_week_format system variable is used. See Section 5.1.4, “Server System Variables”.
Their examples are:
mysql> SELECT WEEK('2008-02-20');
-> 7
mysql> SELECT WEEK('2008-02-20',0);
-> 7
mysql> SELECT WEEK('2008-02-20',1);
-> 8
mysql> SELECT WEEK('2008-12-31',1);
-> 53
So you should not be checking for week 3, but whatever week of the year the 3rd week in march is.
It also looks like these functions return integers instead of strings, so you might want to lose the single-quotes in your query.
Your where clause excludes all of the data, as the third week of the year is in January and you have also specified the month to be March. Try using a where clause of the form:
WHERE expense_details_date BETWEEN '2010-03-15' AND '2010-03-22'
WEEK() actually works like WEEKOFYEAR(), not WEEKOFMONTH(), so it gives values 0 through 53 as the result, by default.
If you're saying that the first week of the month is the first 7 days, not starting at the first Sunday, then you can use DAYOFMONTH(), and BETWEEN to get your third week:
SELECT SUM(expense_details_amount) AS total
FROM expense_details
WHERE YEAR(expense_details_date) = 2010
AND MONTH(expense_details_date) = 3
AND DAYOFMONTH(expense_details_date) BETWEEN 15 AND 21
GROUP BY DAYOFMONTH(expense_details_date)
Notice that I also changed the constants to numerals instead of Strings so MySQL doesn't have to do the conversion.
Related
So I want to make a query to show me if a certain calendar week has all 7 Day.
It would be okay if it just returns the numbers 1-7.
The table that I have contains articles of the 3 month of 2020 but even so the first week just contains Wednesday to Sunday it still counts it as a calendar week.
With that select I would make pl/sql Script to check it and if yes something happens.
This is an example of the Table:
Date Articel_Id
14.10.2020 78
15.10.2020 80
16.10.2020 96
17.10.2020 100
18.10.2020 99
Can I Use to_char() to check if Calendar Week has all 7 Days ?
If yes, how ?
The challenging is actually defining the weeks. If you want to define them using the ISO standard, then aggregate:
select to_char(date, 'IYYYY-IW') as yyyyww,
count(distinct trunc(date)) as num_days
from t
group by to_char(date, 'IYYYY-IW')
order by yyyyww;
This counts the number of days per week. I'm not sure if you want to filter, have a flag, or what the result set should look like. For filtering, using a having clause, such as having count(distinct trunc(date)) = 7.
I am trying to generate a string array of weekdays and use it find how many times each day appears in a month
I am using standard sql on BigQuery
My query would look like
with weeks as (select array['SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY'] as wk)
select DATE_DIFF('2019-01-31','2019-01-01',WEEK(wk)) AS week_weekday_diff
from weeks, unnest(wk) as wk
The query however fails with the error A valid date part argument for WEEK is required, but found wk. wk is a column value having the Days of Week, WEEK is a Functions which expects a literal DAYOFWEEK. Is there a way i pass the column value as arguments
Below is for BigQuery Standard SQL
error "A valid date part argument for WEEK is required, but found wk"
WEEK(<WEEKDAY>): Valid values for WEEKDAY are literal SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
... Is there a way i pass the column value as arguments?
If you wish - you can submit feature request at https://issuetracker.google.com/issues/new?component=187149&template=0
find how many times each day appears in a month
To get your expected result and overcome above "issue" you can approach task from opposite angle - just extract weekdays positions and then do needed stats as in example below
#standardSQL
WITH weekdays AS (SELECT ['SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY'] AS wk)
SELECT wk[ORDINAL(pos)] weekday, COUNT(1) cnt
FROM weekdays,
UNNEST(GENERATE_DATE_ARRAY('2019-01-01','2019-01-31')) day,
UNNEST([EXTRACT(DAYOFWEEK FROM day)]) pos
GROUP BY pos, weekday
ORDER BY pos
with result
Row weekday cnt
1 SUNDAY 4
2 MONDAY 4
3 TUESDAY 5
4 WEDNESDAY 5
5 THURSDAY 5
6 FRIDAY 4
7 SATURDAY 4
Trying your query, what I have noticed to be returning an error is:
select DATE_DIFF('2019-01-31','2019-01-01',WEEK('WEDNESDAY')) AS week_weekday_diff;
as the function WEEK(< WEEKDAY >) is expecting something like:
select DATE_DIFF('2019-01-31','2019-01-01',WEEK(`WEDNESDAY`)) AS week_weekday_diff;
OR
select DATE_DIFF('2019-01-31','2019-01-01',WEEK(WEDNESDAY)) AS week_weekday_diff;
I think that the WEEK(< WEEKDAY >) only accepts the weekdays in the format exposed here, so no strings should be valid.
I have a date field in Hive 2018-06-10, from which i need to get WeekOfMonth
WEEKOFYEAR(order_time)
I need output for 2018-06-10 as 3 (which is 3rd week. assuming week starts from Sunday)
Is there any built in function in Hive to retrieve WeekofMonth. I couldn't find any. I tried below to convert based on minutes and seconds but
from_unixtime(unix_timestamp(CURRENT_DATE())+7200)
But the above is not giving correct value
For the week of the month, you can get the day part of the month and divide by 7.
select case
when DAYOFMONTH(order_time)%7 = 0
then DAYOFMONTH(order_time)/7
else DAYOFMONTH(order_time)/7 + 1
end
Also you can use date_format function:
select date_format('2018-06-10','W');
See more format patterns here: SimpleDateFormat
SQL server DATEPART function has two options to retrieve week number;
ISO_WEEK and WEEK. I Know the difference between the two, I want to have week numbers based on Sunday start standard as followed in the US; i.e. WEEK. But it doesn't handles partial weeks the way I expected. e.g.
SELECT DATEPART(WEEK,'2015-12-31') --53
SELECT DATEPART(WEEK,'2016-01-01') --1
SELECT DATEPART(WEEK,'2016-01-03') --2
gives two different week numbers for a single week, divided in two years. I wanted to implement something like in the following link for week days.
Week numbers according to US standard
Basically I would like something like this;
SELECT DATEPART(WEEK,'2015-12-31') --1
SELECT DATEPART(WEEK,'2016-01-01') --1
SELECT DATEPART(WEEK,'2016-01-03') --2
EDIT:
Basically I am not good with the division of a single week into two, I have to perform some calculations based on week numbers and the fact that a single week to be divided isn't acceptable. So if above isn't possible.
Is it possible that the week number one would start from 2016-01-03. i.e. what I would in that case would be something like this:
SELECT DATEPART(WEEK,'2015-12-31') --53
SELECT DATEPART(WEEK,'2016-01-01') --53
SELECT DATEPART(WEEK,'2016-01-03') --1
If you want the US numbering, you can do this by taking the WEEK number of the end of the week rather than the date itself.
First ensure that the setting for first day of the week is in fact Sunday on your system. You can verify this by running SELECT ##DATEFIRST; this should return 7 for Sunday. If it doesn't, run SET DATEFIRST 7; first.
SELECT
end_of_week=DATEADD(DAY, 7-(DATEPART(WEEKDAY, '20151231')), '20151231'),
week_day=DATEPART(WEEK, DATEADD(DAY, 7-(DATEPART(WEEKDAY, '20151231')), '20151231'));
Which will return 2016/01/02 - 1.
If you wish generate week number of a date, it will return the week number of the year(input date)
Thus, I think sql server treat '2015-12-31' as the last week of 2015.
I want to check a set of values of dates if it falls among 1st or 3rd or 5th Monday of any month. How to do it in SQL Server 2008?
Okay, that can easily be expressed by a couple of conditions:
WHERE
DATEPART(weekday,DateToCheck) = DATEPART(weekday,'20120910') AND
(
DATEPART(day,DateToCheck) between 1 and 7 OR
DATEPART(day,DateToCheck) between 15 and 21 OR
DATEPART(day,DateToCheck) between 29 and 31
)
(I do the DATEPART(weekday,... check as above so that I don't have to know what your date settings are on the server - I'm just checking the value against a "known good" Monday)
try this:
SELECT *
FROM <your_table>
WHERE datename(weekday,<date_col>)='Monday'
AND DATEPART(day,<date_col>)/7 in (0,2,4)