I am trying to create a group by date function. But i don't want to get results from 00:00 till 23:59, i want to get results from 6:00 till 5:59.
Can i change date from till statement?
Example
Select * from Sales group by Convert(date,dateOfSell)
//set statement from 6:00 till next day 5:59
First substract 6 hours from the datetime, and then cast it do a date. Then you can group on this new date.
e.g.
SELECT CAST(DATEADD(hour,-6,dateOfSell) AS DATE),MIN(dateOfSell),MAX(dateOfSell),COUNT(*)
FROM Sales
GROUP BY CAST(DATEADD(hour,-6,dateOfSell) AS DATE)
Related
I have a table in snowflake like below
StockName Date Value
GOOG 10/12/2021 $100
GOOG 10/11/2021 $995
Now i want to create a new column which would give me results that would show the value of the stock on weeks first day which would be monday or the next day if monday is a holiday
StockName date Value value_weekstart
GOOG 10/12/2021 $1000 $995
GOOG 10/11/2021 $995 $995
The following should work:
SELECT
stockname,
date,
value,
FIRST_VALUE(value) OVER (PARTITON BY stockname, datetrunc('week',date) ORDER BY dayofweekiso(date)) as value_weekstart
FROM table_name
ORDER BY 1,2;
that will return the "sunday price" if there is one, but will side step the possible jump if you use the stock dayofweek and someone changes the setting on your instance.
If you really want sunday to be last going to (dayofweekiso(date)+6)%7 will roll it around to last position.
I have a table with a set of business dates I need to select the max date per month and year tried using the last_day function but that returns the last day not the max date of that month.please help me out.
MAX is an aggregate function, so you need to figure out how to group all of the days of the month together. The easiest way to do that is apply a function that will return the same value for every day in that month. LAST_DAY would work, but I prefer TRUNC (with 'MM' specified).
SELECT MAX(your_column) FROM your_table GROUP BY TRUNC(your_column, 'MM')
i need to print the files From the current day plus from the day before...but if the day before is sunday then i want to print the files from saturday
i have a database containing a files of a month..each day produces some files but sunday is a holiday and at that day no files are generated.now i have done the query to pick the files in such a way that giving current date will results the files of that day and previous day. But the problem is on monday when i give the current date it produces files of monday and sunday... i dont want to print files of sunday as it is null,instead of that when am giving the current date if its is monday i should get the files of saturday instead of monday.
this is the code am using now
select files from table1 where [date]>=DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()-1),0) ";
except for sundays i have entered the files for each day
insert into dbtable(date,files) values('date value',' filename');
Assuming your are using SQL Server (and not MySQL), you can use a CROSS APPLY to get data for the last two [dates] with files:
SELECT *
FROM dbo.table1 o
CROSS APPLY ( SELECT TOP 2 [date]
FROM dbo.table1 i
GROUP BY [date]
WHERE [date]>=DATEADD(DAY,DATEDIFF(DAY,0,GETDATE()-5),0)
ORDER BY [date] DESC
) det
WHERE det.[date] = o.[date]
Look for both dates, the current and the one before. From these records take the newer one.
select files
from table1
where date =
(
select max(date)
from table1
where date in (getdate(), getdate() - interval 1 day)
);
(Updated regarding Code-Monk's commment. Thanks.)
This is standard SQL except for the date manipulation which is MySQL specific. If you were mistaken with the DBMS tagged, then replace the part where a day is subtracted from current day with what's appropriate for your DBMS.)
UPDATE: You want to select from two days, the currect day and the last date before. As there should be no future records, this always means the two maximum dates in your table.
The slightly altered query using ORDER BY and LIMIT:
select files
from table1
where date in
(
select date
from table1
order by date desc limit 2
);
Try MySQL function DAYNAME(date1) to check for day is sunday,If so then get difference of two days instead of one.
mysql> SELECT DAYNAME(GETDATE());
+-----------------------+
| DAYNAME('2008-05-15') |
+-----------------------+
| Thursday |
+-----------------------+
1 row in set (0.01 sec)
If you are using SQL server,you can try datename function to get the day name:
datename(dw,getdate())
Try this for SQL Server:
SELECT files from table1
where [date]>=DATEADD(DAY,case when datename(dw,(GETDATE()-1))='SUNDAY' THEN -2 ELSE -1 END,getdate());
For MySQL Server Try:
SELECT files from table1
where `date`>=DATE_SUB(current_Date,interval (case when DAYNAME(date_sub(current_date,interval 1 day))='SUNDAY' THEN 2 ELSE 1 END) day);
Hope this works, because I don't have mysql here to test (only SQL Server):
SELECT files FROM table1
WHERE CAST([date] AS DATE) IN (CURDATE() - CASE DATEPART(dw,GETDATE()) WHEN 2 /* Monday */ THEN 2 ELSE 1 END, CURDATE())
The idea is to test if the date (as date only, no time part) falls in a specific list of dates, being today and yesterday or Saturday.
I've created a virtual table in SQL Server that has 28 days from the current date and each date has rows for time that range from 12-10 pm incremented by 15 min and another value to indicate that it's turned on/off for availability, so it would be something like this:
date time onoff
-------------------------------------------------
2015-04-08 12:00 1
2015-04-08 12:15 1
....continue until 22:00 then start next day
2015-04-09 12:00 1
..... continue for 28 days
I'd like to update the availability based on a query from another table which would return the date, start and end time...
So far I came up with this
update table1
set onoff = 0
where tbl1date in (select tbl2date from table2 where userid = 1)
The problem I'm having is adding in the between certain hours part of the equation and I'm not sure how to do something like this in SQL or how to even search for the answer based on not being able to word it properly...
Can someone help or point me in the right direction?
use a DATETIME, don't use separate DATE and TIME fields.
I think you should take a look at DATEDIFF (https://technet.microsoft.com/pl-pl/library/ms189794(v=sql.110).aspx) function.
Your where query could look like this:
update table1 set onoff = 0
where
DATEDIFF(minute, <MIN_RANGE>, tbl1date) >= 0 and
DATEDIFF(minute, tbl1date, <MAX_RANGE>) >= 0
How you calculate MIN_RANGE and MAX_RANGE depends on your table2 structure.
As suggested, if you have control over the structure, use datetime fields as they are easier to do the comparisons on. I'm going to assume you don't have control over the structure.
In order to compare the datetimes you need to create them from your separate date and times. You can either parse the time field for the hours and minutes and use DATEADD to add the appropriate offsets to the date, or you can use CONVERT to interpret a date time string as a date. Something like
CONVERT(datetime, SUBSTRING(CONVERT(varchar, tbl1date, 121), 1, 10) + ' ' + tbl1time, 121)
What this does is to convert the date to odbc cannonical format and throwaway the time part as it takes only the first 10 characters. Then it appends the time and interprets the whole string as a odbc cannonical datetime string. That format is yyyy-mm-dd hh:mi:ss.mmm. The hours are based on 24 hours. So if your times are in AM/PM format you're going to have to convert them.
If your other table has separate date and times you'd use a similar expression to combine them.
Once you have the datetimes you can do something like this
UPDATE table1
SET onoff = 0
WHERE <expression above> BETWEEN (SELECT min_value FROM table2) AND (SELECT max_value FROM table2)
I need to produce a SQL report showing the number of times a particular event happened in each hourly period during the day. My table has a date/time column on it containing the time the event occurred.
How do I do a count of the number of rows that fall within each each hourly period during the day?
So I need to see output like this...
10:00 - 11:00 12 times
11.00 - 12:00 53 times
12:00 - 13:00 5 times etc
I'm guessing it would be a Group By, but how do you group by each hour?
Thanks in advance.
SELECT DATEPART(hh, DateTimeColumn), COUNT(*)
FROM
TableName
GROUP BY
DATEPART(hh, DateTimeColumn)
ORDER BY
DATEPART(hh, DateTimeColumn)
Seans solution will only work with 24 hours worth of data as datepart dd only returns 0-23.
If you need to process more than that, you'll need to add in the day too.
Something like:
SELECT CAST(DateTimeColumn AS INT) [day],DATEPART(hh, DateTimeColumn), COUNT(*)
FROM
TableName
GROUP BY
CAST(DateTimeColumn AS INT),
DATEPART(hh, DateTimeColumn)
ORDER BY
CAST(DateTimeColumn AS INT),
DATEPART(hh, DateTimeColumn