SSAS - Parent-Child Hierarchy with multiple types of Hierarchy - many-to-many

My data is as follows:
Employee Table:
EmployeeID EmployeeName Departement
A Michael Jackson D1
B Amy Winehouse D3
C Brad Pitt D3
D Name1 D2
E Name2 D2
F Name3 D1
Departement Table:
DepartementID DepartementParentID DepartementName DepCountry
D1 D1 Name1 USA
D2 D1 Name2 USA
D3 D2 Name3 USA
D2 D2 Name1 Tunisia
D1 D2 Name2 Tunisia
D3 D1 Name3 Tunisia
Fact Table:
EmployeeID Value
A 10
B 50
C 30
D 40
E 120
F 600
Now as you can see in the Departement table, there are two types of hierarchy: Tunisia and USA.
The root member in Tunisia is D2 and while in USA it's D1.
What I want to have in my SSAS Cube (Excel) is something like the "Desired Result" table from the image in the link "Results" below.
This is a perfect case for a Many to Many relationship like explained in the the next link. http://geekswithblogs.net/darrengosbell/articles/57811.aspx
I have tried this, the only issue is that my employee is not grouped under the hierarchy, I'm getting a result like the "Real Result" table from the link "Results"
Link:
Results
Does anyone have an idea why this is not working? Or if not, any other ideas to get my desired result?
Any help will be much appreciated.
Thanks in advance.

Related

How to create new column and new row based on two tables?

I have two tables:
Table 1
MARKET ATC3 ATC4 PRODUCT BOOLEAN FLAG JOINING COLUMN
A1 B1 B1C1 D1 1 ATC4
A2 B1 B1C2 D2 1 ATC4
A2 B1 B1C3 ATC4
FAMILY A B1 ATC3
Table 2:
PRODUCT ATC3 ATC4 VALUES
D1 B1 B1C1 10
D1 B1 B1C1 20
D2 B1 B1C2 15
D2 B1 B1C2 25
D2 B1 B1C2 10
D3 B1 B1C3 5
My desired output:
PRODUCT ATC3 ATC4 VALUES MARKET VALUES
D1 B1 B1C1 10 A1 10
D1 B1 B1C1 20 A1 20
D2 B1 B1C2 15 A2 15
D2 B1 B1C2 25 A2 25
D2 B1 B1C2 10 A2 10
D3 B1 B1C3 5 A2 5
ALL D1+D2+D3 FAMILY A 85
The idea is, Table 2 has many rows and products but does not have Market. Table 1 helps you find out which product in Table 2 belongs to which Market-based on the Joining column. For example, There are 3 Markets present in Table 1, I want to then assign a new column Market in Table 2 such that all PRODUCTS in Table 2 with the ATC4 code of B1C1 belongs to the Market A1. Why? Because in Table 1, it says that Market A1 should follow the Joining Column of ATC4 - which corresponds to the code B1C1. In Table 1, we also provided a Product column, this is just for our purpose of identifying our own companies product name. Now if you see that for Table 1, there are two rows of Market A2, with different ATC4, this is very normal, because maybe Product D2 and D10 belong to Market A2, but both may contain different ATC4!
There is also one more nuance to it, we have Family A! This is merely a combination of A1+A2, but in my Table 2, there is no such row value that sums up to Family A. So I need to achieve two things:
I want to make a new column Market in Table 2 so that each product is mapped to the market.
I want to create extra rows to account for the Market Family A (A1+A2) and call the product Name "Lovely Family A" or something. The above table 3 provides an expected output.
Since I am new to SQL, I tried to first use CASE Statements, to map slowly one by one, but soon it gets tedious and I wonder if there's some tricks.
My CASE looks like this
,CASE WHEN ATC4 LIKE '%B1C1%' THEN 'A1'
WHEN ATC4 LIKE '%B1C2%' OR ATC4 LIKE '%B1C3%' THEN 'A2' ELSE ATC4 END AS MARKET_NAME
but have yet to figure out how to add the additional row where I can sum up A1+A2.
You seem to want something like this:
with rows as (
select PRODUCT, ATC3, ATC4, VALUES
from table2
union all
select 'ALL D1+D2+D3', ATC3, NULL, SUM(VALUES)
from table2
group by ATC3
)
select r.*, t1.market
from rows r join
table1 t1
on (t1.joining_column = 'ATC3' and t1.atc3 = r.atc3) or
(t1.joining_column = 'ATC4' and t1.atc4 = r.atc4);
I see no reason to repeat the values column. And values is a really bad name for a column because it is a SQL keyword.

Join table A into table B multiple times

I have 2 tables in a mysql db (simplified example data):
Table 1: 3 names (unique)
Table 2: names (only from table 1), 3 dates, info
For each date there are sometimes all 3 names in table 2, sometimes less then 3.
Example for table 2:
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n3 info
d3 n3 info
Date 1 has got all 3 names, date 2 has got 2 names, date 3 hast got 1 name.
Goal: I need each "date" to have all 3 names. I can already filter 1 date from table 2 and join the tables successfully to have the desired outcome for one date, but how can I "add" all names to each date?
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n2 (added by the join, "info" is empty)
d2 n3 info
d3 n1 (added by the join, "info" is empty)
d3 n2 (added by the join, "info" is empty)
d3 n3 info
My real data has got much more names and dates, which makes individual joins impractical. It feels like there should be an easy solution to this, but I could not find any.
I also could do this with code ("for each date add missing names") , but I wonder if it can be done with sql
Working on MySQL 10.1.37
Use CROSS JOIN to create all possible combinations of names and dates, followed by a LEFT JOIN:
SELECT name_table.name, datelist.date, info_table.info
FROM name_table
CROSS JOIN (SELECT DISTINCT date FROM info_table) AS datelist
LEFT JOIN info_table ON name_table.name = info_table.name AND datelist.date = info_table.date

Get Hierarchial Data using LINQ

ID Name Designation PID
1 E1 D1 0
2 E2 D2 0
3 E3 D3 1
4 E4 D3 1
5 E5 D4 3
6 E6 D4 3
7 E7 D4 2
8 E8 D4 2
How can we get all the child employees based on the parent employee using LINQ ?
For eg., if we want child records for employee E1 we should get E3,E4,E5,E6
Thanks in advance...
You can't do it using one single LINQ query. But you can do it using a recursive function smth like:
IList<Employee> GetAllChildren(IList<Employee> employees, int pid)
{
var children = employees.Where (e => e.PID == pid).ToList();
children.AddRange (children.SelectMany (e => GetAllChildren (employees, e.ID)).ToList());
return children;
}

Trying to sum and group two sets of results from three SQL tables

I am having some major difficulties with grouping and summing results of a query in the way I want. I am having trouble explaining what I want to do in words, so I'll just show you. I have three tables: A, H, and W which look like (simplified):
Table A:
Aid name
1 adam
2 bob
Table H:
Hid Wid date atk Aid
1 1 - 10 2
2 2 - 1 1
3 2 - 5 1
4 1 - 2 2
5 1 - 22 1
6 2 - 7 2
Table W:
Wid name user pass
1 charlie - -
2 donald - -
I am trying to get the SUM of atk grouped by Aid and Wid. Basically, assume this is a fight club tally. I want to display the sum of how many times person W attacked person A (it will always be a one directional fight, ie: charlie can only attack adam, but adam can't attack charlie). (not really a fight club - being used for an online game :))
I am trying to get my result to look like:
name1 atk name2
charlie 22 adam
charlie 12 adam
donald 6 bob
donald 7 bob
My current query looks like...
SELECT w.name AS name1, h.atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
...which gives me every instance that name1 attacked name2. When I try to GROUP BY and/or SUM(h.atk) it is grouping or summing in a way I can't figure out. I'm just not understanding how to accomplish this. Any help would be greatly appreciated.
Thanks in advance!
SELECT w.name AS name1, sum(h.atk) as atk, a.name AS name2
FROM H
JOIN W ON w.Wid=h.Wid
JOIN A ON a.Aid=h.Aid
GROUP BY w.name, a.name

How to resolve a category from a property table in SQL

By way of an example, lets say I have the following (simplified) table (called NumericValue):
Age Gender Occupation Location Value
40 M Accountant Johannesburg 100
40 F Accountant Johannesburg 120
40 M Engineer NULL 110
40 F Engineer NULL 110
Now suppose I have this table called Employees:
Employee Number Age Gender Occupation Location
1000123 40 F Engineer Cape Town
1000124 40 M Accountant Johannesburg
Now, what I need is to select the "value" field for these two employees. And let's say that engineers will never ever have a "location" in the NumericValue table, so I can't just do a simple join. In stead, I reformat my "NumericTable" as follows:
Table: "CategoryValue"
Category Value
1 100
2 120
3 110
4 110
With a "property" table like this:
Table: "CategoryProperty"
Category FieldName FieldValue
1 Age 40
1 Gender M
1 Occupation Accountant
1 Location Johannesburg
.
.
4 Age 40
4 Gender F
4 Occupation Engineer
(note, no entry for "location" under category 4, which refers to the 40 year old female engineer)
Which makes sense to me, since I only have entries where a specific categorisation field is of importance. But how do I resolve this and extract the Value field for the specific employee?
Thanks
Karl
Why don't you do something like this?
Select e.EmployeeNumber,
e.Age,
e.Gender,
e.Occupation,
e.Location,
nv.Value
From Employees e
Join NumericValue nv on e.Age = nv.Age
And e.Gender = nv.Gender
And e.Occupation = nv.Occupation
And e.Location = IsNull(nv.Location, e.Location)
Just replace the NumericValue Location with the value of the Employee Location when it is NULL