从SQL到SSAS的计算脚本

时间:2019-06-14 05:42:07

标签: sql ssas mdx

我想将SQL代码转换为SSAA项目的SSAS计算脚本。 该脚本的目的是计算特定日期的股票价值。

select  sum(p.movement) Stock
        ,
        ((select top 1  price from ItemMovement_New ch 
                                where ch.ITEMID='10003226' and  ch.Store='F-GJK' and ch.SaleDate <='2019-06-01' 
                                order by SaleDate desc)) Price, 
        sum(p.movement) +  ((select top 1  price from ItemMovement_New ch 
                                                where ch.ITEMID='10003226' and  ch.Store='F-GJK' and ch.SaleDate <='2019-06-01' 
                                                order by SaleDate desc)) [Stock Value]
from ItemMovement_New p
where Store='F-GJK'  and ITEMID='10003226' and SaleDate <='2019-06-01'

结果:-

Stock   Price   Stock_Value
 1      2895       2895

2 个答案:

答案 0 :(得分:0)

CREATE MEMBER CURRENTCUBE.[Measures].MaxDate AS
0 , VISIBLE = False;

CREATE MEMBER CURRENTCUBE.[Measures].DAYSTODATE AS 
COUNT(NULL:[Time].[Date].CURRENTMEMBER)-1
, VISIBLE = False; 

CREATE MEMBER CURRENTCUBE.[Measures].HADSALE AS 
IIF([Measures].[Price]=0, NULL, [Measures].DAYSTODATE)
, VISIBLE = False; 

SCOPE([Measures].MAXDATE, [Time].[Time].[Date].MEMBERS);  
    THIS = MAX(NULL:[Time].[Date].CURRENTMEMBER, [Measures].HADSALE); 
END SCOPE;

CREATE MEMBER CURRENTCUBE.[Measures].LASTSALE AS
IIF(ISEMPTY([Measures].MAXDATE), NULL, 
(
([Measures].[Price]) ,
[Time].[Time].[Date].MEMBERS.ITEM([Measures].MAXDATE))), VISIBLE = True; 

答案 1 :(得分:-1)

以下查询价格中的

Plz注释将不是总和。它应该是不同的,或者如果您求和后将其除以行数。

with 
member measure.ClosingCount 
as
sum(([date].[date].currentmember,[Product].[Product].currentmember), 
[Meausres].[Opening]-[Measures].[Qty])

measure.ClosingAmount 
as
sum(([date].[date].currentmember,[Product].[Product].currentmember), 
[measure].[ClosingCount]*[Measures].[Price] )

select measure.ClosingAmount on 0,
[Product].[Product].[YourProduct] on 1 
from yourcube 
where 
[date].[date].[2019-06-01]