CTE doesn't work in SSAS Cube. I want to find solution or convert it ti subquery












0















I write this CTE query and the explanation is:



    WITH TP AS 
(select
c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,c.PeriodCode,
c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD, LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
(select count(*) from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv') as #children
from dTimePeriod c
where (MarketId = 7) ),
TP2 AS
( SELECT *
FROM TP
WHERE #children='12' ),
TP3 AS
(SELECT TP.*, CASE WHEN (TP.WD IS NOT NULL) AND (TP.StartDate <= getdate()) AND TP2.ID=TP.ParentId THEN 18 ELSE NULL END AS WorkingDays
FROM TP LEFT JOIN TP2 ON TP2.ID=TP.ParentId)
select * from TP3
order by ID


and this is the result
CTE Image



I have recursive table called [dTimePeriod] this table contains different cycles and each cycle contains different number of periods,EX: one cycle has 8 periods another cycle has 12 periods and so on, I want if cycles contains 12 periods put to each period value = 18 and for others cycle periods null
and there are some another conditions but it's not the issue.



And when I put it in the SSAS cube doesn't work because the cube doesn't understand the CTE so I tried to find a solution but it doesn't work,
one of them to put this CTE in a view and call this view in the cube but the view doesn't work as well.
so I start to write it as subquery to make the cube able to understand it.
but I am stuck, I can't write this CTE in subquery statement



and this is the subquery where I stuck



SELECT         c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,
c.PeriodCode, c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD,
LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
CASE WHEN (c.WD IS NOT NULL) AND (c.StartDate <= getdate()) THEN 18
WHEN (c.WD IS NOT NULL) AND (c.StartDate > getdate()) THEN NULL ELSE c.WD END AS WorkingDays,

case when (select sub.* from
(select count(*) as children from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv' ) sub ) = 12
then 18 else null end as WWW


FROM dTimePeriod c
WHERE (c.MarketId = 7)









share|improve this question



























    0















    I write this CTE query and the explanation is:



        WITH TP AS 
    (select
    c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,c.PeriodCode,
    c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD, LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
    (select count(*) from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv') as #children
    from dTimePeriod c
    where (MarketId = 7) ),
    TP2 AS
    ( SELECT *
    FROM TP
    WHERE #children='12' ),
    TP3 AS
    (SELECT TP.*, CASE WHEN (TP.WD IS NOT NULL) AND (TP.StartDate <= getdate()) AND TP2.ID=TP.ParentId THEN 18 ELSE NULL END AS WorkingDays
    FROM TP LEFT JOIN TP2 ON TP2.ID=TP.ParentId)
    select * from TP3
    order by ID


    and this is the result
    CTE Image



    I have recursive table called [dTimePeriod] this table contains different cycles and each cycle contains different number of periods,EX: one cycle has 8 periods another cycle has 12 periods and so on, I want if cycles contains 12 periods put to each period value = 18 and for others cycle periods null
    and there are some another conditions but it's not the issue.



    And when I put it in the SSAS cube doesn't work because the cube doesn't understand the CTE so I tried to find a solution but it doesn't work,
    one of them to put this CTE in a view and call this view in the cube but the view doesn't work as well.
    so I start to write it as subquery to make the cube able to understand it.
    but I am stuck, I can't write this CTE in subquery statement



    and this is the subquery where I stuck



    SELECT         c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,
    c.PeriodCode, c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD,
    LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
    CASE WHEN (c.WD IS NOT NULL) AND (c.StartDate <= getdate()) THEN 18
    WHEN (c.WD IS NOT NULL) AND (c.StartDate > getdate()) THEN NULL ELSE c.WD END AS WorkingDays,

    case when (select sub.* from
    (select count(*) as children from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv' ) sub ) = 12
    then 18 else null end as WWW


    FROM dTimePeriod c
    WHERE (c.MarketId = 7)









    share|improve this question

























      0












      0








      0








      I write this CTE query and the explanation is:



          WITH TP AS 
      (select
      c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,c.PeriodCode,
      c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD, LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
      (select count(*) from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv') as #children
      from dTimePeriod c
      where (MarketId = 7) ),
      TP2 AS
      ( SELECT *
      FROM TP
      WHERE #children='12' ),
      TP3 AS
      (SELECT TP.*, CASE WHEN (TP.WD IS NOT NULL) AND (TP.StartDate <= getdate()) AND TP2.ID=TP.ParentId THEN 18 ELSE NULL END AS WorkingDays
      FROM TP LEFT JOIN TP2 ON TP2.ID=TP.ParentId)
      select * from TP3
      order by ID


      and this is the result
      CTE Image



      I have recursive table called [dTimePeriod] this table contains different cycles and each cycle contains different number of periods,EX: one cycle has 8 periods another cycle has 12 periods and so on, I want if cycles contains 12 periods put to each period value = 18 and for others cycle periods null
      and there are some another conditions but it's not the issue.



      And when I put it in the SSAS cube doesn't work because the cube doesn't understand the CTE so I tried to find a solution but it doesn't work,
      one of them to put this CTE in a view and call this view in the cube but the view doesn't work as well.
      so I start to write it as subquery to make the cube able to understand it.
      but I am stuck, I can't write this CTE in subquery statement



      and this is the subquery where I stuck



      SELECT         c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,
      c.PeriodCode, c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD,
      LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
      CASE WHEN (c.WD IS NOT NULL) AND (c.StartDate <= getdate()) THEN 18
      WHEN (c.WD IS NOT NULL) AND (c.StartDate > getdate()) THEN NULL ELSE c.WD END AS WorkingDays,

      case when (select sub.* from
      (select count(*) as children from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv' ) sub ) = 12
      then 18 else null end as WWW


      FROM dTimePeriod c
      WHERE (c.MarketId = 7)









      share|improve this question














      I write this CTE query and the explanation is:



          WITH TP AS 
      (select
      c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,c.PeriodCode,
      c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD, LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
      (select count(*) from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv') as #children
      from dTimePeriod c
      where (MarketId = 7) ),
      TP2 AS
      ( SELECT *
      FROM TP
      WHERE #children='12' ),
      TP3 AS
      (SELECT TP.*, CASE WHEN (TP.WD IS NOT NULL) AND (TP.StartDate <= getdate()) AND TP2.ID=TP.ParentId THEN 18 ELSE NULL END AS WorkingDays
      FROM TP LEFT JOIN TP2 ON TP2.ID=TP.ParentId)
      select * from TP3
      order by ID


      and this is the result
      CTE Image



      I have recursive table called [dTimePeriod] this table contains different cycles and each cycle contains different number of periods,EX: one cycle has 8 periods another cycle has 12 periods and so on, I want if cycles contains 12 periods put to each period value = 18 and for others cycle periods null
      and there are some another conditions but it's not the issue.



      And when I put it in the SSAS cube doesn't work because the cube doesn't understand the CTE so I tried to find a solution but it doesn't work,
      one of them to put this CTE in a view and call this view in the cube but the view doesn't work as well.
      so I start to write it as subquery to make the cube able to understand it.
      but I am stuck, I can't write this CTE in subquery statement



      and this is the subquery where I stuck



      SELECT         c.ID, c.PeriodCId, c.PeriodName, c.Status, c.StatusChangeDate, CAST(c.StartDate AS DATE) AS StartDate, c.EndDate,
      c.PeriodCode, c.PeriodType, c.ParentCId, c.MarketId, c.ParentId, c.WD,
      LEFT(CONVERT(varchar, c.StartDate, 112), 6) AS YEARMONTH,
      CASE WHEN (c.WD IS NOT NULL) AND (c.StartDate <= getdate()) THEN 18
      WHEN (c.WD IS NOT NULL) AND (c.StartDate > getdate()) THEN NULL ELSE c.WD END AS WorkingDays,

      case when (select sub.* from
      (select count(*) as children from dTimePeriod c2 where c2.ParentId = c.ID and c2.Status='actv' ) sub ) = 12
      then 18 else null end as WWW


      FROM dTimePeriod c
      WHERE (c.MarketId = 7)






      sql sql-server subquery ssas common-table-expression






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 19:18









      emad ahmedemad ahmed

      62




      62
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Instead of using the SQL command directly in the data source for your cube, you can turn this query into a stored procedure and use that result set in the cube. For the SQL statement in the cube, just do an EXEC command as below. The database name isn't necessary if this database is already the initial catalog in the connection string of the data source.



          EXEC YourDatabase.YourSchema.YourSP





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436981%2fcte-doesnt-work-in-ssas-cube-i-want-to-find-solution-or-convert-it-ti-subquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Instead of using the SQL command directly in the data source for your cube, you can turn this query into a stored procedure and use that result set in the cube. For the SQL statement in the cube, just do an EXEC command as below. The database name isn't necessary if this database is already the initial catalog in the connection string of the data source.



            EXEC YourDatabase.YourSchema.YourSP





            share|improve this answer




























              0














              Instead of using the SQL command directly in the data source for your cube, you can turn this query into a stored procedure and use that result set in the cube. For the SQL statement in the cube, just do an EXEC command as below. The database name isn't necessary if this database is already the initial catalog in the connection string of the data source.



              EXEC YourDatabase.YourSchema.YourSP





              share|improve this answer


























                0












                0








                0







                Instead of using the SQL command directly in the data source for your cube, you can turn this query into a stored procedure and use that result set in the cube. For the SQL statement in the cube, just do an EXEC command as below. The database name isn't necessary if this database is already the initial catalog in the connection string of the data source.



                EXEC YourDatabase.YourSchema.YourSP





                share|improve this answer













                Instead of using the SQL command directly in the data source for your cube, you can turn this query into a stored procedure and use that result set in the cube. For the SQL statement in the cube, just do an EXEC command as below. The database name isn't necessary if this database is already the initial catalog in the connection string of the data source.



                EXEC YourDatabase.YourSchema.YourSP






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 14:45









                userfl89userfl89

                2,8841512




                2,8841512
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436981%2fcte-doesnt-work-in-ssas-cube-i-want-to-find-solution-or-convert-it-ti-subquery%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    RAC Tourist Trophy