Reporting on multiple time ranges - sql

I have a basic query (Select field from table, etc) where I can easily put in created between 1491721200 and 1492326000 and it works fine. It's simple! My question is, can I have one query that will give me data for multiple time ranges? I would like to select the data from Sunday through Thursday from 2-8pm PST in this one query.
Is this possible?
Thanks!

Yes, simple or will do the trick
SELECT * from table where
created between 1491721200 and 1492326000
OR created between 149999999 and 150000000

Related

TERADATA Query for entire month

I wanted to know if there was a way to extract data from a date column pertaining to an entire month.
SEL * FROM DB.TBLNAME TABLE
WHERE TABLE.DATE_ BETWEEN DATE '01-02-2022' AND DATE ' 28-02-2022'
So in the above query(wrote it here as an example so hope i didnt make any typo mistake) im searching for all dates from the 1st to the 28th of february but wanted to know if there was a more elegant method as to select a month match eg. 02-2022.
I hope my question was detailed enough.
Thanks in advance,
For this you could do something like this to make it easier to develop and change as you would need it
SELECT
*
FROM DB.TBLNAME TABLE
WHERE 1=1
AND MONTH(TABLE.DATE) = 2
AND YEAR(TABLE.DATE) = 2022
This is how I would normally do it as you would then be able to write a python script to go with it or a param to dynamically pull the data you need for the month/year

Other code to use other than this "Util.fnDateRange()" in SQL

A colleague wrote a view that needs changing; the problem is I was not there when it was made, so I do not know it inside out as if I coded this SQL view.
My colleague created a view and used CTEs to grab data faster. This is the CTE I'm having trouble with
vDateRange AS
(
SELECT
Date,
Year,
Month,
Day,
DayOfWeek
FROM
Util.fnDateRange('2017-08-17', '2017-08-22')
)
so as you can see this view will only get data from that date range, can someone show me how I can grab FROM something else instead of the Util.fnDateRange() because this CTE is used so many times in the view I can't just comment it out.
I changed the query by using the range like this instead:
Util.fnDateRange('01-01-2014', GETUTCDATE())
now i can get from 2014 till the date of today.

Selecting Between Hours with Timestamp in SQL

I need to figure out how I can select the AVG from another column in a table, between two hour time intervals. I am using PL/SQL/Serverpages or PSP, so the user would select their interval of choice from a drop down menu (ex "2PM-4PM, 4PM-6PM",etc.) and then on the second page, using their choice I will provide information from another column in the table. The issue I have is that the format of my timestamp column is:
30-OCT-16 02.52.00.000000000 PM
30-OCT-16 02.54.00.000000000 PM
The way I have been trying to solve this problem is by using the following methodology:
IF number_text = 1 THEN
SELECT AVG(column) INTO avg_power
FROM table
WHERE date_column BETWEEN TO_DATE('12','HH') AND TO_DATE('2','HH')
AND LIKE '%PM';
I am going to use various IF statements in order to activate each select statement with the IF contingent on which interval the user selects from a drop down list.
As I said, the variable time depends on what the user selects on a prior page. My biggest issues in this situation are figuring out how I am supposed to code the WHERE clause as well as finding a way to work with the data, in terms of hours, as it exists in the database, while also taking AM and PM into account. I greatly appreciate any and all help to solve this issue.

applying knowledge of SQL for everyday workplace activities

My question is how to properly write a SQL query for the below highlighted/bold question.
There is a table in HMO database which stores doctor's working
hours.Table has following fields
"FirstName","LastName","Date","HoursWorked". write a sql statement
which retrieves average working hours for period January-March for a
doctor with name Joe Doe.
so far i have
SELECT HoursWorked
FROM Table
WHERE DATE = (January - March) AND
SELECT AVG(HoursWorked) FROM Table WHERE FirstName="Joe",LastName="Doe"*
A few pointers as this sounds like a homework question (which we don't answer for you here, but we can try to give you some guidance).
You want to put all the things you want to return from your select first and you want to have all your search conditions at the end.
So the general format would be :
SELECT Column1,
Column2,
Column3,
FROM YourTable
WHERE Column4 = Restriction1
AND Column5 = Restriction2
The next thing you need to think about is how the dates are formatted in your database table. Hopefully they're kept in a column of type datetime or date (options will depend on the database engine you're using, eg, Microsoft SQL Server, Oracle or MySql). In reality some older databases people use can store dates in all sorts of formats which makes this much harder, but since I'm assuming it's a homework type question, lets assume it's a datetime format.
You specify restrictions by comparing columns to a value, so if you wanted all rows where the date was after midnight on the 2nd of March 2012, you would have the WHERE clause :
WHERE MyDateColumn >= '2012-03-02 00:00:00'
Note that to avoid confusion, we usually try to format dates as "Year-Month-Day Hour:Minute:Second". This is because in different countries, dates are often written in different formats and this is considered a Universal format which is understood (by computers at least) everywhere.
So you would want to combine a couple of these comparisons in your WHERE, one for dates AFTER a certain date in time AND one for dates before another point in time.
If you give this a go and see where you get to, update your question with your progress and someone will be able to help get it finished if you have problems.
If you don't have access to an actual database and need to experiment with syntax, try this site : http://sqlfiddle.com/
you already have the answer written
SELECT AVG(HoursWorked) FROM Table WHERE FirstName="Joe",LastName="Doe"*
you only need to fix the query
SELECT AVG(HoursWorked) as AVGWORKED FROM Table WHERE FirstName='Joe' AND LastName='Doe'
That query will give you the average hours worked for Joe Doe, however you only need to get between some time you add the next "AND", if you are using SQL server you can use the built in function DateFromParts(year,month,day) to create a new Date, or if you are using another Database Engine you can convert a string to a DateColumn Convert(Date,'MM/dd/yyyy')
Example
SELECT AVG(HoursWorked) as AVGWORKED FROM Table WHERE FirstName='Joe' AND LastName='Doe' AND DateColumn between DateFromParts(year,month,day) and Convert(Date,'MM/dd/yyyy')
In the example i showed both approaches (datefromparts for the initial date, and convert(date) for the ending date).

advise on schema design

I want to store the opening and closing hours of locations into a DB table. Which of the following table design do you think is more ideal? I'm using MySQL or and maybe PostgreSQL (Would it matter?).
One record per day? (returns 1-7 results)
id, location_id, weekday, start_time, closing_time
Or one record per location? (returns 1 result always)
id, location_id, mon_start, mon_end, tue_start, tue_end, ... ... ... sun_start, sun_end
The most common query will simply to display the contents like this (always the entire week):
Location: blablabla
Operating Hours:
Monday: 8 AM to 8 PM
... ...
Sunday: 8 AM to 12 PM
The first solution is clearly correct. The second one is not normalized, harder to program against, and not extendable (for holidays, special hours, etc).
Definitely the first, it's better normalised and you could easily produce the data in the second format using a lookup table for the days and perhaps a view.
As the first poster states, the first solution could easily be extended to include holidays, etc. The second is not so flexible.