Oracle SQL Count and Avg in the same query - sql

I have the table PATIENT_SESSIONS with these fields:
PATIENT_ID,
Session_Date,
Session_Status (Scheduled, Completed, Canceled),
PATIENT_Paid_Date,
Amount
I want from this table to get for each patient_id the last session_date, the average between PATIENT_Paid_Date and Session_Date, the max(Amount) and count of Complete sessions in a single query.
Is it possible?

Guessing PATIENT_ID is what you mean by "for each student_id"?
SELECT
PATIENT_ID
, MAX(Session_Date) AS last_session_date
, AVG(Session_Date - PATIENT_Paid_Date) AS avg_between_dates
-- not sure if this is what you want without seeing sample data
, MAX(Amount) AS max_amount
, SUM(CASE WHEN Session_Status = 'Completed' THEN 1 ELSE 0 END)
AS count_complete_sessions
FROM PATIENT_SESSIONS
GROUP BY PATIENT_ID
Should be possible.

Related

How to solve a nested aggregate function in SQL?

I'm trying to use a nested aggregate function. I know that SQL does not support it, but I really need to do something like the below query. Basically, I want to count the number of users for each day. But I want to only count the users that haven't completed an order within a 15 days window (relative to a specific day) and that have completed any order within a 30 days window (relative to a specific day). I already know that it is not possible to solve this problem using a regular subquery (it does not allow to change subquery values for each date). The "id" and the "state" attributes are related to the orders. Also, I'm using Fivetran with Snowflake.
SELECT
db.created_at::date as Date,
count(case when
(count(case when (db.state = 'finished')
and (db.created_at::date between dateadd(day,-15,Date) and dateadd(day,-1,Date)) then db.id end)
= 0) and
(count(case when (db.state = 'finished')
and (db.created_at::date between dateadd(day,-30,Date) and dateadd(day,-16,Date)) then db.id end)
> 0) then db.user end)
FROM
data_base as db
WHERE
db.created_at::date between '2020-01-01' and dateadd(day,-1,current_date)
GROUP BY Date
In other words, I want to transform the below query in a way that the "current_date" changes for each date.
WITH completed_15_days_before AS (
select
db.user as User,
count(case when db.state = 'finished' then db.id end) as Completed
from
data_base as db
where
db.created_at::date between dateadd(day,-15,current_date) and dateadd(day,-1,current_date)
group by User
),
completed_16_days_before AS (
select
db.user as User,
count(case when db.state = 'finished' then db.id end) as Completed
from
data_base as db
where
db.created_at::date between dateadd(day,-30,current_date) and dateadd(day,-16,current_date)
group by User
)
SELECT
date(db.created_at) as Date,
count(distinct case when comp_15.completadas = 0 and comp_16.completadas > 0 then comp_15.user end) as "Total Users Churn",
count(distinct case when comp_15.completadas > 0 then comp_15.user end) as "Total Users Active",
week(Date) as Week
FROM
data_base as db
left join completadas_15_days_before as comp_15 on comp_15.user = db.user
left join completadas_16_days_before as comp_16 on comp_16.user = db.user
WHERE
db.created_at::date between '2020-01-01' and dateadd(day,-1,current_date)
GROUP BY Date
Does anyone have a clue on how to solve this puzzle? Thank you very much!
The following should give you roughly what you want - difficult to test without sample data but should be a good enough starting point for you to then amend it to give you exactly what you want.
I've commented to the code to hopefully explain what each section is doing.
-- set parameter for the first date you want to generate the resultset for
set start_date = TO_DATE('2020-01-01','YYYY-MM-DD');
-- calculate the number of days between the start_date and the current date
set num_days = (Select datediff(day, $start_date , current_date()+1));
--generate a list of all the dates from the start date to the current date
-- i.e. every date that needs to appear in the resultset
WITH date_list as (
select
dateadd(
day,
'-' || row_number() over (order by null),
dateadd(day, '+1', current_date())
) as date_item
from table (generator(rowcount => ($num_days)))
)
--Create a list of all the orders that are in scope
-- i.e. 30 days before the start_date up to the current date
-- amend WHERE clause to in/exclude records as appropriate
,order_list as (
SELECT created_at, rt_id
from data_base
where created_at between dateadd(day,-30,$start_date) and current_date()
and state = 'finished'
)
SELECT dl.date_item
,COUNT (DISTINCT ol30.RT_ID) AS USER_COUNT
,COUNT (ol30.RT_ID) as ORDER_COUNT
FROM date_list dl
-- get all orders between -30 and -16 days of each date in date_list
left outer join order_list ol30 on ol30.created_at between dateadd(day,-30,dl.date_item) and dateadd(day,-16,dl.date_item)
-- exclude records that have the same RT_ID as in the ol30 dataset but have a date between 0 amd -15 of the date in date_list
WHERE NOT EXISTS (SELECT ol15.RT_ID
FROM order_list ol15
WHERE ol30.RT_ID = ol15.RT_ID
AND ol15.created_at between dateadd(day,-15,dl.date_item) and dl.date_item)
GROUP BY dl.date_item
ORDER BY dl.date_item;

How to get the difference between (multiple) two different rows?

I have a set of data containing some fields: month, customer_id, row_num (RANK), and verified_date.
The rank field indicates the first (1) and second (2) purchase of each customer. I would like to know the time difference between first and second purchase for each customer and show only its first month = month where row_num = 1.
https://i.ibb.co/PjJk5Y0/Capture.png
So my expected result is like below image:
https://i.ibb.co/y5Mww7k/Capture-2.png
I'm using StandardSQL in Google Bigquery.
row_num, verified_date
from table
GROUP BY 1, 2```
We can try using a pivot query here, aggregating by the customer_id:
SELECT
MAX(CASE WHEN row_num = 1 THEN month END) AS month,
customer_id,
1 AS row_num,
DATE_DIFF(MAX(CASE WHEN row_num = 2 THEN verified_date END),
MAX(CASE WHEN row_num = 1 THEN verified_date END), DAY) AS difference
FROM yourTable
GROUP BY
customer_id;

Calculate % of total - redshift / sql

I'm trying to calculate the percentage of one column over a secondary total column.
I wrote:
create temporary table screenings_count_2018 as
select guid,
datepart(y, screening_screen_date) as year,
sum(case when screening_package = 4 then 1 end) as count_package_4,
sum(case when screening_package = 3 then 1 end) as count_package_3,
sum(case when screening_package = 2 then 1 end) as count_package_2,
sum(case when screening_package = 1 then 1 end) as count_package_1,
sum(case when screening_package in (1, 2, 3, 4) then 1 end) as count_total_packages
from prod.leasing_fact
where year = 2018
group by guid, year;
That table establishes the initial count and total count columns. All columns look correct.
Then, I'm using ratio_to_report to calculate the percentage (referencing this tutorial):
create temporary table screenings_percentage as
select
guid,
year,
ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
I also tried:
select
guid,
year,
sum(count_package_1/count_total_packages) as percentage_package_1
-- ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
Unfortunately, percentage_package_1 just returns all null values (this is not correct - I'm expecting percentages). Neither are working.
What am I doing wrong?
Thanks!
Since you are already laid out the columns with components and a total, in creating screenings_count_2018, do you actually need to use ratio_to_report?
select
, guid
, year
, count_package_1/count_total_packages as percentage_package_1
, count_package_2/count_total_packages as percentage_package_2
, count_package_3/count_total_packages as percentage_package_3
, count_package_4/count_total_packages as percentage_package_4
from screenings_count_2018
That should work. NB are you guaranteed to never have count_total_packages be zero? If it can be zero you'll need to handle it. One way is with a case statement.
If you wish for the per-package percentages to appear in a single column, then you can use ratio_to_report -- it is a "window" analytic function and it will be something like this against the original table.
with count_table as (
select guid
, datepart(y, screening_screen_date) as year
, screening_package
, count(1) as count
from prod.leasing_fact
where year = 2018
group by guid
, datepart(y, screening_screen_date)
, screening_package
)
select guid
, year
, screening_package
, ratio_to_report(count) over(partition by guid, year, screening_package) as perc_of_total
from count_table
you will need round(100.0*count_package_1/count_total_packages,1) and so on as you already calculated the subtotal and total

How can I add cumulative sum column?

I use SqlExpress
Following is the query using which I get the attached result.
SELECT ReceiptId, Date, Amount, Fine, [Transaction]
FROM (
SELECT ReceiptId, Date, Amount, 'DR' AS [Transaction]
FROM ReceiptCRDR
WHERE (Amount > 0)
UNION ALL
SELECT ReceiptId, Date, Amount, 'CR' AS [Transaction]
FROM ReceiptCR
WHERE (Amount > 0)
UNION ALL
SELECT strInvoiceNo AS ReceiptId, CONVERT(datetime, dtInvoiceDt, 103) AS Date, floatTotal AS Amount, 'DR' AS [Transaction]
FROM tblSellDetails
) AS t
ORDER BY Date
Result
want a new column which would show balance amount.
For example. 1 Row should show -2500, 2nd should -3900, 3rd should -700 and so on.
basically, it requires previous row' Account column's data and carry out calculation based on transaction type.
Sample Result
Well, that looks like SQL-Server , if you are using 2012+ , then use SUM() OVER() :
SELECT t.*,
SUM(CASE WHEN t.transactionType = 'DR'
THEN t.amount*-1
ELSE t.amount END)
OVER(PARTITION BY t.date ORDER BY t.receiptId,t.TransactionType DESC) as Cumulative_Col
FROM (YourQuery Here) t
This will SUM the value when its CR and the value*-1 when its DR
Right now I grouped by date, meaning each day will recalculate this column, if you want it for all time, replace the OVER() with this:
OVER(ORDER BY t.date,t.receiptId,t.TransactionType DESC) as Cumulative_Col
Also, I didn't understand why in the same date, for the same ReceiptId DR is calculated before CR , I've add it to the order by but if thats not what you want then explain the logic better.

Working out total from sub total and amount

I have a table with purchased orders data.
Each row contails the amount of certain item purchased, cost per item and the order number group. Each different item purchased is a new row with same order number.
I basically want to return the total cost for that order. I have tried the following but am getting nowhere:
SELECT order_number, SUM( sub_total ) AS `total`
FROM
SELECT order_number, SUM( SUM( amount ) * SUM( cost_per_item ) ) AS `sub_total`
FROM `ecom_orders`
WHERE member_id = '4'
GROUP BY order_number
ORDER BY purchase_date DESC
Pretty much any SQL-92 compliant RDBMS will take this:
SELECT
order_number
,SUM(amount * cost_per_item) AS total
,purchase_date
FROM
ecom_orders
WHERE member_id = '4'
GROUP BY order_number,purchase_date
ORDER BY purchase_date DESC