I need to aggregate over upper level with filter on members from lower one.
It is for report that present sales in every year with filter on one specific month.
This is example for FoodMart database and Product Dimension with levels:
[Product Family]
[Product Department]
MDX:
WITH
MEMBER [Measures].[Unit Sales Calculated] AS
Aggregate(
Intersect(
Descendants(
[Product].CurrentMember,
[Product].[Product Department],
SELF
),
[Filtered Product Department]
),
[Measures].[Unit Sales]
)
SET [Filtered Product Department] AS
Except(
[Product].[Product Department].Members,
[Product].[Drink].[Alcoholic Beverages]
)
SELECT
NON EMPTY [Measures].[Unit Sales Calculated] ON COLUMNS,
NON EMPTY [Product].[Product Family].Members) ON ROWS
FROM [Sales]
This solution works but performance is rather poor.
Is there better way to write this query?
Related
Focused DB tables:
Task:
For given location ID and culture ID, get max(crop_yield.value) * culture_price.price (let's call this multiplication monetaryGain) grouped by year, so something like:
[
{
"year":2014,
"monetaryGain":...
},
{
"year":2015,
"monetaryGain":...
},
{
"year":2016,
"monetaryGain":...
},
...
]
Attempt:
SELECT cp.price * max(cy.value) AS monetaryGain, EXTRACT(YEAR FROM cy.date) AS year
FROM culture_price AS cp
JOIN culture AS c ON cp.id_culture = c.id
JOIN crop_yield AS cy ON cy.id_culture = c.id
WHERE c.id = :cultureId AND cy.id_location = :locationId AND cp.year = year
GROUP BY year
ORDER BY year
The problem:
"columns "cp.price", "cy.value" and "cy.date" must appear in the GROUP BY clause or be used in an aggregate function"
If I put these three columns in GROUP BY, I won't get expected result - It won't be grouped just by year obviously.
Does anyone have an idea on how to fix/write this query better in order to get task result?
Thanks in advance!
The fix
Rewrite monetaryGain to be:
max(cp.price * cy.value) AS monetaryGain
That way you will not be required to group by cp.price because it is not outputted as an group member, but used in aggregate.
Why?
When you write GROUP BY query you can output only columns that are in GROUP BY list and aggregate function values. Well this is expected - you expect single row per group, but you may have several distinct values for the field that is not in grouping column list.
For the same reason you can not use a non grouping column(-s) in arithmetic or any other (not aggregate) function because this would lead in several results for in single row - there would not be a way to display.
This is VERY loose explanation but I hope will help to grasp the concept.
Aliases in GROUP BY
Also you should not use aliases in GROUP BY. Use:
GROUP BY EXTRACT(YEAR FROM cy.date)
Using alias in GROUP BY is not allowed. This link might explain why: https://www.postgresql.org/message-id/7608.1259177709%40sss.pgh.pa.us
I have an table below which shows some data and I am trying to get the high value from each row.
Below is the data I have:
Table A
Once calculated it should looks like below in tableau
Table B
Notice that for Total, its not summing up from pers and bus instead its getting it highest value from total from table A and same concept is for Grand Total. Numbers which you see are balance.
I am able to get high value up to Total but its the grand total where I am struggling with. Below is my Calculation which I am using.
if
countd([Category]) = 1 then
sum({ FIXED [Group], [Category]: max(
{ FIXED [Group], [Category], [Date]: SUM([Balance])})})
ELSE
sum({ FIXED [Group]: max(
{ FIXED [Group], [Date]: SUM([Balance])})}
)
END
Step-1: For max of categories, use this calculation desired maximums
MAX({Fixed [MVRA Group], [Product Category], [Date Display]: ([Primary Measure])})
STEP-2: For max of subtotal, use desired sub-total maximum
MAX({Fixed [MVRA Group], [Date Display]: ([Primary Measure])})
https://drive.google.com/file/d/1zyjCDdG_QECrkFgY-UFm7WM2yN1xaHmZ/view?usp=sharing
I have the following problem.
I need all the products in a table and at the same time put the sum of the quantities sold of each product.
I need to see each product in the product table with the total sum of sales, according to the date range.
If there is no sales record in that range, it must be zero.
My query is as follows, but it doesn't work.
Product Table : sto_producto
Product sales movement table: sto_movdet
SELECT
sto_producto.pro_codprod AS cod_product,
sto_producto.pro_desc AS name_product,
sum(sto_movdet.mvd_cant) AS total_sale,
AVG(sto_movdet.mvd_costo) AS cost_product_sale
FROM sto_producto
INNER JOIN sto_movdet ON (sto_producto.pro_codprod = sto_movdet.mvd_codprod)
WHERE mvd_fecha BETWEEN '2020301' and '20200716'
GROUP BY pro_codprod, pro_desc
I expect a result similar to
cod_product name_product total_sale cost_product_sale
0004 mousered 45 $ 2.355
0071 pc laptop 0 $ 1.000
I'm creating a report for JasperServer in iReport. I am creating a report with the top 10 companies with most product downloads of the selected products. Users can select in JasperReports which products with a list (parameter {$P{ProductFormat}}).
My dimension for products has the following structure: [Product].[ProductFactory].[ProductType], so input for the parameter for example can be: [Product].[ProductCategory1].[Product1], [Product].[ProductCategory2].[Product2], ... I also want those products available as rows so I can use them as fields in iReport. (product in where is not an option)
SELECT
NON EMPTY {
[Measures].[Orders]
} ON COLUMNS,
NON EMPTY
Crossjoin({TopCount({[USER_COMPANY].[Company].Members}, 10, [Measures].[Orders])}, {$P{ProductFormat}})
ON ROWS
FROM [Products]
WHERE $P{DateFilter}
This query return the top 10 companies of all products with the data filtered per products. I want a top 10 companies of the selected products, but I can't get it to work with topcount and as a row.
This query against AdvWrks via SSMS seems to work ok:
WITH
SET [ProductFormat] AS
{
[Product].[Product Categories].[Category].[Bikes]
,[Product].[Product Categories].[Category].[Clothing]
,[Product].[Product Categories].[Category].[Components]
}
SELECT
[Measures].[Reseller Sales Amount] ON 0
,Generate
(
[ProductFormat]
,
[Product].[Product Categories].CurrentMember
*
TopCount
(
[Geography].[Geography].[City].MEMBERS
,5
,[Measures].[Reseller Sales Amount]
)
) ON 1
FROM [Adventure Works];
It returns the following:
If I add a WHERE clause like the following then it seems to be context aware:
WHERE [Date].[Calendar].[Calendar Year].&[2006];
I am new to the MDX queries. I am writing a MDX query to select a Measure value across months and I am putting date Range as filter here just to restrict no of Months returned. For eg I want Sales Revenue for each month in Date Range of 01-Jan-2014 to 30-Jun-2014. Ideally, it should give me sales value for six months i.e Jan, Feb, Mar, Apr, May and June. However when i write below query, I get error. PFB the below enter code here`ow query.
Select NON EMPTY {[Measures].[Target Plan Value]} ON COLUMNS,
NON EMPTY {[Realization Date].[Hierarchy].[Month Year].Members} ON ROWS
From [Cube_BCG_OLAP]
( { [Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231] })
The error I get is The Hierarchy hierarchy already appears in the Axis1 axis. Here Date and Month Year belong to same dimension table named as Realization Date. Please help me. Thanks in advance.
You were missing the WHERE clause but I guess that was a typo. As your error message tells, you can't have members of the same hierarchy on two or more axes. In situations like this, you can use something like below which in MDX terminology is called Subselect.
Select NON EMPTY {[Measures].[Target Plan Value]} ON COLUMNS,
NON EMPTY {[Realization Date].[Hierarchy].[Month Year].Members} ON ROWS
From (
SELECT
[Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231] ON COLUMNS
FROM [Cube_BCG_OLAP]
)
I like the exists function in this situation:
SELECT
NON EMPTY {[Measures].[Target Plan Value]}
ON COLUMNS,
NON EMPTY
EXISTS(
[Realization Date].[Hierarchy].[Month Year].Members
, {
[Realization Date].[Hierarchy].[Date].&[20140101] :
[Realization Date].[Hierarchy].[Date].&[20141231]
}
)
ON ROWS
FROM [Cube_BCG_OLAP]
Select
[Measures].[Target Plan Value]} On Columns
{
[Realization Date].[Hierarchy].[Date].&[20140101].Parent :
[Realization Date].[Hierarchy].[Date].&[20140631].Parent
}
On Rows
From [Cube_BCG_OLAP]
You need to create this same dimension only for filter in the cube, for example, dimension_filter -> hierarchy_filter -> level_filter