SO I have to list the names of anyone in the tenant family who is older than the tenant itself. There are two tables. Tenant and tenant_family. I have tried comparing the two dates to see which DOB in the tenant_family table is less than (meaning they would be older) the DOB in the tenant table.
This is what I have so far but it appears to be wrong. Can someone direct me towards a way of getting the right output?
SELECT DISTINCT tenant_family.name
FROM TENANT_FAMILY, tenant
WHERE tenant_family.dob < tenant.TENANT_DOB;
Use the DATE function to extract the date part of your query for comparison like:
select distinct tenant_family.name from TENANT_FAMILY, tenant where DATE(tenant_family.dob) < DATE(tenant.TENANT_DOB);
In case your dob and TENANT_DOB are not stored as the DATE data type in your database or they are stored as DATETIME then you might extract only the date part using the function:
select distinct tenant_family.name from TENANT_FAMILY, tenant where date_format(tenant_family.dob,"%y-%m-%d") < date_format(tenant.TENANT_DOB,"%y-%m-%d);
This should give you the correct results after comparison.
Related
I have a table where a student record is inserted and there is a column called joined_date which is of type datetime2 in SQL Server and TIMESTAMP(6) in Oracle. I need to retrieve count of students who joined on a particular date and hence I need to group by joined_date. For that, I need to extract only date (YYYY-MM-DD) from the joined_date column and it should be a generic SQL query.
Query: select joined_date, count(*) from student group by joined_date.
I can use TO_CHAR(joined_date, 'YYYY-MM-DD') AS joined_date in Oracle.
Similarly we can convert in SQL Server with its own convert function. But I need a database agnostic generic query.
More or less, every database has its own functions which do similar job. I'm not sure that you can do what you want, using a single query.
But, if you created a view in each of those databases and extracted desired value in the same format - for example, in Oracle
create or replace view v_student as
select student_id,
student_name,
to_char(joined_date, 'yyyy-mm-dd') joined_date
from student;
then you'd run the same query in all those databases:
select joined_date,
count(*)
from v_student
group by joined_date;
or
select student_name
from v_student
where joined_date = '2020-09-01' --> this is a string!!!
order by student_name;
Of course, that "solves" this particular question. Date arithmetic would be difficult as dates are now represented as strings, but - that's what you asked for.
See if it helps.
I have two tables imported into Access from Excel workbooks.
They are narrow tables:
INRMaster: MastDate Date/Time (Short Date)
CIInput: CIDate Date/Time (Short Date)
INRTestResult Number
Dose Number
OutOfRange Short Text
CIInput table was downloaded and the date was date/time with date and time of test. I reformatted that date field to mm:dd:yyyy to match the table I created, INRMaster.
There is no primary key on either table. I tried the join with primary keys of date in both tables and it returned nothing as well. Creating the query using the QBE Grid.
The generated SQL is as follows:
SELECT INRMaster.MastDate, CIINput.[INR test result], CIINput.Dose, CIINput.OutofRange
FROM INRMaster INNER JOIN CIINput ON INRMaster.MastDate = CIINput.CIDate
Office 365 Access, Windows 10.
Setting Format property does not change data. Unless you actually modify saved values, time part is still there and since it is unlikely values will agree to the second, a join will fail. Don't apply formatting in table - view full saved value.
Extract date part with expression in query. If both fields were saved with date and time components, then extract date portion from both. Consider:
SELECT INRMaster.MastDate, CIINput.[INR test result], CIINput.Dose, CIINput.OutofRange
FROM INRMaster INNER JOIN CIINput ON Int(INRMaster.MastDate) = Int(CIINput.CIDate);
DO NOT OPEN QUERY IN DESIGN VIEW. Query Designer cannot resolve this join. Would have to join nested subqueries to enable Design View.
My mysql database table has multiple entries with the following structure:
id, title, date, time
There are presently 30 entries in the table and some of those share a common date.
What I'm trying to accomplish is retrieving the database data in such a way that will group them under common dates. So, all entries that share the same date will be grouped in an array indexed by that common date.
In another post, I learnt INDEX BY is great for what I'm trying to achieve but it works only/best on unique fields.
So, I am just curious if there is anything else that could help efficiently group my database entrie.
SELECT date, GROUP_CONCAT(title)
FROM tbl
GROUP BY date
ORDER BY date;
Don't worry about performance until you have thousands of rows.
My table name is client_details and my date field contain group of dates like
03/03/2015,04/13/2015,05/11/2015,06/08/2015,09/04/...
03/18/2015,04/28/2015,05/26/2015,06/23/2015,09/19/...
03/20/2015,04/30/2015,05/28/2015,06/25/2015,09/21/...
03/26/2015,05/06/2015,06/03/2015,07/01/2015,09/27/...
03/26/2015,05/06/2015,06/03/2015,07/01/2015,09/27/...
03/06/2015,04/16/2015,05/14/2015,06/11/2015,09/07/...
03/13/2015,04/23/2015,05/21/2015,06/18/2015,09/14/...
04/16/2015,05/27/2015,06/24/2015,07/22/2015,10/18/...
03/03/2015,04/13/2015,05/11/2015,06/08/2015,09/04/...
03/04/2015,04/14/2015,05/12/2015,06/09/2015,09/05/...
03/19/2015,04/29/2015,05/27/2015,06/24/2015,09/20/...
I want to search a date between '03/26/2015' and '05/12/2015'.How can i write the query?
You may try this:
SELECT * FROM client_details WHERE `DateField` BETWEEN '03/26/2015' AND '05/12/2015';
SQL Fiddle
EDIT:-
So your comments show that you are storing the dates by seperating them with comma. I would seriosuly discourage that practise and recommend you to change the design of your table. That is a poor way of storing the dates.
Reasons:
It will lead you to troubles everytime you want to use date functions.
It is not performance effective.
Alternate Solutions:
Change the design of your table.
Create a seperate table which stores the dates corresponding to a user_id and then join this table with your client_details table using the user_id and get the date which you want.
I have a table lead and there is a field called added_on (datatype timestamp), I want to to fetch only the leads which are interested in a particular product and the reports should come monthly.
interested_in is a field in the lead table where the interested product's id will be stored as a comma separated values.
and $prod_id is stored with a product id which has to be checked.
the below query works fine just to fetch out the leads which are interested in a particular product. but i want the results to come month by month.
select*from lead where find_in_set('$prod_id',interested_in)
Please guide me what i have to do to achieve that
TRY
WHERE MONTH(added_on) = $giveMonthNumber
OR
WHERE MONTHNAME(added_on) = $givenMonthName;
Reference :
MySQL date time functions
Do this:
select * from lead where find_in_set('$prod_id',interested_in) group by added_on