Count Only if Unique ID Occurs for Multiple Departments? - tableau-api

Let's say I have some data along the lines of:
Department Location | Product Sale ID
New York ID-1
New York ID-1
New York ID-2
New York ID-2
California ID-1
California ID-1
California ID-3
Florida ID-3
Florida ID-4
Florida ID-5
I want to create a new view so that it counts the distinct number of times there is an overlap such the results are:
Department Location | Distinct ID Overlap Count
New York 1
California 2
Florida 1
In this case, New York has an ID overlap with California on ID-1. California has an overlap with New York on ID-1 and Florida on ID-3. Florida, conversely, only has the ID overlap with California on ID-3.
I've looked into doing a LOD calculation along the lines of:
{fixed [Department Location]:countd[Product Sale ID]}
But I'm not sure how to really to extract the results I want from here. I'm having trouble thinking of how to approach this logically and am wondering if it is possible or can only be done on the data source side?

You were on the right track with FIXED. But what we really care about is which Product Sale IDs have multiple locations. You can calculate that with a very straightforward LOD expression:
{ FIXED [Product Sale ID] : COUNTD([Department Location]) } > 1
With that information now available to us, you just need to count, for each Product Location, the number of distinct Product Sale IDs that occur in multiple locations. Here's one way to do that:
COUNTD(
IIF(
{ FIXED [Product Sale ID] : COUNTD([Department Location]) } > 1,
[Product Sale ID],
NULL
)
)

Related

Postgres Retrieve Values Conditionally?

not sure if my title is exactly waht I want to accomplish but it was a guess. I'm trying to see if something like the following is possible in PGSQL.
Basically, I have a set of data that I want to pull no matter what. But, if certain columns of data arent null, then I want it to join another table and pull data referencing that data. So, for example, let's say I have data like:
User_Accounts:
userid
companyname
first name
25df
Test Company 1
Bob
921f
Test Company 1
Lawrence
882s
Company test 2
NULL
8234
Test Company 1
Cleo
8244s
Company test 2
Paul
825q
Test Company 1
Bruce
5552
B Alpha Company
Lenny
Baseball_Rosters:
userid
baseball_team
company
position
25df
Atlanta Aliens
Test Company 1
P
882s
Boston Bruisers
Company test 2
DH
8234
California Craisins
Test Company 1
1B
8244s
Tampa Titans
Company test 2
P
null
Tampa Titans
Test Company 1
P
5552
Tampa Titans
B Alpha Company
2B
5552
Tampa Titans
B Alpha Company
SS
921f
California Craisins
Test Company 1
P
825q
Boston Bruisers
Test Company 1
P
How would I perform a query to get a result like:
userid
baseball_team
company
first name
position
null
Tampa Titans
Test Company 1
null
P
25df
Atlanta Aliens
Test Company 1
Bob
P
825q
Boston Bruisers
Test Company 1
Bruce
P
921f
California Craisins
Test Company 1
Lawrence
P
So essentially, inserting the firstname & lastname field if and only if userid is not null? And then to order by userid null first and then in order? Thanks for the help.
This looks like a LEFT OUTER JOIN.
Like this:
SELECT baseball_rosters.user_id, baseball_team, comapny, first_name, position
FROM baseball_rosters
LEFT OUTER JOIN user_accounts ON user_accounts.user_id = baseball_rosters.user_id;

SQL Subquery for each

I have following tables
create table players
(
name varchar(30) not null primary key,
);
create table injuries
bId int not null primarykey,
date DATE not null,
name varchar(30),
foreign key(name) references players
);
create table sportsBegins
(
cId int not null primarykey,
date DATE,
sportname varchar(20),
name varchar(30)
foreign key(name) references players
);
Following example data:
players
name
John
Jane
George
shows players in db
sportsBegins
cId | date | sportname | name
1 2020-01-01 Basketball John
2 2020-02-02 Basketball John
3 2020-01-01 Soccer John
4 2020-02-02 Basketball Jane
5 2020-01-03 Basketball George
6 2020-01-04 Badminton George
shows what date players begin playing a sport
injuries
bId | date | name
1 2020-01-01 John
2 2020-02-03 Jane
3 2020-01-05 George
shows the date these players reported injuries.
I want to count the number of DISTINCT players that have experienced an injury in Basketball AFTER the first day they got assigned the sport (not the same day).
So for each player, i need to only grab the first date they started playing basketball. Then for that player, i need to compate his name AND date to the name AND date in the injuries table to see if he ever reported an injury after the date he got the sport assigned.
Example
In the example data I provided this would be the output
Total basketball injuries
2
Explanation of answer
John got assigned basketball twice. Only look at first date he got assigned basketball. Then look at injuries table. He only reported an injury on that day, but never after, so ignore. Jane and George reported injuries after first day assigned basketball so count them
This should get you the desired result
SELECT count(distinct injuries.name)
FROM injuries
INNER JOIN (SELECT name, min(date) as startDate FROM sportsBegins WHERE sportname = 'Basketball' GROUP BY name) as startDates ON injuries.name=startDates.name and injuries.date > startDates.startDate
Quick explanation:
startDates extracts the first date each player started playing basketball
the join condition filters only injuries which happened after the first start date for each player
count(distinct injuries.name) ensures each player only gets counted once even if he/she reported more than one injury after the first start date

How do I produce a report to show the number of occurrences an employee has been absent from work

I have been asked to generate a report to show the number of occurrences an employee is absent from work sick.
If an employee is absent from work for 3 consecutive days this will be counted as 1 occurrence. If they then return to work and are then absent again for another 2 consecutive days this will be recorded as 2 occurrences.
I need to generate a report to show the number of occurrences an employee is away from work sick within a 6 month period.
I have set out an example below of the data showing an employee's absence records and how i need the report to look.
How data shows in database:
enter image description here
Name Absence Dates
John Smith 01-Sep-19
John Smith 02-Sep-19
John Smith 03-Sep-19
John Smith 10-Sep-19
John Smith 11-Sep-19
How i wish for the report to look:
Name Occurrences
John Smith 2
I would be grateful for any assistance with writing to code to achieve this result.
Not a full answer, as you should really do some of this yourself, however, based on what you have detailed in your quesiton, you could use the approach below to count up any spells of absence, within a 6 month period.
Assumes you would be compiling this using SQL Server
declare #absences table (empid nvarchar(10), [abs date] date, [ret date] date);
declare #staff table ([empid] int, [name1] nvarchar(50), [name2] nvarchar(50), [surname] nvarchar(50));
-- put some test values in the staff table to work with
insert into #staff
values
(1, 'John', 'Lewis', 'Smith'), -- using a unique ID here, in any good system this should be an incremental number for each new staff member added to the table
(2, 'James', 'Thomas', 'Brown')
-- put some test values in the absences table to work with
insert into #absences
values
(1, '2019-07-01', '2019-07-04'), -- userid, absence date & return date
(1, '2019-08-04', '2019-08-06'),
(2, '2019-07-02', '2019-07-05'),
(2, '2019-08-05', '2019-08-07')
select count(*) spellsoff, empid, name1, name2, surname, [days absent]
from
(
select
s.empid,
s.name1,
s.name2,
s.surname,
a.[abs date],
a.[ret date],
datediff(d,a.[abs date], a.[ret date]) [days absent]
from #staff s
left join #absences a
on s.empid = a.empid
where [abs date] >= DATEADD(M,-6,GETDATE()) -- pull back those employeess that have been absent in the last 6 months from today's date
)doff
group by empid, name1, name2, surname, [days absent]
Gives you the following breakdown:
spellsoff empid name1 name2 surname days absent
1 1 John Lewis Smith 2
1 1 John Lewis Smith 3
1 2 James Thomas Brown 2
1 2 James Thomas Brown 3

Select a specific row from a table with duplicated entries based on one field

I have a table which holds data in the following format, however I would like to be able to create a query that checks whether the reference number is duplicated and only return the entry with the latest date_issued.
ref_no name gender place date_issued
xgb/358632/p John Smith M London 02.08.2016
Xgb/358632/p John Smith M London 14.06.2017
Rtu/638932/k Jane Doe F Birmingham 04.09.2017
The result from the query should be;
ref_no name gender place date_issued
Xgb/358632/p John Smith M London 14.06.2017
Rtu/638932/k Jane Doe F Birmingham 04.09.2017
Is there a fairly straightforward solution for this?
assuming the date column is type date or timestamp
select distinct on(ref_no) * from tablename order by refno,date desc;
this works beacuse distinct on supresses rows with duplicates of the expression in parenthese.

How to calculate market share in tableau

I have a data set for 10 countries. Each country has more than 8 products and my company has 3 products A,B,C which are sold in each of the country.
So Now, I want to calculate the market share for my products country wise.
for E.g. If total sale for 8 products in country 1 is 100 and sale for products A+B+C is 35, then market share of my company in country1 is 35/100= 35%
.
Please help !
It depends a bit on what you want to do with that afterwards, if you are just interested in the number, do the following:
Create a calculated field TotalSalesPerCountry with {fixed [country]: sum([Sales])} (calculates the total sales per country)
Create a calculated field CompanySalesPerCountry with {fixed [country]: sum(IIF([Product] = 'A' OR [Product] = 'B' OR [Product] = 'C',[Sales],0))} (calculates the total sum per country where [PRODUCT] = A, B or C)
Create a calculated field MarketShare with AVG([CompanySalesPerCountry]) / AVG([TotalSalesPerCountry])
Change the Properties for the last field to 'Percent', drag [Country] to rows and [MarketShare] to columns