Hi I need help analyzing below data. The logic I need is sum of each provider should be divided by rest of the providers. For example based on below data each sum(provider) should be dived by sum( rest of providers)
sum(east RISK)/sum(west Risk)+sum(south RISK)
sum(west RISK)/sum(east RISK)+sum(south RISK)
sum(south RISK)/sum(east RISK)+sum(west RISK)
and so on....
....
....
Mbr Provider Group Risk
1 east Group 2.44
2 east Group 0.05
3 east Group 1.01
4 east Group 0.14
5 west Comp MRKT 0.32
6 west Comp MRKT 2.12
7 south Comp MRKT 5.78
8 south Comp MRKT 1.11
I think you can use ANSI standard window functions for this purpose:
select provider,
(sum(risk) / (sum(sum(risk)) over () - sum(risk))
)
from t
group by provider;
Related
Have a table based on Covid-19 data from the Department of Health (UK).
So far so nice. But I want to create an extra column for the percentage change. I am still learning. I'll add the data although it has been modified - there is a question about how I did that in Pandas. How to transform data in Pandas with categoricals and timestamps for use in tableau
Update
I know I have to switch year data to create column.
Type 1 Acute? NHS England Region Code Name Date Value
0 No East of England 8D379 CARETECH COMMUNITY SERVICES LTD 2020-08-01 0.0
1 No East of England 8D379 CARETECH COMMUNITY SERVICES LTD 2020-08-02 0.0
2 No East of England 8D379 CARETECH COMMUNITY SERVICES LTD 2020-08-03 0.0
3 No East of England 8D379 CARETECH COMMUNITY SERVICES LTD 2020-08-04 0.0
4 No East of England 8D379 CARETECH COMMUNITY SERVICES LTD 2020-08-05 0.0
I tried this combination. Would be too confusing for most people...
Any analysis in Tableau can be expressed in terms of percentages. For example, rather than viewing sales for every product, you might want to view each product’s sales as a percentage of the total sales for all products.
Read about % change
I have a report I am trying to simplify but I am running into an issue.
(Undesired) The rows/columns of the report currently look like the following.
Department
Total
Probation (%)
Suspended (%)
All Employees
32
16.3
1.4
All Teams
30
23.5
2.2
Total Men's Teams
10
14.8
2.8
Total Women's Teams
10
34.3
1.4
Men's Wear
10
5.9
0.0
Women's Wear
10
21.4
0.0
UniSec Wear
10
15.0
6.3
This is happening because two people work on two teams. One person works in Mens Wear and UniSex Wear, and one person works in Women's Wear and UniSex Wear. The below table has records like this.
Col1
Col2
1234
Men's Wear
1234
UniSex Wear
9876
Women's Wear
9876
UniSex Wear
(Desired) Im looking for something like this.
Department
Total
Probation (%)
Suspended (%)
All Employees
30
16.3
1.4
All Teams
30
23.5
2.2
Total Men's Teams
10
14.8
2.8
Total Women's Teams
10
34.3
1.4
Men's Wear
10
5.9
0.0
Women's Wear
10
21.4
0.0
UniSec Wear
10
15.0
6.3
I have thought about using LISTAGG() on Col2 to get this affect.
Col1
Col2
1234
Men's Wear,UniSex Wear
9876
Women's Wear,UniSex Wear
Using LISTAGG() gives me the correct count for "All Employees" but then I get groupings of "Men's Wear,UniSex Wear" instead of a separate one for "Men's Wear" and one for "UniSex Wear". Is it possible to group by the individual comma separated values in Col2 after they have been LISTAGG()'ed, or is there a better way of achieving my end results?
Any assistance on achieving this would be greatly appreciated.
I would advise correcting the All_Employees data alone instead of doing the LISTAGG.
OR
Use a separate table to LISTAGG and un-LISTAGG your data which is different from the original table used to calculate the Total, Probation and Suspended data
For un-LISTAGG you can use the below example where table_two is your source table.
with
d2 as (
select
distinct id,
regexp_substr(
products, '[^,]+', 1, column_value
) as products
from
table_two cross
join TABLE(
Cast(
MULTISET (
SELECT
LEVEL
FROM
dual CONNECT BY level <= REGEXP_COUNT(products, '[^,]+')
) AS sys.ODCINUMBERLIST
)
)
)
SELECT
ID,
PRODUCTS
FROM
d2;
I need some help with creating a query as SAS proc SQL.
Consider the following dataset which has sales from different regions already bucketed by 3 hour chunks (its only a subset, actual data covers 24 hours):
Date ObsAtHour Region Sales
1/1/2018 2 Asia 76
1/1/2018 2 Africa 5
1/1/2018 5 Asia 14
1/1/2018 5 Africa 10
2/1/2018 2 Asia 40
2/1/2018 2 Africa 1
2/1/2018 5 Asia 15
2/1/2018 5 Africa 20
I get data covering last 45 days..
I am trying to do two things
1) Group by date, ObsAtHour and Region and get cumulative sum of Sales such that I get something like
Date ObsAtHour Region Sales CumSales
1/1/2018 2 Asia 76 76
1/1/2018 2 Africa 5 5
1/1/2018 5 Asia 14 90
1/1/2018 5 Africa 10 15
2/1/2018 2 Asia 40 40
2/1/2018 2 Africa 1 1
2/1/2018 5 Asia 15 55
2/1/2018 5 Africa 20 21
2) Get Percentage for sales that indicate what percentage of daily sales per Region has been achieved at any obsAtHour. It would look like:
Date ObsAtHour Region Sales CumSales Pct
1/1/2018 2 Asia 76 76 84%
1/1/2018 2 Africa 5 5 33%
1/1/2018 5 Asia 14 90 100%
1/1/2018 5 Africa 10 15 100%
2/1/2018 2 Asia 40 40 72%
2/1/2018 2 Africa 1 1 4.76%
2/1/2018 5 Asia 15 55 100%
2/1/2018 5 Africa 20 21 100%
Your help will be very appreciated.
something like below
data have;
input Date:mmddyy10. ObsAtHour Region $ Sales;
format date mmddyy10;
datalines;
1/1/2018 2 Asia 76
1/1/2018 2 Africa 5
1/1/2018 5 Asia 14
1/1/2018 5 Africa 10
2/1/2018 2 Asia 40
2/1/2018 2 Africa 1
2/1/2018 5 Asia 15
2/1/2018 5 Africa 20
;
proc sort data=have;
by date region;
run;
/* this gives moving sum*/
data have1;
format date mmddyy10.;
set have;
by date region;
if first.region then sumsales = sales;
else sumsales+sales;
run;
/* get the total sales from your intial table by group and join it back
and calculate the percent*/
proc sql;
select a.*, sumsales/tot_sales as per format =percent10.2 from
(select * from have1)a
inner join
(select region , date, sum(sales) as tot_sales
from have
group by 1, 2)b
on a.region =b.region
and a.date =b.date;
The key to understanding the following query is that the cumulative levels will be called tiers. The tiers are used as part of the self-join criteria to restrict the items that are grouped for being summed.
Data
data have;
input Date ddmmyy10. ObsAtHour Region $ Sales;
format Date yymmdd10.;
datalines;
1/1/2018 2 Asia 76
1/1/2018 2 Africa 5
1/1/2018 5 Asia 14
1/1/2018 5 Africa 10
2/1/2018 2 Asia 40
2/1/2018 2 Africa 1
2/1/2018 5 Asia 15
2/1/2018 5 Africa 20
run;
Sample query
The second query (percentage computation) is performed off the result of the first query (cumulative computation), however, the first query could by embedded as a nested query within the second one.
proc sql;
create table want(label='Cumulative within day up to obsathour') as
select
tiers.Date
, tiers.ObsAtHour
, tiers.Region
, Sum(case when have.ObsAtHour = tiers.ObsAtHour then have.Sales else 0 end) as SalesAtTier
, Sum(have.Sales) as CumSales
, Count(*) as CumCount
from
have
join
(select distinct Date, ObsAtHour, Region from have) as tiers
on
have.Date = tiers.Date
and have.Region = tiers.Region
and have.ObsAtHour <= tiers.ObsAtHour
group by
tiers.Date, tiers.Region, tiers.ObsAtHour
order
by Date, ObsAtHour, Region
;
create table want2 as
select
cum.Date
, cum.ObsAtHour
, cum.Region
, cum.SalesAtTier
, cum.CumSales
, cum.CumSales / Sum(cum.SalesAtTier) as fraction format=Percent7.2
from
want as cum
group by
cum.Date, cum.Region
order by
cum.Date, cum.ObsAtHour, cum.Region
;
I have two tables (T1 and T2).
shop land
------------------
1 F24UK UK
2 MDUK UK
3 RDAUK UK
4 EDOUK UK
5 RDUK UK
6 TIUK UK
shop land customertype
---------------------------------
1 RDUK GB B2C
2 RDUK GB B2C
3 MDUK GB B2C
4 MDUK GB B2C
I want to join over column 'land'. But broblem is in t2 i have GB instead of UK. How can i solve this issue optimally?
Thanks
You can use a CASE expression:
t1.land=CASE WHEN t2.land='GB' THEN 'UK' ELSE t2.land END
I have date column which i have to divide in 6 quarters and calculate count,ratio- A/Count, Avg(colC) by State. Date column i have converted to YYYY Q format. I was wondering if i can get results shown below. i am using oracle 11g. I am just trying to write a sql which can give me results in above format. I am able to Group results in quarter but unable to further subgroup it to show count,Ratio and Avg under each quarter. –
I have 2 tables that i need to use to get data below.
Table 1 Table 2
Customer_id St_Nme St_Cd Customer_id No_of_sales Time_spent Date
1 Alabama AL 1 4 4.5 01122012
2 California CA 2 7.5 9.33 03062012
Desired Output
Count-Count of sales
Ratio-Time_spent/Count of sales
Avg - Average of time spent
Q42012 Q32012 Q22012 Q12012 Q42011 Q32012
count Ratio Avg count Ratio Avg count Ratio Avg
State
Alabama 3 4.5 1.2 8 7.4 3.2 65 21.1 34.4
A.. 4 7.5 3.2 5 9.4 5.2 61 25.1 39.4
A.. 9 6.5 5.2 4 3.4 3.7 54 41.1 44.4
Boston
Cali..
Den..