I have a query below that i want to have a running total on the cumulativesum column. currently it shows just the total for the period i am after.
complexity i have is the hour of day is different against one day to the next. example below is 03-23(hr of day) for 28, then 00-02(hr of day) for 29.
mdx for refer
with
member [measures].[Cumulativesum] as sum ({
{[Hour Of Day].[Hour].&[4]:[Hour Of Day].[Hour].&[24]}*{[Date].[Fiscal Months].[Date].&[20220828]},
{[Hour Of Day].[Hour].&[1]:[Hour Of Day].[Hour].&[3]}*{[Date].[Fiscal Months].[Date].&[20220829]}
}, [Measures].[Cartons Picked])
select
{
[Measures].[Cartons Picked],
[measures].[Cumulativesum]
}
on 0,
{
{[Hour Of Day].[Hour].&[4]:[Hour Of Day].[Hour].&[24]}*{[Date].[Fiscal Months].[Date].&[20220828]}
,
{[Hour Of Day].[Hour].&[1]:[Hour Of Day].[Hour].&[3]}*{[Date].[Fiscal Months].[Date].&[20220829]}
} [enter image description here][1]
a view of the output
running total using .currentmember but need it in one column and only for the hours i need.
i've broken the above to parts to solve one step at a time, one problem i have come apon is the ranking bit, see below. any help on solving the rank much appriciated.
with
set daterange1 as {[Date].[Fiscal Months].[Date].&[20220828]}
set daterange2 as {[Date].[Fiscal Months].[Date].&[20220828].nextmember}
set hrs1 as {[Hour Of Day].[Hour].&[4]:[Hour Of Day].[Hour].&[24]}
set hrs2 as {[Hour Of Day].[Hour].&[1]:[Hour Of Day].[Hour].&[3]}
set Datehrs as nonempty({{hrs1}*{daterange1},{hrs2}*{daterange2}}
,[Measures].[Cartons Picked])
member [Measures].[rank] as rank(([Hour Of Day].[Hour].currentmember,[Date].[Fiscal Months].[Date].currentmember), Datehrs)
select
{
[Measures].[Cartons Picked]
,
[Measures].[rank]
}
on 0,
datehrs on 1
Cartons Picked rank
03 28-Aug-2022 1802 #Error
04 28-Aug-2022 3587 #Error
05 28-Aug-2022 6333 #Error
06 28-Aug-2022 7711 #Error
07 28-Aug-2022 5707 #Error
08 28-Aug-2022 7923 #Error
09 28-Aug-2022 6502 #Error
10 28-Aug-2022 5100 #Error
11 28-Aug-2022 7656 #Error
12 28-Aug-2022 5067 #Error
13 28-Aug-2022 5505 #Error
14 28-Aug-2022 8914 #Error
15 28-Aug-2022 10050 #Error
Related
I would love to have your help transposing this table's column into a row. I am trying to have the Project Names (column 1) each becoming their own column header with the MUVs value entered as the column value.
I have read through 20+ threads on this topic, but they are using different SQL packages and code that I am unable to use.
The ideal solution would modify this query to create the desired query output:
Current Query:
SELECT
cust_gae_account.project_name as "Project Name",
cust_be_project_usage.unique_visitor_count as "MUVs",
cust_be_project_usage.billing_period_start as "Month"
FROM
cust_be_project_usage
INNER JOIN
cust_gae_account
ON cust_gae_account.account_id = cust_be_project_usage.project_id
WHERE
cust_be_project_usage.admin_account_id = {ACCOUNT_ID}
Current Query Output:
Project Name | MUVs | Month
-----------------------------------------------------
ProjectAAAAAZ | 68000 | Jun 01, 2016
DynamicName | 3200 | Jun 01, 2016
ProjectAAAAAZ | 21000 | May 01, 2016
DynamicName | 4500 | May 01, 2016
CustomProject | 117000 | Jun 01, 2016
CustomProject | 118400 | May 01, 2016
Desired Query Output:
Project Name | CustomProject | ProjectAAAAAZ | DynamicName
-------------------------------------------------------------------
Jun 01, 2016 | 117000 | 68000 | 3200
May 01, 2016 | 118400 | 21000 | 4500
Notes:
We are using PostgreSQL via Chartio, we can only use base SQL
We cannot use custom functions sadly
Names and number of projects is dynamic, varies from 1 to 20
MUVs are always numeric, there are no duplicates
You can use crosstab https://www.postgresql.org/docs/9.1/static/tablefunc.html , but it will only work with a static list of columns. So you need a dynamic query. You need to define that query, and execute it.
EXECUTE ('SELECT * FROM crosstab(
\'SELECT
cust_be_project_usage.billing_period_start as "Month",
cust_gae_account.project_name as "Project Name",
cust_be_project_usage.unique_visitor_count as "MUVs"
FROM
cust_be_project_usage
INNER JOIN
cust_gae_account
ON cust_gae_account.account_id = cust_be_project_usage.project_id
WHERE
cust_be_project_usage.admin_account_id = {ACCOUNT_ID}\',
\'SELECT
cust_gae_account.project_name as "Project Name"
FROM
cust_be_project_usage
INNER JOIN
cust_gae_account
ON cust_gae_account.account_id = cust_be_project_usage.project_id
WHERE
cust_be_project_usage.admin_account_id = {ACCOUNT_ID}\')
AS ( "Project Name" date, ' ||
(SELECT
string_agg(cust_gae_account.project_name, ' int,')
FROM
cust_be_project_usage
INNER JOIN
cust_gae_account
ON cust_gae_account.account_id = cust_be_project_usage.project_id
WHERE
cust_be_project_usage.admin_account_id = {ACCOUNT_ID}) || ' int'
|| ' )')
I am not able to test it though. It SHOULD work, but my suspicions are that it might not, in these situations:
Concatenating to a scalar formed by a query (might need to use a variable for that one)
I might have mixed up a column somewhere
Not sure how you pass the {ACCOUNT_ID} parameter so you might need to change that too
I need to get that email address. The definition of this Physical File is: I have a crystal report that is going to have the sales rep id name (DMARANO1). I need to get the email address of the rep so the report can send to him/her only on correct rep data.
1-4 CAT
5-20 REF FILE SELECTION
21-22 RFSQ2
23-62 RDATA
63-63 FILLER
64-64 ACTIVITY CODE
As I see it, the 0025 has the sales rep name and code (code = DM1), then the 8757 has that code DM1 and the email.
I need to read the 0025, get the code, on the '01' record, then read the 8757
I would have the DMARANO1 to match on (the 0075 record.)
this is not ideal way but we are using a very old Iseries code here.
0025DM1 01DEMI MARANO 0000000 +
0025DM1 02000000 +
0075DMARANO1 01 +
0075DMARANO1 02 00 +
0075DMARANO1 0311OACI 11 1 +
0075DMARANO1 05 0 +
0075DOLDHOFF 01 +
0075DOLDHOFF 02 00 +
0075DOLDHOFF 0311OACI 11 1 +
0075DOLDHOFF 05 0 +
8511DS55 010030210210004000000002500000030000000000 +
8511DS55 02000000023400006250001750PLSSDM55 GOLD +
8506DMARANO1 01CUS1 +
0069DS55 01ENG-1.75"X.625" GOLD FLEXI 7000 +
7012DMARANO1 01 +
8757DM1 01DMARANO#TESTAWARDS.COM +
8757DM1 02914-347-7566452DEMI MORANO
8511GNPY 010110180180004000000002500000110000000000
Assuming the physical file is named FILE, it contains a single 64 character field named FILE, and the records can be joined on REF_FILE_SELECTION and RFSQ2 the following SQL should get you started:
WITH CTE AS (
SELECT
SUBSTR(FILE, 1, 4) CAT,
SUBSTR(FILE, 5, 15) REF_FILE_SELECTION,
SUBSTR(FILE, 21, 2) RFSQ2,
SUBSTR(FILE, 23, 40) RDATA,
SUBSTR(FILE, 63, 1) FILLER,
SUBSTR(FILE, 64, 1) ACTIVITY_CODE
FROM FILE)
SELECT A.REF_FILE_SELECTION, B.RDATA
FROM CTE A
JOIN CTE B ON A.REF_FILE_SELECTION = B.REF_FILE_SELECTION AND A.RFSQ2 = B.RFSQ2
WHERE A.CAT='0025' AND A.RFSQ2='01' AND B.CAT='8757'
I have a following query (based on sample data provided with Microsoft® SQL Server® 2008 MDX Step by Step book):
WITH
SET important_months AS
{
([Product].[Product Categories].[Subcategory].&[28].CHILDREN , {[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
([Product].[Product Categories].[Product].&[477] , {[Date].[Month of Year].&[3]})
}
SELECT [Measures].[Order Count] ON COLUMNS,
important_months ON ROWS
FROM [Step-by-Step]
The query shows the number of orders placed on products in a particular subcategory in particular months. For all products in category 28, i need the count of orders placed in January or February (month 1 or 2). Exceptions are orders placed on product 447: in this
case, I additionally need to include number of orders placed in March.
In the end however, I'm not really interested in details regarding months:
all I want, is simple number of orders placed on a particular product (i.e. I want to loose/hide the information about what was the month the order was placed).
So instead of
Mountain Bottle Cage, January, 176
Mountain Bottle Cage, February, 183
Road Bottle Cage, January, 141
Road Bottle Cage, February, 152
Water Bottle - 30 oz, January, 381
Water Bottle - 30 oz, February, 403
Water Bottle - 30 oz, March, 414
I need to have:
Mountain Bottle Cage, 359 (176 + 183)
Road Bottle Cage, 293 (141 + 152)
Water Bottle - 30 oz., 1198 (381 + 403 + 414)
I tried with putting the important_months set into a where clause, but (besides circular reference error due to custom set) I wouldn't be able to project the
categories on rows axis (would I?). Also, I thought of using a subquery, but it appears I cannot refer to the important_months set there either.
In other words: I need to get result that in SQL i would get by issuing
SELECT SUM([Order Count])
FROM <MDX RESULT HERE>
GROUP BY Product
Can it be done?
An educated guess is that MDX Subqueries is the solution. Did you try using tuples in the subselect :
WITH
SELECT [Measures].[Order Count] ON COLUMNS,
{[Product].[Product Categories].[Subcategory].&[28].CHILDREN,[Product].[Product Categories].[Product].&[477]} ON ROWS
FROM (
SELECT
{([Product].[Product Categories].[Subcategory].&[28].CHILDREN,{[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
([Product].[Product Categories].[Product].&[477],{[Date].[Month of Year].&[3]})} ON 0
FROM [Step-by-Step]
)
You're creating an asymmetric set (with March for Water Bottle only), so you can't really slice this directly in the WHERE clause without including it for all other products.
icCube's answer looks good to me, with one small addition: add a DISTINCT to the row selection to combine Water Bottle back into one row.
WITH
SELECT [Measures].[Order Count] ON COLUMNS,
DISTINCT {[Product].[Product Categories].[Subcategory].&[28].CHILDREN, [Product].[Product Categories].[Product].&[477]} ON ROWS
FROM (
SELECT
{([Product].[Product Categories].[Subcategory].&[28].CHILDREN,{[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
([Product].[Product Categories].[Product].&[477],{[Date].[Month of Year].&[3]})} ON 0
FROM [Step-by-Step]
)
I have a question related to cubes with Microsoft Technologies, we have an SSAS Cube built on BIDS 2008 version, and the requirement is to group (sum) the measure quantity taking the previous twelve months and excluding some values in one dimension.
I have come up with a calculation inside the cube as follows:
CREATE MEMBER CURRENTCUBE.[Measures].[N-12]
AS Sum
(
(
Except
(
[Dim Sistema Corporativo].[Sistema Corporativo].[Id Sistema]
,{
[Dim Sistema Corporativo].[Sistema Corporativo].[Id Sistema].&[3]
,[Dim Sistema Corporativo].[Sistema Corporativo].[Id Sistema].&[4]
}
)
,lastperiods(12,
[Dim Tiempo].[Fecha].CURRENTMEMBER
)
)
,
[Measures].[importe]
)/*Lime*/,
VISIBLE = 1 ;
I get something like this:
Row Importe N-12
2011 12399121166 12399121166
01.2011 1040785565 1040785565
02.2011 1069453202 2110238768
03.2011 1359303502 3469542269
04.2011 1068266294 4537808563
05.2011 1163538168 5701346731
06.2011 1146393010 6847739741
07.2011 936369144.8 7784108886
08.2011 1000363518 8784472404
09.2011 859885351.7 9644357755
10.2011 779035206.2 10423392962
11.2011 933409920.9 11356802882
12.2011 1042318283 12399121166
2012 25162093544 22225879797
01.2012 -40878580.57 11317457020
02.2012 678706164.3 10926709982
03.2012 16323643149 10555714716
04.2012 947692878 10435141301
05.2012 1057496411 10329099544
06.2012 1103249990 10285956525
07.2012 976810086.3 10326397466
08.2012 1046738046 10372771994
09.2012 1027644991 10540531634
10.2012 1108157924 10869654352
11.2012 932832484 10869076915
12.2012 9826758631
2013 22225879797
2014 22225879797
2015 22225879797
2016 22225879797
2017 22225879797
2018 22225879797
2019 22225879797
2020 22225879797
2021 22225879797
2022 22225879797
2023 9826758631
As you can see it is doing the right thing altough the user asked me to make some tweaks:
First one is that the cube does not show anything if theres no data (identified as "importe" measure group) and as you can see it shows blank spaces for the next years (until 2023) when when there's no data for the default measure group.
Second one is that the user only wants to see data if and only if the current member has 12 previous months so for example, right now is showing info for the year 2011 but as the user said it should only display data for the year 2012 because all those months have previos 12 months and 2011 does not have 2010 data so for this I should have a null value.
is it possible to do this...???
or maybe you can suggest any other option like affecting the fact table directly and so on.
Thank you in advise guys.
To check if the current member has 12 previous month you can use an expression like this one:
IIf(Count(
NonEmpty(lastperiods(12, [Dim Tiempo].[Fecha].CURRENTMEMBER),
[Measures].[importe])
) = 12,
...,
...)
Sorry in advance if this has already been covered.
I am working on a database which isnt particularly well structured but it is owned by a third party and cannot be changed.
I need some assistance with t-sql in find the next occurrence of a value within the table and return records based on the result. Let me first explain the data. I have simplified this to make it easier to understand.
Polref Effective Date Transaction Type Suffix Value
ABCD1 01/06/2010 New Bus 1 175.00
ABCD1 01/06/2011 Ren 2 200.00
ABCD1 19/08/2011 Adjust 3 50.00
ABCD1 23/04/2012 Adjust 4 50.00
ABCD1 01/06/2012 Ren 5 275.00
So if I ran my query for 2011, the code would need to return in this example rows with suffix 2,3 and 4. So what I have been trying to do is find the first suffix of a New Bus or Ren for the specified year and then finding the next suffix for a New Bus or Ren for the same polref and then using those two suffix values to limit my recordset. It aint working!!
I cant use MAX() as transactions for 2013 have already been added to the system to I would get more records than I actually need.
There result I should be expecting for this example data would be:
ABCD1 300.00
Any help would be greatly appreciated.
To answer another question, If I select 2011 as my year to run the report, there should only be one New Bus or Ren transaction for 2011 so if its a New Bus transaction, the next main transaction will be a Ren, if its a Ren then the next main transaction will be a Ren. Again in my example below, if I run for 2011, it should find the Ren from 01/06/2011 so I want to return that Ren and the two Adjust records.
Sorry, I've not used this forum before so apologies if I was a little vague.
The table I am using has many polrefs so I need this code to calculate totals for all polrefs that fall within the date range. Some polrefs may only have one row, a New Bus, some will have many rows depending on how many adjustments have been made throughout the year of the policy
Partial answer:
This query:
declare #t table (PolRef char(5) not null, EffectiveDate date not null,TransactionType varchar(10) not null,Suffix int not null,Value decimal(10,2) not null)
insert into #t (Polref,EffectiveDate,TransactionType,Suffix,Value) values
('ABCD1','20100601','New Bus',1,175.00),
('ABCD1','20110601','Ren',2,200.00),
('ABCD1','20110819','Adjust',3,50.00),
('ABCD1','20120423','Adjust',4,50.00),
('ABCD1','20120601','Ren',5,275.00)
;With StartTransactions as (
select PolRef,Suffix,ROW_NUMBER() OVER (PARTITION BY PolRef ORDER BY Suffix) rn
from #t where TransactionType in ('New Bus','Ren')
), Periods as (
select st1.PolRef,st1.Suffix as StartSuffix,st2.Suffix as EndSuffix
from
StartTransactions st1
left join
StartTransactions st2
on
st1.PolRef = st2.PolRef and
st1.rn = st2.rn - 1
)
select
p.PolRef,t2.EffectiveDate,SUM(t.Value) as Total
from
Periods p
inner join
#t t
on
p.PolRef = t.PolRef and
p.StartSuffix <= t.Suffix and
(p.EndSuffix > t.Suffix or
p.EndSuffix is null)
inner join
#t t2
on
p.PolRef = t2.PolRef and
t2.Suffix = p.StartSuffix
group by
p.PolRef,t2.EffectiveDate
Groups each set of transactions based on each successive Ren or New Bus transaction:
PolRef EffectiveDate Total
------ ------------- ---------------------------------------
ABCD1 2010-06-01 175.00
ABCD1 2011-06-01 300.00
ABCD1 2012-06-01 275.00
From that, it should be trivial to e.g. select out only the ones you're interested in from a particular year. But your question is still vague on some specifics, so I'm not taking it any further at this point.