So first I have a typical table that looks like this:
SectionID
SectionName
DivisionID
DivisionName
IndustryID
IndustryName
SectorID1
SecNam1
DivisionID11
DivName11
IndustryID111
InduNam111
SectorID1
SecNam1
DivisionID11
DivName11
IndustryID112
InduNam112
SectorID1
SecNam1
DivisionID12
DivName12
IndustryID121
InduNam121
SectorID2
SecNam2
DivisionID21
DivName21
IndustryID211
InduNam211
SectorID3
SecNam3
DivisionID31
DivName31
IndustryID311
InduNam311
now I want to transform it in a more dynamic hierarchy structure with parentIds which looks like this:
ID
ParentID
Level
Type
Name
1
NULL
1
SectorID1
SecNam1
2
NULL
1
SectorID2
SecNam2
3
NULL
2
SectorID3
SecNam3
4
1
2
DivisionID11
DivName11
5
1
2
DivisionID12
DivName12
5
1
2
DivisionID21
DivName21
6
2
2
DivisionID31
DivName31
7
2
3
IndustryID
InduNam111
8
2
3
IndustryID
InduNam112
9
2
3
IndustryID
InduNam121
10
2
3
IndustryID
InduNam211
11
2
3
IndustryID
InduNam311
Is it okay to have multiple empty parentId's?
Is this transformation ok?
Having the parent for the root node as null is fine - though in some designs this is populated with the same value as the record itself rather than having to deal with nulls e.g.
ID
ParentID
Level
Type
Name
1
SecNam1
1
SectorID1
SecNam1
Also, having different versions of the same value in the TYPE column looks odd. I would have expected just "SectorID" not "SectorID1", "SectorID2", "SectorID3". The difference between different sectors is handled by the name of the sector e.g. like you have done with "Division".
Did you mean the level of "SectorID3" to be 2 rather than 1 - or is that just a typo?
Related
I want to generate some mock data in mockaroo.
The format of the data should be like this
Claim ID Claim subid
1 1
1 2
1 3
2 1
2 2
2 3
3 1
So, basically the claimdid column can have multiple subids. Is this possible?
I'm trying to join several tables, where one of the tables is acting as a
key-value store, and then after the joins find the maximum value in a
column less than another column. As a simplified example, I have the following three tables:
Documents:
DocumentID
Filename
LatestRevision
1
D1001.SLDDRW
18
2
P5002.SLDPRT
10
Variables:
VariableID
VariableName
1
DateReleased
2
Change
3
Description
VariableValues:
DocumentID
VariableID
Revision
Value
1
2
1
Created
1
3
1
Drawing
1
2
3
Changed Dimension
1
1
4
2021-02-01
1
2
11
Corrected typos
1
1
16
2021-02-25
2
3
1
Generic part
2
3
5
Screw
2
2
4
2021-02-24
I can use the LEFT JOIN/IS NULL thing to get the latest version of
variables relatively easily (see http://sqlfiddle.com/#!7/5982d/3/0).
What I want is the latest version of variables that are less than or equal
to a revision which has a DateReleased, for example:
DocumentID
Filename
Variable
Value
VariableRev
DateReleased
ReleasedRev
1
D1001.SLDDRW
Change
Changed Dimension
3
2021-02-01
4
1
D1001.SLDDRW
Description
Drawing
1
2021-02-01
4
1
D1001.SLDDRW
Description
Drawing
1
2021-02-25
16
1
D1001.SLDDRW
Change
Corrected Typos
11
2021-02-25
16
2
P5002.SLDPRT
Description
Generic Part
1
2021-02-24
4
How do I do this?
I figured this out. Add another JOIN at the start to add in another version of the VariableValues table selecting only the DateReleased variables, then make sure that all the VariableValues Revisions selected are less than this date released. I think the LEFT JOIN has to be added after this table.
The example at http://sqlfiddle.com/#!9/bd6068/3/0 shows this better.
I need your help to write query with select statement to pull only claim IDs which contains code from this range: 99213, 99214, 99215, 99217.
So my results should be claim ID 1 (all lines) and claim ID 3 (all lines). Since claim ID 2 has codes which are outside of the range, i do not want that in my results.
Claim id line # code
1 1 99213
1 2 99214
1 3 99215
1 4 99217
2 1 99213
2 2 89557
2 3 36415
3 1 99215
3 2 99217
Result should be like this
Claim id line # code
1 1 99213
1 2 99214
1 3 99215
1 4 99217
3 1 99215
3 2 99217
Use a subquery to isolate ClaimIDs that have Codes outside of your list of values. Then rule them out of the main query with a not in.
SELECT *
FROM Table
WHERE ClaimID NOT IN (
SELECT ClaimID FROM Table WHERE Code NOT IN (99213,99214,99215,99217)
);
I have 1 Virtual cube consists of 2 cubes.
Example of fact table of 1st cube.
id object_id time_id date_id state
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 0
5 11 4 1 0
6 11 7 1 1
7 10 8 1 0
8 11 5 1 0
9 10 7 1 1
10 10 9 1 2
Where State: 0 - Ok, 1 - Down, 2 - Unknown
For this cube I have one measure StateCount it should count States for each object_id.
Here for example we have such result:
for 10 : 3 times Ok , 2 times Down, 1 time Unknown
for 11 : 3 times Ok , 1 time Down
Second cube looks like this:
id object_id time_id date_id status
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 1
5 11 4 1 1
Where Status: 0 - out, 1 - in. I keep this in StatusDim.
In this table I keep records that should not be count. If object have status 1 that means that I have exclude it from count.
If we intersect these tables and use StateCount we will receive this result:
for 10 : 2 times Ok , 1 times Down, 1 time Unknown
for 11 : 2 times Ok , 1 time Down
As far as i know, i must use calculated member with IIF condition. Currently I'm trying something like this.
WITH MEMBER [Measures].[StateTimeCountDown] AS(
iif(
[StatusDimDown.DowntimeHierarchy].[DowntimeStatus].CurrentMember.MemberValue
<> "in"
, [Measures].[StateTimeCount]
, null )
)
The multidimensional way to do this would be to make attributes from your state and status columns (hopefully with user understandable members, i. e. using "Ok" and not "0"). Then, you can just use a normal count measure on the fact tables, and slice by these attributes. No need for complex calculation definitions.
Master Table
===========
ID NAME
1 A
2 B
3 C
4 D
5 E
Hierarchy table with multiple parents (Note that neither can be primary column due to duplicate values):
Relations Table
================
ChildID ParentID
3 1
3 2
4 3
4 2
5 4
Hierarchy becomes like (it might not be this linear always):
1 2
| |
3 3
| |
4 4
| |
5 5
For reporting purpose I need data in recursive hierarchy format so that I can drill down it. I'm not getting if I can get drill down feature from existing data itself (seems not doable as I can not create recursive parent-child relationship due to duplicate values).
Do you have any ideas? My goal is to finally use this structure as a dimension in SSAS which automatically gives drill down if a table has a self primary key-child key relationship.
Using your example data, I actually get a different tree...
Relations Table Tree
================ =======
ChildID ParentID 1 2
3 1 \ /|
3 2 3 |
4 3 \|
4 2 4
5 4 |
5
Do you actually want two independent trees? If that's the case, you could introduce an extra field such as a tree id...
Relations Table Tree1 Tree2
======================= ===== =====
TreeID ParentID ChildID
1 NULL 1 1 2
1 1 3 | |
1 3 4 3 3
1 4 5 | |
2 NULL 2 4 4
2 2 3 | |
2 3 4 5 5
2 4 5
Without some extra piece of information, you'll always have problems of branches splitting and merging without a very well formed set of constraints. For example, if you wanted two linear trees of 1-3-4-5 and 2-3-4-6, your current model would have this...
Relations Table Tree
================ =======
ParentID ChildID 1 2
1 3 \ /
2 3 3
3 4 |
4 5 4
4 6 / \
5 6
The problem you now have though, is that there are FOUR linear paths...
- 1-3-4-5
- 1-3-4-6
- 2-3-4-5
- 2-3-4-6
What may be required is for you to describe a real world situation, exactly what you want from it, and exactly what you don't want from it.
My typical experience is that, for reporting purposes, any node in a tree should only have one parent, but may have many children. This means that when climbing up a tree you only have one route, and when climbing down a tree the data separates into sub-nodes.
Having many parents and many children makes a web rather than a tree. Where you have multiple routes, no matter which direction you traverse the tree.