I have data that have a hierachy level. If one leg of the hierarchy is shorter than the others, the last value must be carried down to the last level in my output.
Example: I have max 3 levels of data
AAB
/ \
AA B
/
A
I want to have output in which 'AA' and 'B' should be on the same level
but my output is giving me 'a' and 'b' on the same level.
Something like this
A-AA-AAB
B-AAB-NULL
But I want
A-AA-AAB
NULL-B-AAB
How to fill in the nulls for the missing nodes?
Related
I have 3 columns (DateTime, GroupName, Value), some of these groups are closely related and I would like to display these in a stacked graph. The problem I am facing (I THINK) is that I don't have entries for all groups at all times.
(cannot find a decent way to add a table, so here is some code)
Datetime Groupname Value
1 a whatever
1 b whatever
1 c whatever
2 a whatever
2 b whatever
3 a whatever
3 b whatever
3 c whatever
4 b whatever
So in the example I don't have an entry for C at time 2. And I also don't have an entry for A and B at time 4.
Resulting in:
edit: added to onedrive link
With my limited SQL skills I am not sure how to fix this. How do I get the graph to connect the points from the DateTime points where we do have data, and ignore DateTime points where we do not have data?
11-07-2016 Edit
Ok, so here some pictures of the actual data
No rep - Onedrive it is
https://1drv.ms/f/s!AhKMFQBAmZ7GgYEMzdpTBvuXTi5gAQ
the graph looks different than my first example because I set the X-axis to scalar.
On 7/5/2016 and 7/6/2016 (month/day/year notation) the CH3 is low and 0. If I remove CH3 from results the graph looks ok.
#sqlandmore.com
This is the query. Very basic.
Data is coming from a database were the datetime is not in a proper datetime format so thats gets converted into the wimsview.
SELECT
WimsView.TagID
,WimsView.SampleDateTime
,WimsView.SampleValue
,WimsView.TagName
FROM
WimsView
WHERE
WimsView.SampleDateTime > N'07/4/2016 00:00:00'
AND ((WimsView.TagName LIKE N'%MBA%') OR (WimsView.TagName LIKE N'%MBB%') OR (WimsView.TagName LIKE N'%GF1_DPC%')OR (WimsView.TagName LIKE N'%KF1_DPC%')OR (WimsView.TagName LIKE N'%CH3_DPC%'))
AND WimsView.SampleValue IS NOT NULL
You can't ignore specific values on the graph.
you can either change your select statement not to include them
you can calculate an "average" (if possible) for the missing value in order to fill the missing "points" in your graph
or another calculation (i.e - same value as previous one on the graph)
whatever you decide - it should be handled on a query level, not on a drwaing level
I have a SSAS DSV similar to following structure:
Id Type Special
1 A 1
2 B Null
3 A Null
4 C 1
5 C Null
I built a dimension for this DSV including one attribute for Type.
Then I have in my cube three measures
Measure1: Count of rows
Measure2A: Sum of Special
Measure2B: Count of non-empty values for Special
Finally in Excel, I display data as following:
Rows --> Type attribute
Values --> Measure1 / Measure2A / Measure2B
When I look at the results, everything is correct.
For instance, I get a count of 1 for measure2A and measure 2B for row = C
BUT when I attempt to drill through for related cells, instead of getting 1 row, I get 2 (the ones where type = C without considering the value of Special)
I guess I am doing something wrong in my design of the cube but cannot understand what.
When determining what rows to show in drillthrough SSAS only considers the dimension context not which detail rows have a non null measure value.
You could add a new dimension on the Special column and add that dimension as a filter to your PivotTable.
Or you could install ASSP and construct a custom rowset action that fires an MDX query which does a NON EMPTY on your measure.
http://asstoredprocedures.codeplex.com/wikipage?title=Drillthrough&referringTitle=Home
I have created a view in my SQL Server database which will give me number of columns.
One of the column heading is Priority and the values in this column are Low, Medium, High and Immediate.
When I execute this view, the result is returned perfectly like below. I want to change or assign values for these priorities. For example: instead of Low I should get 4, instead of Medium I should get 3, for High it should be 2 and for Immediate it should be 1.
What should I do to achieve this?
Ticket# Priority
123 Low
1254 Low
5478 Medium
4585 High
etc., etc.,
Use CASE:
Instead of Low I should get 4, instead of Medium I should get 3, for
High it should be 2 and for Immediate it should be 1
SELECT
[Ticket#],
[Priority] = CASE Priority
WHEN 'Low' THEN 4
WHEN 'Medium' THEN 3
WHEN 'High' THEN 2
WHEN 'Immediate' THEN 1
ELSE NULL
END
FROM table_name;
EDIT:
If you use dictionary table like in George Botros Solution you need to remember about:
1) Maintaining and storing dictionary table
2) Adding UNIUQE index to Priority.Name to avoid duplicates like:
Priority table
--------------------
Id | Name | Value
--------------------
1 | Low | 4
2 | Low | 4
...
3) Instead of INNER JOIN defensively you ought to use LEFT JOIN to get all results even if there is no corresponding value in dictionary table.
I have an alternative solution for your problem by creating a new Priority table (Id, Name, Value)
by joining to this table you will be able to select the value column
SELECT Ticket.*, Priority.Value
FROM Ticket INNER JOIN Priority
ON Priority.Name = Ticket.Priority
Note: although using the case keyword is the most straight forward solution for
this problem
this solution may be useful if you will need this priority value in many places at your system
I just started out using Informatica and currently I am figuring out how to get this to a target output (flat file to Microsoft SSIS):
ID Letter Parent_ID
---- ------ ---------
1 A NULL
2 B 1
3 C 1
4 D 2
5 E 2
6 F 3
7 G 3
8 H 4
9 I 4
From (assuming that this is a comma-delimited flat file):
c1,c2,c3,c4
A,B,D,H
A,B,D,I
A,B,E
A,C,F
A,C,G
EDIT: Where c1 c2 c3 and c4 being a header.
EDIT: A more descriptive representation of what I want to acheive:
EDIT: Here is what I have so far (Normalizer for achieving the letter column and Sequence Generator for ID)
Thanks in advance.
I'd go with a two-phased approach. Here's the general idea (not a full, step-by-step solution).
Perform pivot to get all values in separate rows (eg. from "A,B,D,H" do a substring and union the data to get four rows)
Perform sort with distinct and insert into target to get IDs assigned. End of mapping one.
In mapping two add a Sequence to add row numbers
Do the pivot again
Use expression variable to refer previous row and previous RowID (How do I get previous row?)
If current RowID doesn't match previous RowID, this is a top node and has no parent.
If previous row exists and the RowID is matching, previous row is a parent. Perform a lookup to get it's ID from DB and use as Parent_ID. Send update to DB.
I am working on a tag recommendation system that takes metadata strings (e.g. text descriptions) of an object, and splits it into 1-, 2- and 3-grams.
The data for this system is kept in 3 tables:
The "object" table (e.g. what is being described),
The "token" table, filled with all 1-, 2- and 3-grams found (examples below), and
The "mapping" table, which maintains associations between (1) and (2), as well as a frequency count for these occurrences.
I am therefore able to construct a table via a LEFT JOIN, that looks somewhat like this:
SELECT mapping.object_id, mapping.token_id, mapping.freq, token.token_size, token.token
FROM mapping LEFT JOIN
token
ON (mapping.token_id = token.id)
WHERE mapping.object_id = 1;
object_id token_id freq token_size token
+-----------+----------+------+------------+--------------
1 1 1 2 'a big'
1 2 1 1 'a'
1 3 1 1 'big'
1 4 2 3 'a big slice'
1 5 1 1 'slice'
1 6 3 2 'big slice'
Now I'd like to be able to get the relative probability of each term within the context of a single object ID, so that I can sort them by probability, and see which terms are most probably (e.g. ORDER BY rel_prob DESC LIMIT 25)
For each row, I'm envisioning the addition of a column which gives the result of freq/sum of all freqs for that given token_size. In the case of 'a big', for instance, that would be 1/(1+3) = 0.25. For 'a', that's 1/3 = 0.333, etc.
I can't, for the life of me, figure out how to do this. Any help is greatly appreciated!
If I understood your problem, here's the query you need
select
m.object_id, m.token_id, m.freq,
t.token_size, t.token,
cast(m.freq as decimal(29, 10)) / sum(m.freq) over (partition by t.token_size, m.object_id)
from mapping as m
left outer join token on m.token_id = t.id
where m.object_id = 1;
sql fiddle example
hope that helps