I was just wondering is there possible ways to display the data from 3d cube in a tabular format withoug slicing through MDX Query-
Suppose, I have a cube with 3 dimensions - Class(A,B,C,D..), Vendor(V1,V2,V3), Period(2010,2011) and the measure would be SaleValue..
Now, I would like to view the records like this..
**Class| Vendor|Period|SaleValue**
A | V1 | 2010 | 987
A | V2 | 2011 | 654
A | V3 | 2010 | 214
A | V1 | 2011 | 5643
A | V2 | 2010 | 698
A | V3 | 2011 | 212
B | V1 | 2010 | 224
B | V2 | 2011 | 668
B | V3 | 2010 | 741
B | V1 | 2011 | 3216
B | V2 | 2010 | 953
B | V3 | 2011 | 2114
C | V1 | 2010 | 159
C | V2 | 2011 | 852
C | V3 | 2010 | 369
C | V1 | 2011 | 147
C | V2 | 2010 | 123
C | V3 | 2011 | 654
Is this possible to implement using MDX queries ? If so, please tel me any of sample queries..
Thanks in advance..
You can write an Mdx query that will give something really close to a tabular format.
For instance:
SELECT {[Measures].[SaleValue]} ON COLUMNS,
NON EMPTY [Class].Members * [Vendor].Members * [Period].Members ON ROWS
FROM cube
The result will look like this:
| SaleValue
(A, V1, 2010) | 987
(A, V2, 2011) | 654
(A, V3, 2010) | 214
...
If you do not want to have sub totals, you can replace [Class].Members with lowest_level_of_Class.Members or use the leaves function.
This should help:
http://blogs.msdn.com/b/jamiemac/archive/2008/03/10/unwinding-mdx-flattening-semantics-with-dmx.aspx
Related
There is a table (SQL Server 2017) on sales of goods in stores, some records have no price.
+---------+-------------+---------+----------+-------+
| year_id | week_number | good_id | store_id | price |
+---------+-------------+---------+----------+-------+
| 2019 | 6 | 140629 | 2 | 199 |
+---------+-------------+---------+----------+-------+
| 2019 | 8 | 140629 | 2 | NULL |
+---------+-------------+---------+----------+-------+
| 2017 | 40 | 137233 | 9 | 278 |
+---------+-------------+---------+----------+-------+
| 2017 | 35 | 137233 | 9 | NULL |
+---------+-------------+---------+----------+-------+
| 2017 | 37 | 137233 | 9 | NULL |
+---------+-------------+---------+----------+-------+
We would like to replace the missing values according to the following scheme: set the price value to the same as the good with this number (good_id) from the same store (store_id), but sold as far as possible in the nearest to the missing value date, for example:
+---------+-------------+---------+----------+-------+
| year_id | week_number | good_id | store_id | price |
+---------+-------------+---------+----------+-------+
| 2019 | 6 | 140629 | 2 | 199 |
+---------+-------------+---------+----------+-------+
| 2019 | 8 | 140629 | 2 | 199 |
+---------+-------------+---------+----------+-------+
| 2017 | 40 | 137233 | 9 | 278 |
+---------+-------------+---------+----------+-------+
| 2017 | 35 | 137233 | 9 | 278 |
+---------+-------------+---------+----------+-------+
| 2017 | 37 | 137233 | 9 | 278 |
+---------+-------------+---------+----------+-------+
So far made something like this, but this query contains mutually exclusive conditions, so it does not affect the rows:
UPDATE dataset
SET price = p.price
FROM dataset AS p
WHERE good_id = p.good_id
AND store_id = p.store_id
AND price IS NULL
AND p.price IS NOT NULL;
GO
You can use apply. This works if all years have 52 weeks:
update d
set price = d2.price
from dataset d cross apply
(select top (1) d2.*
from dataset d2
where d2.good_id = d.good_id and
d2.store_id = d.store_id and
d2.price is not null
order by abs( (d2.year_id * 52 + d2.week_id) - (d.year_id * 52 + d.week_id) )
) d2
where d.price is null;
The only issue is when the comparisons pass the year boundary and the previous year has 53 weeks. Depending on how you define years, you can convert the year/week combos in to dates and use direct date comparisons for the difference.
I have an ACTIVE_TRANSPORTATION table:
+--------+----------+--------+
| ATN_ID | TYPE | LENGTH |
+--------+----------+--------+
| 1 | SIDEWALK | 20.6 |
| 2 | SIDEWALK | 30.1 |
| 3 | TRAIL | 15.9 |
| 4 | TRAIL | 40.4 |
| 5 | SIDEWALK | 35.2 |
| 6 | TRAIL | 50.5 |
+--------+----------+--------+
It is related to an INSPECTION table via the ATN_ID:
+---------+--------+------------------+
| INSP_ID | ATN_ID | LENGTH_INSPECTED |
+---------+--------+------------------+
| 101 | 2 | 15.2 |
| 102 | 3 | 5.4 |
| 103 | 5 | 15.9 |
| 104 | 6 | 20.1 |
+---------+--------+------------------+
I want to summarize the information like this:
+----------+--------+-------------------+
| TYPE | LENGTH | PERCENT_INSPECTED |
+----------+--------+-------------------+
| SIDEWALK | 85.9 | 36% |
| TRAIL | 106.8 | 23% |
+----------+--------+-------------------+
How can I do this within a single query?
Here is the updated answer using ACCESS 2010. Note that LENGTH is reserved in ACCESS, so it needs to be changed to LENGTH_
SELECT
TYPE,
SUM(LENGTH) as LENGTH_,
SUM(IIF(ISNULL(LENGTH_INSPECTED),0, LENGTH_INSPECTED))/SUM(LENGTH) as PERCENT_INSPECTED
FROM
ACTIVE_TRANSPORTATION A
LEFT JOIN INSPECTION B
ON A.ATN_ID = B.ATN_ID
GROUP BY TYPE
Here is the answer using T-SQL in SQL SERVER 2014 I had originally
SELECT SUM(LENGTH) as LENGTH,
SUM(ISNULL(LENGTH_INSPECTED,0))/SUM(LENGTH) as PERCENT_INSPECTED,
TYPE
FROM
ACTIVE_TRANSPORTATION A
LEFT JOIN INSPECTION B
ON A.ATN_ID = B.ATN_ID
GROUP BY TYPE
Let me know if you need it to be converted to percent, rounded, etc, but I'm guessing that part is easy for you.
I face the following problem in MS ACCESS 2010.
I have a (or many) table A that looks like this:
F1 | F2 | F3 | ... | F?
-------------------------------
| B005 | B005 | ... | B345
| C235 | C255 | ... | C235
A123 | 12.3 | 27.4 | ... | 87.0
A276 | 34.8 | 7.2 | ... | 42.0
... | ... | ... | ... | ...
A876 | 91.2 | 13.3 | ... | 0.1
So there a two rows at the top and one column to the left containing key values.
I need to achieve a representation of this data that looks like this (more normalized?):
K1 | K2 | K3 | Data
-------------------------
A123 | B005 | C235 | 12.3
A123 | B005 | B255 | 27.4
A123 | B345 | C235 | 87.0
A276 | B005 | C235 | 34.8
. . .
A876 | B345 | C235 | 0.1
Searching for a solution brought up UNPIVOT (which does not work with ACCESS?) and a number of UNIONs of SELECTs (to simulate UNPIVOT).
The problem, however, is that the total number of columns, nor their names, in table A is not known in advance (or not constant for different tables as input).
How can I deal with this?
Thanks for suggestions.
I am very new to Excel and have so far done a few basic pivot tables/charts.
I am trying to rearrange my source data to make a column chart where credits are shown in green and debits in red.
Here's my original data I get from a database. Please note there's 3 sets of values A, B, C.
Set1 A, B, C refers to Credits
Set2 A, B, C refers to debits
Set3 A, B, C is the difference between Set1 A - Set2 A and so on..
I need to make a chart like the one below, Set1 of A,B,Cs are the green columns, Set2 of A, B, Cs are the red columns. Set3 A, B, Cs is hte difference displayed on top.
How do I do this?
This is what I have done so far:
I hand edited and rearranged my original dataset to look like this: It was quite a bit of work to do this since I have a large amount of data.
Then I could make a chart out of this. But what I would like to know is if there's a better way to achieve this. Thanks much!
Update: Here's the data sample
+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| Year | Set1 A | Set1 B | Set1 C | Set2 A | Set2 B | Set2 C | Set3 A | Set3 B | Set3 C |
+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 2010 | 58 | 33 | 111 | 0 | 0 | 300 | 58 | 33 | -189 |
| 2011 | 56 | 33 | 112 | 0 | 0 | 300 | 56 | 33 | -188 |
| 2012 | 56 | 33 | 112 | 0 | 0 | 300 | 56 | 33 | -188 |
| 2013 | 56 | 33 | 112 | 0 | 0 | 300 | 56 | 33 | -188 |
| 2014 | 56 | 30 | 102 | 0 | 0 | 300 | 56 | 30 | -198 |
| 2015 | 134 | 0 | 0 | 190 | 0 | 60 | -56 | 0 | -60 |
+------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
If anyone is interested, I was able to rearrange data using the Excel add-in 'PowerQuery'. Powerquery is available for Excel 2010 Professional and beyond. It let me unpivot my table data and with a few clicks I got it to work. Thanks.
Ok, so i'm trying to write a complex query (at least complex to me) and need some pro help. This is my database setup:
Table: MakeList
| MakeListId | Make |
| 1 | Acura |
| 2 | Chevy |
| 3 | Pontiac |
| 4 | Scion |
| 5 | Toyota |
Table: CustomerMake
| CustomerMakeId | CustomerId | _Descriptor |
| 1 | 123 | Acura |
| 2 | 124 | Chevy |
| 3 | 125 | Pontiac |
| 4 | 126 | Scion |
| 5 | 127 | Toyota |
| 6 | 128 | Acura |
| 7 | 129 | Chevy |
| 8 | 130 | Pontiac |
| 9 | 131 | Scion |
| 10 | 132 | Toyota |
Table: Customer
| CustomerId | StatusId |
| 123 | 1 |
| 124 | 1 |
| 125 | 1 |
| 126 | 2 |
| 127 | 1 |
| 128 | 1 |
| 129 | 2 |
| 130 | 1 |
| 131 | 1 |
| 132 | 1 |
What i am trying to end up with is this...
Desired Result Set:
| Make | CustomerId|
| Acura | 123 |
| Chevy | 124 |
| Pontiac | 125 |
| Scion | 131 |
| Toyota | 127 |
I am wanting a list of unique Makes with one active (StatusId = 1) CustomerId to go with it. I'm assuming i'll have to do some GROUP BYs and JOINS but i haven't been able to figure it out. Any help would be greatly appreciated. Let me know if i haven't given enough info for my question. Thanks!
UPDATE: The script doesn't have to be performant - it will be used one time for testing purposes.
Something like this:
select cm._Descriptor,
min(cu.customerid)
from CustomerMake cm
join Customer cu on cuo.CustomerId = cm.CustomerId and cu.StatusId = 1
group by cm._Descriptor
I left out the MakeList table as it seems unnecessary because you are storing the full make name as _Descriptorin the CustomerMake table anyway (so the question is what is the MakeList table for? Why don't you store a FK to it in the CustomerMake table?)
You want to
(a) join the customer and customermake tables
(b) filter on customer.statusid
(c) group by customermake._descriptor
Depending on your RDBMS, you may need to explicitly apply a group function to customer.customerid to include it in the select list. Since you don't care which particular customerid is displayed, you could use MIN or MAX to just pick an essentially arbitrary value.