No idea of sql to mdx - mdx

I have no idea how to do it in mdx as I have only worked in sql but how do I translate this query from sql to mdx?
select monthname, sum(sales) sales
from table
where dt between 20190315 and 20220204
group by monthname;
Thank you for your attention and help.

Be careful of assuming that SQL and MDX can be translated back and forth. The nature of multi-dimensional databases means that the basic concepts which apply to the two query languages are very different.
Sometimes you can get similar answers, of course. In your case, the following MDX would give the sort of thing you were after...
SELECT
{[All Time].[2019].[March]:[All Time].[2022].[February]} ON ROWS,
{[Measures].[Sales] ON COLUMNS
FROM [Cubename]
... although this would look at 20190301 to 20220228 which is not exactly what your SQL query did.

Related

Grouping by "on the fly" calculation in Postgres

Very simple question here, but a quick google search didn't seem to be definitive (and I do not have access to a DB to test right now). I would like to check whether you can do "on the fly" grouping in Postgres (as is possible in SQL Server). best way to clarify is an example i.e. can I do this to group by weekly periods:
select ...
from ...
group by cast((current_date - transaction_date)/7 as int)
or is it necessary to first define a week column in a subquery (as per the calculation above) and then do the grouping?
Thanks in advance for your help.
You can include most expressions in the GROUP BY, so your code is fine. This is true in Postgres and in almost any database.
It is unusual to have an aggregation query where the aggregation expressions are not part of the GROUP BY. But if you have data on every day, then this is a sensible query:
select min(date_trunc(transaction_date)) as week_start, count(*)
from ...
group by cast((current_date - transaction_date)/7 as int)
You surely can. I would slightly modify your example like
select cast((current_date - transaction_date)/7 as int) as wp, ...
from ...
group by wp;

Ignore repeated days values using MDX

How can i ignore repeated days values using MDX ?
For example, from this:
12/1/1997
12/2/1997
12/3/1998
12/4/1999
To this:
12
I'm already tried use Distinct, but i'ts like they still considere as unique dates.
SELECT
{} ON COLUMNS,
DISTINCT([Time].[Weekly].[Day].Members) ON ROWS FROM [Sales 2]
These are different values and your cube treats them as such. I guess you can do some mimicing of that behavior with MDX by creating a MEMBER which has just the date part and doing distinct on that member. Something like:
WITH MEMBER [MonthPart] AS LEFT([Time].[Weekly].[Day].currentMember, 2)
SELECT
{} ON COLUMNS,
DISTINCT([MonthPart].Members) ON ROWS FROM [Sales 2]
Something like that but I am not sure for the correct syntax.
The proper way to fix that though is to create a good Time Dimension for your dates in the cube. There you can define hierarchies, levels and just take the [Month] part which will give you what you want (and will be much faster than writing complex MDX queries)

Nesting Queries in Access SQL

I have recently started using SQL and am stuck when applying it to Access. My previous experience (limited) has been with PostgreSQL, and I understand that SQL in Microsoft Access requires you to nest queries into sub-queries, which I am not familiar with.
I believe the code in SQL (not for access would look something like this...)
select weeks, sum(sweets_eaten), count(distinct (sweet))
from table
group by weeks;
This would then give me a table where I would have the unique weeks, the sum of sweets eaten per week and the number of sweets per week.
Ideally, what the query would then do is also tell me the average sweets eaten per week by dividing the total sweets eaten per week by the number of sweets.
Does anyone know how to write a query so this will work in Microsoft Access?
Thanks!
Edited code, this is what I am entering
select f15, sum(f16), count(*)
from (select f15, sum(f16) as sum_sweets_eaten
from table1
group by f15, f16
) as t
group by f15;
For the average, would it be possible to do this in addition to the sum.
The query you have written will not work in MS Access, because it does not support count(distinct).
You can pre-aggregate to get the result you want:
select weeks, sum(sum_sweets_eaten), count(*)
from (select weeks, sum(sweets_eaten) as sum_sweets_eaten
from table
group by weeks, sweet
) as t
group by weeks;
To get the average, use avg() rather than sum().

SQL Statement - want daily dates rolled up and displayed as Year

I have two years worth of data that I'm summing up for instance
Date | Ingredient_cost_Amount| Cost_Share_amount |
I'm looking at two years worth of data for 2012 and 2013,
I want to roll up all the totals so I have only two rows, one row for 2012 and one row for 2013. How do I write a SQL statement that will look at the dates but display only the 4 digit year vs 8 digit daily date. I suspect the sum piece of it will be taken care of by summing those columns withe calculations, so I'm really looking for help in how to tranpose a daily date to a 4 digit year.
Help is greatly appreciated.
select DATEPART(year,[Date]) [Year]
, sum(Ingredient_cost_Amount) Total
from #table
group by DATEPART(year,[Date])
Define a range/grouping table.
Something similar to the following should work in most RDBMSs:
SELECT Grouping.id, SUM(Ingredient.ingredient_cost_amount) AS Ingredient_Cost_Amount,
SUM(Ingredient.cost_share_amount) AS Cost_Share_Amount
FROM (VALUES (2013, DATE('2013-01-01'), DATE('2014-01-01')),
(2012, DATE('2012-01-01'), DATE('2013-01-01'))) Grouping(id, gStart, gEnd)
JOIN Ingredient
ON Ingredient.date >= Grouping.gStart
AND Ingredient.date < Grouping.gEnd
GROUP BY Grouping.id
(DATE() and related conversion functions are heavily DB dependent. Some RDBMSs don't support using VALUES this way, although there are other ways to create the virtual grouping table)
See this blog post for why I used an exclusive upper bound for the range.
Using a range table this way will potentially allow the db to use indices to help with the aggregation. How much this helps depends on a bunch of other factors, like the specific RDBMS used.

Pentaho CDF - MDX query: Showing data between months (parameters)

I have two parameters: 'from month' and 'to month'. I would like to show data between those months. This is my situation:
with member [Measures].[Ordercount Y-1] as '([Year].PrevMember, [Measures].[Ordercount])'
member [Measures].[Growth] as IIF([Measures].[Ordercount Y-1] >0,
[Measures].[Ordercount]/[Measures].[Ordercount Y-1] *100,0)
select {[Measures].[Growth]} ON COLUMNS,
NON EMPTY {[Year].[" +year+ "]} ON ROWS
from [Ordercube]
Its a dialchart, I want to show the % of sales compared to last year in combination with a range between months.
In SQL it would be easy: Where month >= frommonth and month <= tomonth.
Since you can only slice once in a MDX query I don't know what to do.
I hope someone can help me.
Thanks in advance
Actually, you'd find that SQL wouldn't be quite as easy if the months weren't both in the same year :)
Either way, what you're looking for is something like this:
select NON EMPTY {[Measures].[Quantity]} ON COLUMNS,
NON EMPTY [Markets].Children ON ROWS
from [SteelWheelsSales]
where {([Time].[2003].[QTR1] : [Time].[2004].[QTR2])}
This query was written against pentaho's data warehouse. I haven't the faintest clue what your data wharehouse looks like so I don't know what to use in the time dimension for your query, but it's the ([Time].[2003].[QTR1] : [Time].[2004].[QTR2]) syntax you're looking for, I think.
(disclaimer: I'm one of the CDF core developers, but my MDX sucks)
EDIT: In this particular case (Range Operator Reference) the reference site isn't particularly explicit, but the MSDN reference site for MDX is pretty good, so here's the general MDX Reference Site.