mondrian - Count only unique members in sets - mdx

I am new to Mondrian, and I need some help with MDX query.
For example:
year 1 id_client: 1,2
year 2 id_client: 2,3
year 3 id_client: 1
If I try to do a "distinct-count" aggregator for id_client, I get this result:
year 1 2
year 2 2
year 3 1
How can I calculate only new id_client? I need this result:
years new
year 1 2 both clients are new
year 2 1 only client with ID = 3 is new
year 3 0 no new clients

Related

sql snowflake, aggregate over window or sth

I have a table below
days
balance
user_id
wanted column
2022/08/01
10
1
1
2022/08/02
11
1
1
2022/08/03
10
1
1
2022/08/03
0
2
1
2022/08/05
3
2
2
2022/08/06
3
2
2
2022/08/07
3
3
3
2022/08/08
0
2
3
since I'm new to SQL couldn't aggregate over window by clauses, correctly.
which means; I want to find unique users that have balance>0 per day.
thanks
update:
exact output wanted:
days
unque users
2022/08/01
1
2022/08/02
1
2022/08/03
1
2022/08/05
2
2022/08/06
2
2022/08/07
3
2022/08/08
3
update: how if I want to accumulate the number of unique users over time? with consideration of new users [means: counting users who didn't exist before], and the balance > 0
everyones help is appreaciated deeply :)
SELECT
*,
COUNT(DISTINCT CASE WHEN balance > 0 THEN USER_ID END) OVER (ORDER BY days)
FROM
your_table

Sum not working when using with, sum, group by, order by and from

I have code that gets columns from two different tables in order to show customer IDs and sum all the products they used for the duration of their subscription. However the code is not summing so for a table like this, I want the output to be the same columns except product usage is summed by product and master_id.
master_id
product
product_usage
id
billing_period_start_date
start_date
1
apples
2
1
January 1
January 1
1
apples
5
1
February 1
January 1
1
oranges
3
1
January 1
January 1
2
oranges
1
2
January 1
January 1
2
oranges
7
2
February 1
January 1
2
apples
2
2
January 1
January 1
Output
master_id
product
product_usage
id
billing_period_start_date
start_date
1
apples
7
1
January 1
January 1
1
oranges
3
1
January 1
January 1
2
oranges
8
2
January 1
January 1
2
apples
2
2
January 1
January 1
but my code is not doing the sum and is returning everything separately like the first table. Any ideas on how to fix?
with results as
(select bill.master_id,
bill.product,
sum(bill.product_usage),
subscription.id,
bill.billing_period_start_date,
subscription.start_date,
from
bill,
subscription
where bill.master_id = subscription.id
and master_id in ('1',
'2',
'3',
'4',
'5',)
and billing_period_start_date >= start_date
group by master_id, product, id, billing_period_start_date, start_date
order by master_id
)
select * from results
Maybe like this?
select
bi.master_id,
bi.product,
su.id,
bi.billing_period_start_date,
su.start_date,
sum(bi.product_usage)
from
bill bi,
subscription su
where
bi.master_id = su.id and bi.master_id in ('1','2','3','4','5') and bi.billing_period_start_date >= su.start_date
group by
bi.master_id,
bi.product,
su.id,
bi.billing_period_start_date,
su.start_date
order by bi.master_id;

How can I calculate user session time from heart beat data in Presto SQL?

I'm currently recording when user's are active via a heart beat. It's stored in a table like so:
User ID
Minute of Day
1
3
1
4
1
5
1
8
1
9
2
2
2
3
2
4
User ID 1 is active from 3 to 5 but then is inactive from 6 to 7 and then becomes active again from 8 to 9.
User ID 1 was active for 3 minutes: (5-3 + 9-8) = 3
User ID 2 was active for 2 minutes: 4-2 = 2
How can I calculate this using a SQL (Presto) query?
Output should be like so:
User ID
Total Minutes
1
3
2
2
You may try the following which uses the lag function to determine active periods (diff = 1) before summing them
SELECT
USERID,
SUM(diff) as TotalMinutes
FROM (
SELECT
UserId,
(MinuteofDay - LAG(MinuteofDay,1,MinuteofDay) OVER (PARTITION BY UserId ORDER BY MinuteofDay)) as diff
FROM
my_table
) t
WHERE
diff = 1
GROUP BY
UserID;
userid
TotalMinutes
1
3
2
2
View on DB Fiddle

Oracle Sql cumulative sum by month and user

I have an table like below in oracle
USER
YEAR
MONTH
POINT
1
2020
1
1
2
2020
1
1
3
2020
1
0
1
2020
2
1
2
2020
2
0
3
2020
2
1
1
2020
3
1
2
2020
3
0
3
2020
3
1
now I want to get table like below
USER
YEAR
MONTH
POINT
1
2020
1
1
1
2020
2
1
1
2020
3
2
2
2020
1
1
2
2020
2
1
2
2020
3
1
3
2020
1
0
3
2020
2
1
3
2020
3
2
I tried below but not working what I expected
SELECT A_YEAR,A_MONTH,USER, SUM(POINTT) OVER (ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
FROM (
SELECT WA.USER, WA.A_YEAR,WA.A_MONTH,1 AS POINTT FROM HRANALY.PUAN_ACTUAL PA
INNER JOIN HRANALY.WAGE_ACTUAL WA ON WA.USER= PA.USER AND WA.A_YEAR = PA.A_YEAR AND WA.A_MONTH = PA.A_MONTH
INNER JOIN HRANALY.PUAN_ACTUAL_DETAIL PAD ON PAD.REF_WAGE=PA.ID AND PAD.KALEM_KOD='GRUP_HEDEF' AND PAD.AMOUNT>0
ORDER BY a_month
)
ORDER BY A_YEAR,A_MONTH,USER;
in this query CUM_POINT goes from 1 to n not working as user year month based
How can I get second table with query.
Thanks in advance
You need analytical function as follows:
select user_id, year, month,
sum(point) over (partition by user_id order by year, month) as points
from t
In ytour query just add partition by clause as follows:
SUM(POINTT) OVER (PARTITION BY USER ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
select x.*,
sum(point) over (partition by year, month, userid) sum_point
from foo x

Find missing dates from multi table query

I'm looking to write a query that can count missing entries from a table of dates based on skills that a resource has to forecast availability of resource for booking. I'm not sure if it can be done and I'm certainly struggling with the logic!!
Tables
Dates
ID dateFrom StaffID
1 01-06-2014 1
2 02-06-2014 1
3 03-06-2014 1
4 04-06-2014 1
5 05-06-2014 1
6 01-06-2014 2
7 03-06-2014 2
8 04-06-2014 2
9 05-06-2014 2
10 06-06-2014 2
(Free dates on the 6th for staffID 1 and 2nd for staffID 2)
Staff
StaffID Name
1 John
2 Paul
Skills
ID StaffID SkillID
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 2 4
So I want to write a query that says in June, for each of the skills there is X no of days available to book. Is this even possible? looking for records that don't exist to join with a staff table?
I've put together a calendar table that can identify days without bookings but I'm struggling from there on to be honest.
Any help would be greatly appreciated!!
Steve
EDIT: DB is SQL 2005.
Expected output (if possible)
SkillID Number of days available
1 20
2 22
3 14
etc
create a calendar table with all possible dates (booked or not)
select count(distinct ad.calendarDate), s.SkillID
from all_dates ad
cross join skills s
where not exists (
select 1 from
dates where dateFrom = ad.calendarDate
and StaffID = s.StaffID
)
group by s.SkillID
If I understand your problem, your query will be some thing like:
Select sum(temp.nbrDate), temp.SkillID from
(Select s.SkillID, count (d.ID) as nbrDate from Skills s, Dates d
where s.StaffID = d.StaffID
Group by SkillID) temp
group by SkillID
If you want to add a date range, add this in your where close:
and d.DateForm between '01-06-2014' and '30-06-2014'