I have a table like this,
Date is in yyyy-mm-dd format
Name Date Credits
--------------------------------
Bill 2013-04-04 5
Paul 2013-04-05 4
Bill 2013-04-05 3
Angel 2013-04-07 9
Bill 2013-05-01 8
Paul 2013-05-02 7
Bill 2013-06-15 6
Angel 2013-07-22 15
Paul 2013-07-23 7
Angel 2013-08-11 9
And my expected result is
Name MinDate MaxDate Credits
-----------------------------------------------
Bill 2013-04-04 2013-06-15 1
Paul 2013-04-05 2013-07-23 3
Angel 2013-04-07 2013-08-11 0
How to form the Query. Help needed.
My approach would be something like this:
SELECT t1.name, MIN(t1.date) AS MinDate, MAX(t1.date) AS MaxDate
FROM table t1
GROUP BY t1.name
I don't know how you calculate your credits, though, so I left this one out.
If it's SUM(t1.credit) or something alike, just add this to the FROM-clause.
Hope this helps.
Related
In Sqlite db I have a table: Examination with columns ExamID, InternalPID, ExamDateTime
ExamID InternalPID ExamDateTime (from left to right)
1 2 2015-03-11
2 1 2015-11-11
3 4 2015-05-01
4 6 2015-08-10
5 2 2015-04-22
6 1 2014-12-11
7 2 2015-03-12
the query output should be latest Examination date of each patient. i.e the InternalPID should be distinct with its latest ExamDateTime.
Expect output from query:
ExamID InternalPID ExamDateTime
5 2 2015-04-22
2 1 2015-11-11
3 4 2015-05-01
4 6 2015-08-10
Thank you in advance
You can do this using a join and aggregation or a clever where clause:
select e.*
from examination e
where e.ExamDateTime = (select max(e2.ExamDateTime)
from examination e2
where e2.patientid = e.patientid
);
This is similar to some other questions here, but those use a CASE which I cannot. This is on Oracle, and I will be running the query from an excel sheet. (And by the way these do not support WITH, which makes life much harder)
I have a range of dates in one big table - like 1/3/2011, 4/5/2012, 7/1/2013, 9/1/2013.....
Then I have another table with hours worked by employees on certain dates. So what I need to do is get a sum of number of hours worked by each employee in each intervening time period. So the tables are like
Dates
1-May-2011
5-Aug-2011
4-Apr-2012
....
and another
Employee Hours Date
Sam 4 1-Jan-2011
Sam 7 5-Jan-2011
Mary 12 7-Jan-2012
Mary 5 12-Dec-2013
......
so the result should be
Employee Hours In Date Range Till
Sam 11 1-May-2011
Sam 0 5-Aug-2011
Sam 0 4-Apr-2012
Mary 0 1-May-2011
Mary 0 5-Aug-2011
Mary 12 4-Apr-2012
....
Any pointers on how to achieve this please?
I'm unfamiliar with Oracle SQL and it's abilities/limitations, but since you asked for pointers, here's my take:
Join the tables (INNER JOIN) with the join rule being EmployeeHours.Date < Dates.Dates. Then GROUP BY Employee, Dates.Dates and select the grouping columns + SUM(Hours). What you'd end up with (Using your sample data) is:
Employee | Dates | Hours
Sam | 1-May-2011 | 11
Sam | 5-Aug-2011 | 11
Sam | 4-Apr-2012 | 11
Mary | 1-May-2011 | 0
Mary | 5-Aug-2011 | 0
Mary | 4-Apr-2012 | 12
With other (more complex) data, there will be more "interesting" results, but basically each row contains total hours up to that point.
You could then use that as an input to an outer query to find MAX(Hours) for all rows where Dates < currentDates and subtract that from your result.
Again, this is not a complete answer, but it's a direction that should work.
I have a table with the start date(event.startdate), end date(event.enddate), and the hours/person (event.hrday) of an event. I have another table with weekdays listed which has another field for each person (calendar.name). I want to populate those columns with the total hours worked each day. I can't seem to figure out how to properly sum the hours if two events overlap in dates, I can only come up with a correct value for a single event in a time period.
I believe that in theory this question has the answer I need: compute sum of values associated with overlapping date ranges
But I am very new to SQL and I don't fundamentally understand the solution posted even after some additional research. I am using Access 2013. Apologies if this is a super elementary question, I was hoping what I wanted to do could be handled "visually" with Access...
What I Have: ("event" table)
Startdate | Enddate | Hrsday | Name
5/1/2015 5/12/2015 1.25 Joe
5/7/2015 5/8/2015 8 Joe
What I'm looking for:("calendar" table, days already filled in first column)
Weekdays | Joe | name2 | name3 | ....
5/1/2015 1.25
5/4/2015 1.25
5/5/2015 1.25
5/6/2015 1.25
5/7/2015 9.25
5/8/2015 9.25
5/11/2015 1.25
5/12/2015 1.25
I've tried using the query builder within access to build an UPDATE query, but my result either does not appear at all (no updates, all null) or will only fill in one event with no overlaps. (5/1-5/12 all have 1.25).
I think you will need to create a "date table" if you want to be able to achieve this sort of result in MS-Access (without using windowed functions).
Here is a quick example of how this might work in SQL Server, but only using syntax available to MS-Access (I hope).
--Load the test data into a table variable
DECLARE #event TABLE (
[start_date] DATE,
end_date DATE,
hrsperday NUMERIC(19,2),
name VARCHAR(20));
INSERT INTO #event SELECT '20150401', '20150412', 1.25, 'Joe';
INSERT INTO #event SELECT '20150407', '20150408', 8, 'Joe';
--Add some more test data, to make it more "interesting"
INSERT INTO #event SELECT '20150401', '20150405', 0.1, 'Bill';
INSERT INTO #event SELECT '20150401', '20150430', 7.5, 'Bill';
INSERT INTO #event SELECT '20150412', '20150415', 0.5, 'Bill';
--Make a date table, this creates one on the fly but wouldn't work in MS-Access
--I store a date for each day in 2015/Apr, obviously I would want more dates eventually
DECLARE #dates TABLE (
[date] DATE);
WITH cte AS (
SELECT CONVERT(DATE, '20150401') AS [date]
UNION ALL
SELECT DATEADD(DAY, 1, [date]) FROM cte WHERE [date] < '20150430')
INSERT INTO
#dates
SELECT
[date]
FROM
cte OPTION (MAXRECURSION 0);
--Now the answer is trivial
SELECT
e.name,
d.[date],
SUM(hrsperday) AS hrs
FROM
#dates d
LEFT JOIN #event e ON d.[date] BETWEEN e.[start_date] AND e.end_date
GROUP BY
e.name,
d.[date]
ORDER BY
e.name,
d.[date];
--Note the format you want, but a PIVOT would give you this
--(I don't think PIVOT is supported by MS-Access though)
Results for this are:
name date hrs
Bill 2015-04-01 7.60
Bill 2015-04-02 7.60
Bill 2015-04-03 7.60
Bill 2015-04-04 7.60
Bill 2015-04-05 7.60
Bill 2015-04-06 7.50
Bill 2015-04-07 7.50
Bill 2015-04-08 7.50
Bill 2015-04-09 7.50
Bill 2015-04-10 7.50
Bill 2015-04-11 7.50
Bill 2015-04-12 8.00
Bill 2015-04-13 8.00
Bill 2015-04-14 8.00
Bill 2015-04-15 8.00
Bill 2015-04-16 7.50
Bill 2015-04-17 7.50
Bill 2015-04-18 7.50
Bill 2015-04-19 7.50
Bill 2015-04-20 7.50
Bill 2015-04-21 7.50
Bill 2015-04-22 7.50
Bill 2015-04-23 7.50
Bill 2015-04-24 7.50
Bill 2015-04-25 7.50
Bill 2015-04-26 7.50
Bill 2015-04-27 7.50
Bill 2015-04-28 7.50
Bill 2015-04-29 7.50
Bill 2015-04-30 7.50
Joe 2015-04-01 1.25
Joe 2015-04-02 1.25
Joe 2015-04-03 1.25
Joe 2015-04-04 1.25
Joe 2015-04-05 1.25
Joe 2015-04-06 1.25
Joe 2015-04-07 9.25
Joe 2015-04-08 9.25
Joe 2015-04-09 1.25
Joe 2015-04-10 1.25
Joe 2015-04-11 1.25
Joe 2015-04-12 1.25
I’m working on showing employees that have not entered in any hours for a previous week. I’m currently working with three tables. One table is a calendar that has the first date of each week. The week format is Sunday to Saturday. The second table is the list of hours entered. The Time table contains the date the time was entered and the employees name. The third table is the list of all the employees. I can’t seem to get the joins to work how I would like them to. The end result I would like to see that Bob entered time in week 7 and 8, but week 9 is null. Thank you for your help. Its greatly appreciated.
Current Code
SELECT
d.Resource
,SUM(p.Hours) AS Hours
,m.[WeeksSundayToSaturday]
,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber
FROM CalendarWeeks m
LEFT JOIN [TimeTracking] p ON
(m.[WeeksSundayToSaturday] BETWEEN p.Date AND p.Date + 7)
RIGHT JOIN [DepartmentMembers] d ON
d.Resource = p.CreatedBy
GROUP BY
d.Resource
,m.WeeksSundayToSaturday
Data Tables
Department Members
Name Department
Bob Engineer
Sue HR
John Operations
Time Tracking
Resource Hours Date
Bob 13 2/9/2014
Sue 12 2/10/2014
John 2 2/11/2014
Bob 6 2/12/2014
Bob 8 2/13/2014
John 8 2/14/2014
John 8 2/15/2014
Bob 8 2/16/2014
Bob 1 2/17/2014
Bob 2 2/18/2014
Bob 1 2/19/2014
Bob 8 2/20/2014
Bob 9 2/21/2014
Bob 6 2/22/2014
Sue 8 2/23/2014
John 2 2/24/2014
Calendar
WeeksSundayToSaturday
1/5/2014
1/12/2014
1/19/2014
1/26/2014
2/2/2014
2/9/2014
2/16/2014
2/23/2014
3/2/2014
3/9/2014
3/16/2014
3/23/2014
3/30/2014
Desired Result
Bob
Week 7 = 27
Week 8 = 35
Week 9 = NULL
Your above query is giving compilation error, please try below query i think it will help you
SELECT
d.Resource
,SUM(p.Hours) AS Hours
,m.[WeeksSundayToSaturday]
,DatePart(wk, m.[WeeksSundayToSaturday]) AS WeekNumber
FROM CalendarWeeks m
LEFT JOIN [TimeTracking] p ON (p.Date BETWEEN
m.[WeeksSundayToSaturday] AND Dateadd(d,6, m.[WeeksSundayToSaturday])
RIGHT JOIN [DepartmentMembers] d ON d.Resource = p.CreatedBy
GROUP BY d.Resource ,m.WeeksSundayToSaturday
I have database table that I am after some SQL for (Which is defeating me so far!)
Imagine there are 192 Athletic Clubs who all take part in 12 Track Meets per season.
So that is 2304 individual performances per season (for example in the 100Metres)
I would like to find the top 48 (unique) individual performances from the table, these 48 athletes are then going to take part in the end of season World Championships.
So imagine the 2 fastest times are both set by "John Smith", but he can only be entered once in the world champs. So i would then look for the next fastest time not set by "John Smith"... so on and so until I have 48 unique athletes..
hope that makes sense.
thanks in advance if anyone can help
PS
I did have a nice screen shot created that would explain it much better. but as a newish user i cannot post images.
I'll try a copy and paste version instead...
ID AthleteName AthleteID Time
1 Josh Lewis 3 11.99
2 Joe Dundee 4 11.31
3 Mark Danes 5 13.44
4 Josh Lewis 3 13.12
5 John Smith 1 11.12
6 John Smith 1 12.18
7 John Smith 1 11.22
8 Adam Bennett 6 11.33
9 Ronny Bower 7 12.88
10 John Smith 1 13.49
11 Adam Bennett 6 12.55
12 Mark Danes 5 12.12
13 Carl Tompkins 2 13.11
14 Joe Dundee 4 11.28
15 Ronny Bower 7 12.14
16 Carl Tompkin 2 11.88
17 Nigel Downs 8 14.14
18 Nigel Downs 8 12.19
Top 4 unique individual performances
1 John Smith 1 11.12
3 Joe Dundee 4 11.28
5 Adam Bennett 6 11.33
6 Carl Tompkins 2 11.88
Basically something like this:
select top 48 *
from (
select athleteId,min(time) as bestTime
from theRaces
where raceId = '123' -- e.g., 123=100 meters
group by athleteId
) x
order by bestTime
try this --
select x.ID, x.AthleteName , x.AthleteID , x.Time
(
select rownum tr_count,v.AthleteID AthleteID, v.AthleteName AthleteName, v.Time Time,v.id id
from
(
select
tr1.AthleteName AthleteName, tr1.Time time,min(tr1.id) id, tr1.AthleteID AthleteID
from theRaces tr1
where time =
(select min(time) from theRaces tr2 where tr2.athleteId = tr1.athleteId)
group by tr1.AthleteName, tr1.AthleteID, tr1.Time
having tr1.Time = ( select min(tr2.time) from theRaces tr2 where tr1.AthleteID =tr2.AthleteID)
order by tr1.time
) v
) x
where x.tr_count < 48