I have a table with 5 columns, but I really need only information from two. Here is an example of that table, though mine has 1600+ records:
Date Name
2/18 Bob
2/18 Karen
2/19 Fred
2/20 Jared
2/21 Fred
2/22 Bob
2/23 Steve
2/24 Bob
2/25 Jared
I would like to set a date range and find which names were repeats and which were new. For example, if I did this for 2/18-2/21 and 2/22-2/25, I would see that in 2/22-2/25 Bob and Jared were also found in the 2/18-2/21 date range and that Steve was "new." Does anyone have any ideas on a SQLite query to accomplish this task?
You can do this using conditional aggregation, assuming your dates are really in a reasonable format.
select name,
(case when min(date> < STARTDATE then 'RETURNING'
else 'NEW'
end)
from table t
where date between STARTDATE and ENDDATE
group by name;
Related
In MS ACCESS, I have a table containing names and dates for when a persons yearly exam expires. This exam is valid for 12 months, so the next exam is typically done before all 12 months have expired.
Table, called "Exam", looks like this (in the real table names are unique):
ID Name Dateexp
1 Peter 30/07/2020
2 john 10/09/2020
3 Bob 11/10/2019
4 Peter 25/06/2021
I have a query that shows the persons with a "valid" exam. I looks like this:
SELECT Name As Name, Dateexp As Expiry FROM Overall WHERE Dateexp > now();
It returns:
Name Expiry
Peter 30/07/2020
John 10/09/2020
Peter 25/06/2021
Problem is that "Peter" has done a new exam thereby extending his expiry date from 30/07/2020 to 25/06/21 and I only want the latest one to be shown.
Query should return:
Name Expiry
Peter 25/06/2021
John 10/09/2020
I am truly lost - does anyone have an idea as to how this can be solved?
Thank you!
You can use max and having clause:
Select name, max(dateexp) as dateexp
from overall
Group by name
Having max(dateexp) > now()
If I followed you correctly, you can just use aggregation, and filter with a having clause:
select name, max(dateexp) as expiry
from overall
group by name
having max(dateexp) > now();
This filters on names whose latest expiry date is in the future.
I'm using SQL Management Studio 2012.I Have a query output like below. I would like to have a column that looks at the date completed and if it's greater that 2019-01-01 then the exam is'valid' if earlier then 'Renew'. It can't be a hardcoded 2019-01-01, at the turn of the year I would like it to be looking at 2020-01-01 and so on.
Name Exam Date Completed
Matt English 2018-11-26
James Maths 2019-06-02
I would like
Name Exam Date Completed Valid
Matt English 2018-11-26 No
James Maths 2019-06-02 Yes
I think you simply want to know if the year is the same as the current year. That would be:
(case when year(date_completed) = year(getdate()) then 'Yes' else 'No'
end) as valid
This also works, but I don't know if it's more or less efficient than the case statement above:
SELECT IIF(YEAR(date_completed) < YEAR(GETDATE()),'No','Yes') AS valid
I am use ADO SQL in VBA to try and need the solution to the following example:
Table
ID Effective Date of Documentation
1 1/1/2015
2 6/1/2015
3 1/1/2016
4 6/1/2016
In the example above the documentation for the ID 1 is in effective from 1/1/2015 through 5/31/2015, for ID 2 its in effect from 6/1/2015 through 12/31/2015, and so forth. So if I have a date say 8/1/2015, then I need to return the record which the documentation is in effect for. So in this example the record for ID 2 would be returned. I need some SQL to accomplish this and I cant figure out how! Any ideas? Is this possible to do with this structure or do I need to create some artificial column first for the end date and query that?
You can do this by doing:
select t.*
from table t
where effdate < #date
order by effdate desc
fetch first 1 row only;
This is the SQL standard syntax. Different databases might have different syntax for selecting one row.
EDIT:
In MS Access you would do:
select TOP 1 t.*
from t
where effdate < #date
order by effdate desc;
So essentially I have a table that looks like this:
SalesPerson Target
John 50000.00
Bill 75000.00
Jake 40000.00
I want to add a new column that will make my query look like this:
SalesPerson Target Month
John 50000.00 01/01/14
Bill 75000.00 01/01/14
Jake 40000.00 01/01/14
John 50000.00 02/01/14
Bill 75000.00 02/01/14
Jake 40000.00 02/01/14
And so on.... Obviously the target is a monthly value. The purpose is to be used in a pivot chart in Tableau.
The month field needs to be datetime which should be easy. The only thing coming to mind is to manually do something like:
Convert(datetime, '2014-01-01 00:00:000') as 'MONTH'
and then do that 11 more times and use UNION all each time. This just seems like a lot of text and time. I'm hoping there is a much easier way.
Thanks in advance for all the help!
This is a somehow bizarre scenario, why would you need to repeat every value of your table for every month?. That said, this is one way to do it:
SELECT A.*,
DATEADD(MONTH,B.number,'20140101') AS [Month]
FROM YourTable A
CROSS JOIN (SELECT *
FROM master.dbo.spt_values
WHERE type = 'P'
AND number BETWEEN 0 AND 11) B
Do be aware that this is multiplying the number of rows of your table by 12.
Bit of a newbie to SQL but I'm making my way in pretty well. My question, however, is of ordering things in a specific way. Say, for example, that I have the following table:
DATE RANGE NAME ORDER COUNT
5/5/14 - 5/6/14 Bob Food 3
5/5/14 - 5/6/14 Jim Drink 2
5/4/14 - 5/5/14 Bob Food 3
I would like to order these in a specific way:
DATE RANGE NAME ORDER COUNT
5/4/14 - 5/5/14 Bob Food 3
5/5/14 - 5/6/14 Bob Food 2
5/5/14 - 5/6/14 Jim Drink 2
To where it is ordered by where name and order are the same, count doesn't necessarily have to be the same, and that is all ordered by date range. I've tried various permutations of ORDER BY with no luck, but it seems to me like this should overall be a very simple query. Does anyone have any advice?
Here is a sqlFiddle for the code. You should GROUP BY NAME, ORDER, DATE RANGE, COUNT, then ORDER BY DATE RANGE.