Sqlite date time, getting the latest date - sqlite

I have a project about a clinic. I have a table in my database that is about the consultation of the patients. My field's are patientid and consultaion_date. So the example inputs is like this:
PatientID|consultation_date
1 | January/3/2001
1 | December/2/2001
1 | December/6/2003
1 | January/2/2005
2 | January/3/2005
2 | January/31/2005
2 | April/5/2005
Can I have a query statement that's going to give me the patientID and the latest date of the consultation of the patients?
PatientID|consultation_date
1 | January/2/2005
2 | April/5/2005

Related

Kusto query to calculate number of users who experienced X number of crashes in Y days since release

My kusto data table records an event any time someone using the product experiences a crash. I want to calculate how many users experienced at least "X" number of crashes in "Y" number of days since the product was released.
So if the product was released on January 1st, and the table of crash events looks like:
| Date | User Id | Days Since Release |
| 1/1 | A | 0 |
| 1/1 | A | 0 |
| 1/1 | B | 0 |
| 1/2 | A | 1 |
| 1/3 | B | 2 |
| 1/4 | C | 3 |
Then the results would be:
| Days Since Release | Number of Crashes | Number of users |
| 0 | 1 | 2 | // Users A and B experienced 1 crash on 0th day
| 0 | 2 | 1 | // User A experienced 2 crashes on 0th day
| 1 | 1 | 2 | // Users A and B experienced at least 1 crash on 1st day
| 1 | 2 | 1 | // User A experienced at least 2 crashes on 1st day
| 1 | 3 | 1 | // User A experienced at least 3 crashes on 1st day
| 2 | 1 | 2 | // Users A and B experienced at least 1 crash on 2nd day
| 2 | 2 | 2 | // Users A and B experienced at least 2 crashes on 2nd day
| 2 | 3 | 1 | // User A experienced at least 3 crashes on 2nd day
| 3 | 1 | 3 | // Users A, B and C experienced at least 1 crash on 3rd day
| 3 | 2 | 2 | // Users A and B experienced at least 2 crashes on 3rd day
| 3 | 3 | 1 | // User A experienced at least 3 crashes on 3rd day
I tried to do this using the activity_engagement function but have not been able to perform the aggregations needed on number of crashes "X" and days since release "Y".
Please see the query below, I believe this is what you're looking for.
The idea is to expand the counts for each user from the day of the crash and until the total DaysSinceRelease (since if a crash happened on day #1, it should also be considered in all days following #1). Then, we also expand range(1, NumCrashes, 1) since if a user had 3 crashes, these should also be counted in the at-least-1 and at-least-2 bins.
let totalDaysSinceRelease = 3;
datatable(Date:datetime, UserId:string, DaysSinceRelease:long)
[
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "B", 0,
datetime(2020-01-02), "A", 1,
datetime(2020-01-03), "B", 2,
datetime(2020-01-04), "C", 3
]
| summarize NumCrashes = count() by UserId, DaysSinceRelease
| order by UserId asc, DaysSinceRelease asc
| extend NumCrashes = row_cumsum(NumCrashes, UserId != prev(UserId))
| extend DaysSinceRelease = range(DaysSinceRelease, totalDaysSinceRelease, 1)
| mv-expand DaysSinceRelease to typeof(long)
| summarize NumCrashes=max(NumCrashes) by UserId, DaysSinceRelease
| extend NumCrashes = range(1, NumCrashes, 1) | mv-expand NumCrashes to typeof(long)
| summarize dcount(UserId) by DaysSinceRelease, NumCrashes
| order by DaysSinceRelease asc, NumCrashes asc

PL/SQL: Creating a grid (like an excel table) with the intersection of 2 selects, one for rows and the other for columns

I have 3 sets of values i wish to display
Class
Duration
Students
The columns of the tables are as follows:
table 1: class - duration
table 2: class - student
The data would look like this:
table 1:
class - duration
1 - 6
2 - 12
3 - 18
4 - 24
Table 2:
class - student
1 - student 1
2 - student 2
2 - student 3
4 - student 3
the return from the select would look like this:
+-----------+---+----+----+----+
| | 6 | 12 | 18 | 24 |
+-----------+---+----+----+----+
| Student 1 | x | | | |
| Student 2 | | x | | |
| Student 3 | | x | | x |
+-----------+---+----+----+----+
thanks for the ideas!
With known durations, you can pivot data like this:
SELECT *
FROM ( SELECT student
, duration
FROM table1 t
JOIN table2 t2 ON t.class = t2.class )
PIVOT
(
count(duration)
FOR duration IN (6,12,18,24)
)
http://sqlfiddle.com/#!4/a8c7bb/57
Other propositions:
You can create dynamic query to fetch duration and create the list.
You can use PIVOT XML and then extractvalue into readable data
but you will need to know the number of returned columns

How to join these two tables properly?

I have these two tables
customers:
id | name | address
---+---------------------+-------------------------
1 | company 1 | some address information
2 | company 2 | another address
3 | yet another company | no address here
orders:
id | customer_id | date
---+-------------+---------
1 | 2 | 20151209
2 | 2 | 20151211
3 | 3 | 20151210
4 | 1 | 20151223
Now I want to get a resulting table with each customer on the left and the amount of orders within an arbitrary period of time on the right.
For example, given this period to be 20151207 <= date <= 20151211, the resulting table should look like this:
name | orders count
--------------------+-------------
company 1 | 0
company 2 | 2
yet another company | 1
Note: date = 20151207 means the 7th of december 2015.
How to join them?
SELECT c.name, COUNT(CASE WHEN ((o.date BETWEEN 20151207 AND 20151211) OR (o.date ISNULL)) THEN o.customer_id END) AS "Total Sales" FROM customers AS c LEFT JOIN orders o ON c.id == o.customer_id GROUP BY c.name

SQLite query select between 2 records on same condition

i am trying to count (in minutes) between two records on 1 table and WHERE clause is the same condition.
_id | venue_id | act_time | status |
1 | 1 | 13:30 | 0 |
2 | 1 | 15:40 | 1 |
3 | 2 | 13:03 | 0 |
4 | 2 | 16:06 | 1 |
when i exec query like this :
SELECT _id, venue_id, status, (julianday(act_time IN (SELECT act_time FROM reports WHERE venue_id='1' AND status='1')) - julianday(act_time))*1440 AS duration FROM reports WHERE venue_id='1' AND status='0'
but, the result show the wrong calculation
Please help me what is the correct query for this problem?
so, if i count the duration between 15:40 - 13:30 (at venue_id='1') = 130 minutes.
thank you.
The IN operator checks whether the value on the left side is contained in the set of values on the right side, and returns a boolean result (0 or 1).
You just want to use the act_time value directly; drop act_time IN.

Group by Week with Columns for each day of week

I'm trying to created a report for my asp.net application which will show the quantity of each item in combination with unit that was ordered for each day of the week. The days of the week are columns.
To be more specific:
I have two table, one is the Orders table with order id, customer name, date etc...
The second table is OrderItems, this table has order id as a foreign key, order Item id, item name, unit (exp: each, box , case), quantity, and price.
When a user picks a date range for the report, for example from 3/2/12 to 4/2/12, on my asp page, the report will group order items by week and will look as follows:
**week (1) starting from sunday of such date to saturday of such date**
item | unit | Sun | Mon | Tues | Wedn | Thur | Fri | Sat | Total Price for week
item1 | bag | 3 | 0 | 12 | 8 | 45 | 1 | 4 | $1234
item4 | box | 2 | 4 | 5 | 0 | 5 | 2 | 6 | $1234
**week (2) starting from sunday of such date to saturday of such date**
item | unit | Sun | Mon | Tues | Wedn | Thur | Fri | Sat | Total Price for week
item1 | bag | 3 | 0 | 12 | 8 | 45 | 1 | 4 | $1234
item4 | box | 2 | 4 | 5 | 0 | 5 | 2 | 6 | $2354
**week (2) starting from sunday of such date to saturday of such date**
item | unit | Sun | Mon | Tues | Wedn | Thur | Fri | Sat | Total Price for week
item1 | bag | 3 | 0 | 12 | 8 | 45 | 1 | 4 | $1234
item4 | box | 2 | 4 | 5 | 0 | 5 | 2 | 6 | $2354
I wish I could have something to show that I have already started, but crystal isn't my strong point and I dont even know where start tackling this one. I do know how to pass parameters and a datatable that I myself pre-filtered before passing it to the report. For example filtering items by date range and customer or order id.
any help would be much appreciated
Create a formula for each day of the week that totals the order.
ie Sunday quantity:
if dayOfWeek(dateField) = 'Sun'
then order.quantity
else 0
Add each day formula to the detail section of the report and then summarize it for each group level. To group it by week, just group by the date field, then set the grouping option to by week. Suppress the detail, and you'll have what you are looking for.
I don't remember the exact name of the dayOfWeek function, but it's something like that.

Resources