Get Hierarchial Data using LINQ - linq-to-sql

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;
}

Related

MariaDB / MySQL Combine two tables

I have 2 tables tnHeaders and tnData
tnHeaders
fnIDX
fnDESCRIPTION
1
h1
2
h2
3
h3
tnData
fnIDX
fnHEADER_IDX
fnDESCRIPTION
1
1
d1
2
1
d2
3
1
d3
4
2
d4
5
2
d5
6
2
d6
7
3
d7
8
3
d8
9
3
d9
and would like to produce this output
fnOUTPUT
h1
d1
d2
d3
h2
d4
d5
d6
h3
d7
d8
d9
I can do this in code no problem, but how can I do this in SQL? (Make the server work)
You need to use UNION ALL for the descriptions of both tables and sort the results in such a way that headers are on top of their data:
SELECT fnOUTPUT
FROM (
SELECT fnDESCRIPTION fnOUTPUT, fnIDX header_index, 1 is_header FROM tnHeaders
UNION ALL
SELECT fnDESCRIPTION, fnHEADER_IDX, 0 FROM tnData
) t
ORDER BY header_index, is_header DESC, fnOUTPUT;
See the demo.

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.

SSAS - Parent-Child Hierarchy with multiple types of Hierarchy

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.

Selecting values by last date

I have table with several entries for one point, is it possible to show only last entry for each point?
Example
points | date
A1 2016-02-12
A1 2016-02-15
A1 2016-03-12
B1 2016-01-11
B1 2016-03-15
B1 2015-09-28
C1 2016-01-28
C2 2016-03-03
D1 2015-12-12
D1 2016-01-12
E2 Null
E3 Null
F1 Null
I want to get something like this, without ignoring Null values.
points | date
A1 12.03.2016.
B1 12.03.2016.
C1 03.03.2016.
D1 12.01.2016.
E2 Null
E3 Null
F1 Null
I edited question because I tried group by and it didn't work for me( I forgot to mention before) It showed only entries with date, and I need points with null value as well
Something like this:
A1 12.03.2016.
B1 12.03.2016.
C1 03.03.2016.
D1 12.01.2016.
You can get by using MAX FUNTION
SELECT points,MAX(date)
FROM table_name
GROUP BY points;
if you want to change date format you can use DATE_FORMAT function

Improvement of VBA code in Access to do something like a pivot table

I have a table of data in MS Access 2007. There are 6 fields per record, thousands of records. I want to make a sort of pivot table like object. That is, if any two rows happens to be the same in the first 4 fields, then they will end up grouped together into one row. The column headers in this pivot table will be the values from the 5th field, and the value in the pivot table will be the 6th field, a dollar amount. Think of the 5th field as letters A, B, C, D, E, F, G. So, the table I start with might have a row with A in the 5th field and $3.48 in the 6th field. Another row may match in the first 4 fields, have B in the 5th field and $8.59 in the 6th field. Another may match in the first 4 fields, have E in the 5th field and $45.20 in the 6th field. I want all these rows to be turned into one row (in a new table) that starts with the first 4 fields where they match, then lists $3.48, $8.59, $0.00, $0.00, $45.20, $0.00, $0.00, corresponding to column headers A, B, C, D, E, F, G (since no records contained C, D, F, G, their corresponding values are $0.00), and then ends with one more field that totals up the money in that row.
Currently, I have some VBA code that does this, written by someone else a few years ago. It is extremely slow and I am hoping for a better way. I asked a previous question (but not very clearly so I was advised to create a new question), where I was asking if there was a better way to do this in VBA. My question asked about reading and writing large amounts of data all at once in Access through VBA, which I know is a good practice in Excel. That is, I was hoping to take my original table and just assign the entire thing to an array all at once (as in Excel, instead of cell by cell), then work with that array in VBA and create some new array and then write that entire array all at once to a new table (instead of record by record, field by field). From the answers in that question, it seems like that is not really a possibility in Access, but my best bet might be to use some sort of query. I tried the Query Wizard and found the Cross Tab query which is close to what I describe above. But, there appears to be a max of 3 fields used in the Row Heading, whereas here I have 4. And, instead of putting $0.00 when a value is not specified (like C, D, F, G in my example above), it just leaves a blank.
Update (in response to Remou's comment to give sample data): Here is some sample data.
ID a b c d e f
7 1 2 3 5 A 5
8 1 2 3 5 B 10
9 1 2 3 5 C 15
10 1 2 3 5 D 20
11 1 2 3 5 E 25
12 1 2 4 4 A 16
13 1 2 4 4 B 26
14 1 3 3 7 D 11
15 1 3 3 7 B 11
The result should be:
a b c d an bn cn dn en Total
1 2 3 5 5 10 15 20 25 75
1 2 4 4 16 26 0 0 0 42
1 3 3 7 0 11 0 11 0 22
But, when I copy and paste the SQL given by Remou, the only output I get is
a b c d an bn cn dn en
1 2 3 5 5 10 15 20 25
This is, I think, what you want, but it would be better to consider database design, because this is a spreadsheet-like solution.
SELECT t0.a,
t0.b,
t0.c,
t0.d,
Iif(Isnull([a1]), 0, [a1]) AS an,
Iif(Isnull([b1]), 0, [b1]) AS bn,
Iif(Isnull([c1]), 0, [c1]) AS cn,
Iif(Isnull([d1]), 0, [d1]) AS dn,
Iif(Isnull([e1]), 0, [e1]) AS en
FROM (((((SELECT DISTINCT t.a,
t.b,
t.c,
t.d
FROM table3 t) AS t0
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS a1
FROM table3 t
WHERE t.e = "A") AS a0
ON ( t0.d = a0.d )
AND ( t0.c = a0.c )
AND ( t0.b = a0.b )
AND ( t0.a = a0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS b1
FROM table3 t
WHERE t.e = "B") AS b0
ON ( t0.d = b0.d )
AND ( t0.c = b0.c )
AND ( t0.b = b0.b )
AND ( t0.a = b0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS c1
FROM table3 t
WHERE t.e = "C") AS c0
ON ( t0.d = c0.d )
AND ( t0.c = c0.c )
AND ( t0.b = c0.b )
AND ( t0.a = c0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS d1
FROM table3 t
WHERE t.e = "D") AS d0
ON ( t0.d = d0.d )
AND ( t0.c = d0.c )
AND ( t0.b = d0.b )
AND ( t0.a = d0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0
ON ( t0.d = e0.d )
AND ( t0.c = e0.c )
AND ( t0.b = e0.b )
AND ( t0.a = e0.a );
Table3
ID a b c d e f
1 1 2 3 4 a €10.00
2 1 2 3 4 b €10.00
3 1 2 3 4 c €10.00
4 1 2 3 4 d €10.00
5 1 2 3 4 e €10.00
6 1 2 3 5 a €10.00
7 1 2 3 5 b
8 1 2 3 5 c €10.00
9 1 2 3 5 d €10.00
10 1 2 3 5 e €10.00
Result
There are two rows, because there are only two different sets in the first four columns.
a b c d an bn cn dn en
1 2 3 4 €10.00 €10.00 €10.00 €10.00 €10.00
1 2 3 5 €10.00 €0.00 €10.00 €10.00 €10.00
The way the sql above is supposed to work, is that it selects each of the four definition columns and the currency column from the table where the sort column has a particular sort letter and labels the currency column with the sort letter, each of these sub queries are then assembled, however, you can take a sub query and look at the results. The last one is the part between the parentheses:
INNER JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0