TERADATA Query for entire month - sql

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

Related

A way to check for specific month in specific year sql?

Out of curiosity I was playing around with SQL, and as we all know, DATEPART() and DATENAME() can be used to check for tuples with a specific day, year, or month as a condition for us to browse through records in a relation.
But I was just curious whether there is a way to check for both at the same time without using something like:
DATENAME(MONTH,Datevariable1)="February" AND
DATEPART(DAY,Datevariable2)="26"`
to find these records.
Is there a simpler way of doing it? I am asking this question just out of curiosity.
The month() function will call back a specific month. the range is 0 not a month and 1-12 in this case you wanted Feb so we check month against 2. And the day function acts the same way it will look for that day which is set to 26 in this case. Hope it Helps.
The mysql Statement
SELECT *, MONTH(dateColumn)=2 and DAY(dateColumn)=26 as 'MY_MONTH_AND_DAY' FROM yourTable;
By that you want a function that can return "26 Feb" instead of 26 alone and Feb alone?
If that the case you can use something like this in SQL Server
LEFT(CONVERT(VARCHAR(9), dateColumn, 6),6)

Reporting on multiple time ranges

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

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).

How can I get the average of a date field?

I want to get the average of days between some dates, for example, I have a table called Patient that has the id of the registration, patient's id, entry date and final date:
(1,1,'07-04-2014','08-04-2014'),
(2,2,'07-04-2014','07-04-2014'),
(3,3,'08-04-2014','10-04-2014'),
(4,4,'09-04-2014','10-04-2014')
I want to get the average of days of the entry fields, I have tried a lot of thing but I only get random results. I tried with dtiff but it needs two arguments and I only need one.
You could get the average DURATION between a fixed date and the date field. But averaging a date doesn't really make sense.
SELECT AVG(DATEDIFF(DD,'19700101',dateField)) AS avgDays
You could say the "average" date would then be: DATEADD(DD,avgDays,'19700101')
But I'm not sure if that makes sense in the context of what you're trying to do.
thanks for answering, maybe I couldn't express what I wanted to do but I found a solution and it was actually vey simple:
select Patient.Name, avg(day(Patient.FinalDate) - day(Patient.EntryDate)) as [Average] from Patient,DetailPatient where Patient.IdPatient=DetailPatient.IdPatiene group by DetailPatient.Name
I know it looks very simple haha, but is the first time I use avg function this way.
Thank you guys.

need query for this simple pulling data sql server

I am pulling data, but how do I pull from all data listed till an specific range?
Something like
select *
from xxx
where processdate = '6/30/2013'
this gives me all data only for that specific day..
I want data for all the days till 6/30/2013
I am having problems telling the sql to pull data available till 6/30/2013
This will probably be something really simple, but I cant seem to find the function to tell it
Or will I need to look at the data and see the earliest date, and then do a range
It is quite plain, use <= or < if you want to exclude this day.
select * from xxx where processdate <= '6/30/2013'