Microsoft Access 2010
I'm trying to get the Max visit number of a user within a sequence of visits less than or equal to 13 months apart.
So the last visit that was within a sequence of visits less than or equal to 13 months apart. I want to exclude those who only occasionally use it (ie. over 13 months apart)
I'm using this to run my query:
SELECT ID, AppointmentDate, Visit
FROM tblVisitQuestions t1
WHERE t1.Visit =
(SELECT Max(t2.Visit)
FROM tblVisitQuestions t2
WHERE t2.ID=t1.ID AND (DateDiff("m",[t1].[AppointmentDate],[t2].[AppointmentDate]) <= 13)
GROUP BY t2.ID)
Its working except that its returning multiple values for the same ID number.
Not sure if its an error within my data or my query.
Ex: example data in table
CR AppointmentDate Visit
1 15-Apr-05 0
1 15-Jul-05 1
1 16-May-06 2
1 06-Jun-06 3
1 19-Dec-06 4
1 11-Nov-11 5
1 31-Jan-12 6
2 08-Jun-04 0
2 17-Dec-04 1
2 05-Jul-05 2
2 06-Dec-05 3
2 06-Feb-09 4
2 19-Apr-11 5
what I would like after the query (not what I'm getting now)
CR AppointmentDate Visit
1 19-Dec-06 4
2 06-Dec-05 3
what I am actually getting:
CR AppointmentDate Visit
1 19-Dec-06 4
1 31-Jan-12 6
2 06-Feb-09 4
2 19-Apr-11 5
I need a way to select the min value of the max values I am getting
Any help would be greatly appreciated!
Thanks
For each visit, you can get the maximum visit number in the 13 months afterwards by doing:
SELECT ID, AppointmentDate, Visit,
(SELECT Max(t2.Visit)
FROM tblVisitQuestions as t2
WHERE t2.ID = t1.ID AND (DateDiff("m",[t1].[AppointmentDate],[t2].[AppointmentDate]) <= 13)
) as MaxVisit
FROM tblVisitQuestions as t1;
If you want this only where the visit number is one, then add a where clause:
WHERE t1.Visit = 1;
Related
View of a table
ID
kWh
1
3
1
10
1
8
1
11
2
12
2
4
2
7
2
8
3
3
3
4
3
5
I want to recive
ID
kWh
1
32
2
31
3
12
The table itself is more complex and larger. But the point is this. How can this be done? And I can't know in advance the ID numbers of the first column.
SELECT T.ID,SUM(T.KWH)SUM_KWH
FROM YOUR_TABLE T
GROUP BY T.ID
Do you need this one?
Let's assume your database name is 'testdb' and table name is 'table1'.
SELECT * FROM testdb.table1;
SELECT id, SUM(kwh) AS "kwh2"
FROM stack.table1
WHERE id = 1
keep running the query will all (ids). you will get output.
By following this query you will get desired output.
Hope this helps.
I have data that looks like this:
ID num_of_days
1 0
2 0
2 8
2 9
2 10
2 15
3 10
3 20
I want to add another column that increments in value only if the num_of_days column is divisible by 5 or the ID number increases so my end result would look like this:
ID num_of_days row_num
1 0 1
2 0 2
2 8 2
2 9 2
2 10 3
2 15 4
3 10 5
3 20 6
Any suggestions?
Edit #1:
num_of_days represents the number of days since the customer last saw a doctor between 1 visit and the next.
A customer can see a doctor 1 time or they can see a doctor multiple times.
If it's the first time visiting, the num_of_days = 0.
SQL tables represent unordered sets. Based on your question, I'll assume that the combination of id/num_of_days provides the ordering.
You can use a cumulative sum . . . with lag():
select t.*,
sum(case when prev_id = id and num_of_days % 5 <> 0
then 0 else 1
end) over (order by id, num_of_days)
from (select t.*,
lag(id) over (order by id, num_of_days) as prev_id
from t
) t;
Here is a db<>fiddle.
If you have a different ordering column, then just use that in the order by clauses.
I have two tables:
Report
ReportId CreatedDate
1 2018-01-12
2 2018-02-12
3 2018-03-12
ReportSpecialty
SpecialtyId ReportId IsPrimarySpecialty
1 1 1
2 2 1
3 3 1
1 2 0
1 3 0
I am trying to write a query that will retrieve me the last 10 reports that were published. However, I need to get 1 report from each specialty. Assume there are 100 specialties, I can pass in as an argument any number of specialties, 10, 20, 5, 2, etc...
I'm trying to figure out a way where if I send it all specialties, it will get me the last 10 reports posted based on the last date created, but it won't give me 2 articles from same specialty. If I send it 10 specialties, then I will get 1 of each. If I send it 5, then I'll get 2 of each. If I send it 3 then I'll get 4 of 1 and 3 of other two.
I may need to write multiple queries for this, I'm trying to see if there is a way to do this on the SQL side of things? If there isn't, then how would I break down to multiple queries to get the result I want?
What I have tried is this, however I get multiple reports with same specialties:
SELECT TOP 10 r.ReportId, rs.SpecialtyId, r.CreatedDate
FROM Report r
INNER JOIN ReportSpecialty rs ON r.ReportId = rs.ReportId AND rs.IsPrimarySpecialty = 1
GROUP BY rs.SpecialtyId, r.AceReportid, r.CreatedDate
ORDER BY r.CreatedDate DESC
with cte as (
SELECT R.ReportId, R.CreatedDate, RS.SpecialtyId,
ROW_NUMBER() OVER (PARTITION BY RS.SpecialtyId
ORDER BY R.CreatedDate DESC) as rn
FROM Report R
JOIN ReportSpecialty RS
ON R.ReportId = RS.ReportId
AND RS.IsPrimarySpecialty = 1
WHERE RS.SpecialtyId IN ( .... ids ... )
)
SELECT TOP 10 *
FROM cte
ORDER BY rn, CreatedDate DESC
row_number will create a id for each speciality, so if you pass 3 speciality you will get something like this.
rn speciality_id
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
I have a table and data like below.
ID is distinct for each record, recno is record number which goes multiple status changes as below table. I need to query recno which are in error status and never go to closed status. From below sample data I should get recno 3 as result since record 1 and 2 has error status but those have closed status.
**ID recno status**
1 1 Started
2 1 inProgress
3 1 released
4 1 error
5 1 closed
6 2 Started
7 2 inProgress
8 2 released
9 2 error
10 2 error
11 2 Closed
12 3 Started
13 3 inProgress
14 3 released
15 3 error
If you want the last status, you can do:
select t.*
from t
where t.id = (select max(t2.id) from t t2 where t2.recno = t.recno);
If you want the ones that are in error status:
select t.*
from t
where t.id = (select max(t2.id) from t t2 where t2.recno = t.recno) and
t.status = 'error';
Note: In most databases and even in some versions of Sybase, you can use row_number() for this query as well. However, not all versions of Sybase support window functions.
I am learning SQL and I am stuck with a certain question for a while. I have a huge data set looking like:
id v1
1 3
2 3
3 -
4 5
5 3
6 5
7 3
I need to count how many times each id is in v1. The output i seek is:
id count
1 0
2 0
3 4
4 0
5 2
6 0
7 0
Have been looking for an answer on many forums. The problem is that there are a lot of ids so that I can`t search by number "1" and so on. If I use something like id=v1 i get how many times a row has equal values in these columns. Looking for some help. Please.
Try this:
SELECT t1.id, COUNT(t2.v1)
FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.id = t2.v1
GROUP BY t1.id
ORDER BY t1.id
Demo here