I have the following MDX query:
with
MEMBER [Measures].[Prior Week]
AS AGGREGATE(ParallelPeriod([Date].[Sun Weeks], 1, [Date].[Sun Weeks].CURRENTMEMBER), [Measures].[Value Count])
SELECT
NON EMPTY
{
[Measures].[Value Count],
[Measures].[Prior Week]
} ON COLUMNS,
NON EMPTY
{
( [Date].[Sun Weeks].[Day].ALLMEMBERS )
} ON ROWS
FROM [MyCube];
The sun weeks hierarchy is [Date].[Sun Weeks].[Sun Week Year].[Sun Week].[Day]
When I run the above query the [Prior Week] column produces #Error
Does anyone know why? How do I fix it? Does it have something to do do with ParallelPeriod?
You might need to explicitly force the first argument to be a set:
MEMBER [Measures].[Prior Week] AS
AGGREGATE(
{ParallelPeriod(
[Date].[Sun Weeks]
, 1
, [Date].[Sun Weeks].CURRENTMEMBER
)}
,[Measures].[Value Count]
)
But the first argument you've given ParallelPeriod is [Date].[Sun Weeks] ... is this a level in your cube? That function requires a level as its first argument.
If all you want to achieve is to go back 7 days then lag is easier:
MEMBER [Measures].[Prior Week] AS
(
[Date].[Sun Weeks].CURRENTMEMBER.lag(7).item(0)
,[Measures].[Value Count]
)
Or you could lag by 7 days and then jump up a level to the week:
MEMBER [Measures].[Prior Week] AS
(
[Date].[Sun Weeks].CURRENTMEMBER.lag(7).parent
,[Measures].[Value Count]
)
Related
as a newbie in SQL I am lost regarding nested queries.
I am trying to achieve the following: getting a table grouped by month with a count from all values of a column, then the same count filtered by status.
So for instance in January I could have the following result:
Jan 22 Count= 100 Count with Status filter= 57
I tried several variations along these lines:
SELECT
FORMAT ( [CreatedDate.table2] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
count(*) as count_all,
count_filtered
FROM
(SELECT
FORMAT ( [CreatedDate] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
Count(*) AS count_filtered
FROM DM_AccessNoAgg.DimLead
WHERE [CreatedDate] >= '2022-01-01'
AND [Status]='Qualifiziert'
GROUP BY RecordTypeName__c,FORMAT ( [CreatedDate] , 'yyyyMM' )
)
Basically I am using the same value in both cases, just that the second count has to be filtered. What's the best method to get this done?
Thanks for your help!
Pauline.
I have a table with order information. It is broken down by OrderID, Item Name. How can I get the count of all orders with multiple x and y items. For example, how many orders have more than 1 dumplings and more than 1 water?
You can use the Countif function like:
countIf (
Revenue,
# Conditions
CalendarDay >= ${BasePeriodStartDate} AND
CalendarDay <= ${BasePeriodEndDate} AND
SourcingType <> 'Indirect'
)
Documentation
I want to filter the months of [ComptaEcriture Date] by selecting only the months from [ComptaPlanId].[ComptaDateDebut] to [ComptaPlanId].[ComptaDateFin], but since
[ComptaDateDebut] and [ComptaDateFin] are not from the same level and bot are not from the same dimension as [ComptaEcriture Date].[ComptaEcriture Date].[Month], I don't know how to achieve that.
If I could generate a range of months that would be great. My dimensions are as follows:
Assuming you're testing that PlanId is no the All member, you can use isAll MDX+ function for this.
For the set, we will combine the Filter function with a Declared function, even though we could put it all the code in the filter. It looks as :
WITH
FUNCTION inRange(Value _date,Value _start, Value _end) AS _start <= _date AND _date <= _end
SET myDates as Filter( [Date].[Date].[Month] as t, inRange(t.current.key, DateTime(2015,6,1), DateTime(2017,1,1) ) )
SELECT
myDates on 0
FROM [Cube]
And using the compact and faster version :
SELECT
Filter( [Date].[Date].[Month] as t, DateTime(2015,6,1) <= t.current.key AND t.current.key <= DateTime(2017,1,1) ) on 0
FROM [Cube]
Using the members :
WITH
FUNCTION inRange(Value _date,Value _start, Value _end) AS _start <= _date AND _date <= _end
SET myDates as Filter( [Date].[Date].[Month] as t,
inRange(t.current.key, [ComptaDateDebut].currentmember.key, [ComptaDateFin].currentmember.key )
)
SELECT
myDates on 0
FROM [Cube]
You can use contextMember instead of currentMember that check also in the slicer (FILTER BY or subselect)
In my Symfony app I have an entity 'Project' which contains two fields: 'createdOn' (type = date) and 'individual'. An individual can occur multiple times in 'Project'.
_created_On_|_individual_id
2012.12.01 | 3
2012.12.24 | 5
2013.01.10 | 9
I'm trying to build a query to count all distinct individuals grouped by 'createdOn' in such a way, that I get results sorted by month. And it must be possible to set a date range for the query.
My query so far:
'SELECT p.createdOn, COUNT (DISTINCT p.individual)
FROM ...\DossierBundle\Entity\Project p
WHERE p.createdOn
BETWEEN :name1
AND :name2'
)->setParameters(array(
'name1' => $startDate,
'name2' => $endDate,
))
This doesn't quite get me the desired result below
_DATE____|_Number_of_Individuals
Dec 2012 | 2
Jan 2013 | 1
But instead I get
__DATE_____|_Number_of_Individuals
2012.12.01 | 1
2012.12.24 | 1
2013.01.10 | 1
Google didn't help me either so any support will be much appreciated.
Flo
You need to extend doctrine with custom dates functions , and be carefull because you cant use group by with functions so you'll have to trick doctrine.
have a look at that :
http://www.doctrine-project.org/blog/doctrine2-custom-dql-udfs.html
here is an exemple of a day native mysql function :
https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Day.php
and read that for the group by issue and work around ( using as ) :
http://www.doctrine-project.org/jira/browse/DDC-1236
i needed to group visits by date ( without the time ) , so i wrote that dql query with a date extension :
select count(v) as visit_count , DATE(v.created_at) as day_created_at from Shorten\Entity\Visit v group by day_created_at
hope it helps.
I am having some trouble with doctrine2. I want to order a query but by day of the week.
What do I mean:
If where the day of the date given is Tuesday, I want to have it ordered by Tuesday, Wednseday, ..., Sunday, Monday.
But an performance can have multiple days.
The code I got here does the trick for the order by
public function getValidPerformancesByDay2($date, $max = 25, $from, $asSql = false){
$myday = intVal(date('w',strtotime($date)));
$q = $this->getEntityManager()
->createQueryBuilder()
->select('DISTINCT textdesc,
CASE WHEN (perfdays.id < '. $myday .') THEN perfdays.id + 8
ELSE perfdays.id END AS HIDDEN sortvalue')
->from ('sys4winsegundaBundle:Performance','textdesc')
->join('textdesc.days', 'perfdays')
->where ('textdesc.enddate >= :date')
->andWhere('textdesc.isvalid = true')
->orderBy('sortvalue','ASC')
->setMaxResults($max)
->setFirstResult($from)
->setParameter('date',$date)
;
$query = $q->getQuery();
if ($asSql){
return $query;
}
return $query->getResult();
}
But unfortunately, when I look at the query that has been sent it is:
SELECT DISTINCT p0_.id AS id0, p0_.name AS name1, p0_.duration AS duration2,
p0_.addedby AS addedby3, p0_.startdate AS startdate4, p0_.enddate AS enddate5,
p0_.starthour AS starthour6, p0_.flyer AS flyer7, p0_.price AS price8,
p0_.discount AS discount9, p0_.isvalid AS isvalid10,
p0_.archivedon AS archivedon11, p0_.description AS description12,
p0_.weblink AS weblink13, p0_.techinfo AS techinfo14, p0_.slug AS slug15,
CASE WHEN (d1_.id < 4) THEN d1_.id + 8 ELSE d1_.id END AS sclr16,
p0_.place_id AS place_id17, p0_.gallery_id AS gallery_id18 FROM performances
p0_ INNER JOIN performance_day p2_ ON p0_.id = p2_.performance_id
INNER JOIN days d1_ ON d1_.id = p2_.day_id WHERE p0_.enddate >= ? AND
p0_.isvalid = 1 ORDER BY sclr16 ASC OFFSET 0
Parameters: ['2012-08-23']
Time: 5.13 ms
Which means that if a performance occurred 3 times a week, I get 3 occurrences.
anyone got an idea?
EDIT
my english being pretty bad, i lltry to explain it differently:
Well i got art performances that occurs on various days.
What i want to do is order them by nearest occurence in time.
But the way i sent it to database is with a startdate, an enddate and then what days it occurs (Tuesday, wednesday...)
My query does this( ordering by nearest one) but as some performance occured for example on wednesdays y fridays, my query will return that performance 2 times (on for wednsday and the other one for friday) while i should only retrieve on occurance of each performance but with the same order (nearest first)
If you want 1 occurrence per week use
$q->groupBy()
For the date converted to week number. And use count() in your select. I'm not sure that I Understand your question.