I'm wondering if it it possible to SELECT records in which [RRRmonth] field is the current month? I've written this SQL in many ways and can't seen to get it to work.
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE (((CFRRR.[workername]) Is Null) AND ((CFRRR.RRRmonth)=Month([RRRmonth])));
Thanks!
The month function will give you the month of a date/time field. The date function will give you the current date. Use the two together will give you the answer you're looking for
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE CFRRR.[workername] Is Null AND month(CFRRR.[RRRmonth])=month(Date());
You also don't need all those parenthesis and makes for a messy query.
Related
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)
I'm working on a query that pulls a date from another query, I have my reasons for the nesting. The problem I'm facing is that there is a field that is called DueDate.
My SQL is
SELECT DueDate
FROM qryDueDates
WHERE DueDates <= DateAdd("d",60,Date())
The data causing the issue is when it equals something like "1/25/2019", "11/19/2019" or any date in 2019.
Goal
I need to limit the results to show dates that are expired or expiring within 60 days or less.
I'm trying to prepare the dataset for the conditional formatting.
if you can put your nested sub-query in your post that may give better picture, and if you can mention what is the error you are getting that may also help. Since you mentioned that you are getting error only when sub-query returns certain dates, I would suggest that cast your sub-query result to DATE if you have not already done.
Below is my attempt to help you with limited information I could extract from your post. I have used some of MS-SQL function below, please replace with your DB specific function.
SELECT myDates.* FROM (select COLUMN_NAME DueDates from TABLE_NAME) as myDates WHERE myDates.DueDates <= DateAdd("d",60, GETDATE())
Turns out that the original query was screwing it up. I moved the query into the main one and it worked.
Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'
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).
I have around 20,000 entries in a SQL table for which a date column is of the form
YYYY-MM-DD HH-SS. I would like to convert this format to a YYYY-MM-DD format so I can run a query on all of the entries that will count the number of entries based on
a) the month under which they fall
b) the day
I'm new to SQL and not sure if there is a way to loop through all of the entries and check based on the required criteria; and as such, would greatly appreciate any help.
I unfortunately, cannot send a screenshot of the table since the data is classified.
You don't need to change the data in the table. Most databases have year() and month() functions, so you could do:
select year(datecol), month(datecol), count(*)
from sqltable
group by year(datecol), month(datecol)
order by year(datecol), month(datecol);
If these specific functions are not available, then I'm sure your database supports something similar.