Using group by in CT with Cross Apply in SQL Server 2008












0















I am trying to do an average between different rows data as shown below.



Here's my original Questions
which McNets answered.



So basically here is the table



create table t (id int identity, BIDID int,  AppID int, AppStatus varchar(20), [Time] date);
insert into t values
(23390, 16, 'In Review', '20170307'),
(23390, 16, 'Approved', '20170309'),
(23390, 16, 'In Review', '20171110'),
(23390, 16, 'Approved', '20171112'),
(23390, 17, 'In Review', '20171114'),
(23390, 18, 'Approved', '20171112'),
(23390, 16, 'Approved', '20171114'),
(23391, 17, 'In Review', '20171112'),
(23391, 18, 'Approved', '20171114')


I am trying to calculate the average between In Review and Approved values only first between an individual AppID and then doing the Average in different AppId's in a individual bid and then finally doing the Average between all the bids to come to a single number



Here is the code for it



SELECT
CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
THEN DATEDIFF(day, t1.[Time], t2.[Time])
ELSE 0
END as Days
FROM
t t1
CROSS APPLY(SELECT TOP 1 *
FROM t
WHERE id > t1.id
ORDER BY id) t2


and this gets me the Average



WITH ct AS
(
SELECT
CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
THEN DATEDIFF(day, t1.[Time], t2.[Time])
ELSE 0
END as Days
FROM
t t1
CROSS APPLY(SELECT TOP 1 *
FROM t
WHERE id > t1.id
ORDER BY id) t2
)
SELECT
SUM(days) / COUNT(*) Average
FROM
ct
WHERE
days <> 0


This currently does not do any grouping of any sort so it just takes into account first In Review and then finds the next Approved and so on. I need to group it first by AppID to calculate individual AppId average and then do the average of different AppId's inside a bid and then do an average of the bid's



(23390, 16, 'In Review', '20170307'), 
(23390, 16, 'Approved', '20170309'),
(23390, 16, 'In Review', '20171110'),
(23390, 16, 'Approved', '20171112'),
(23390, 17, 'In Review', '20171114'),
(23390, 18, 'Approved', '20171112'),
(23390, 16, 'Approved', '20171114'),
(23391, 17, 'In Review', '20171112'),
(23391, 18, 'Approved', '20171114')


First group by BidID so 2 bids 23390 and 23391 then Avg with App Id 16,17 for first Bid and 18 for second bid and then calculate the Avg between the bids.



Can someone please tell me how should I add the grouping.



DBFiddle



Thanks










share|improve this question





























    0















    I am trying to do an average between different rows data as shown below.



    Here's my original Questions
    which McNets answered.



    So basically here is the table



    create table t (id int identity, BIDID int,  AppID int, AppStatus varchar(20), [Time] date);
    insert into t values
    (23390, 16, 'In Review', '20170307'),
    (23390, 16, 'Approved', '20170309'),
    (23390, 16, 'In Review', '20171110'),
    (23390, 16, 'Approved', '20171112'),
    (23390, 17, 'In Review', '20171114'),
    (23390, 18, 'Approved', '20171112'),
    (23390, 16, 'Approved', '20171114'),
    (23391, 17, 'In Review', '20171112'),
    (23391, 18, 'Approved', '20171114')


    I am trying to calculate the average between In Review and Approved values only first between an individual AppID and then doing the Average in different AppId's in a individual bid and then finally doing the Average between all the bids to come to a single number



    Here is the code for it



    SELECT
    CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
    THEN DATEDIFF(day, t1.[Time], t2.[Time])
    ELSE 0
    END as Days
    FROM
    t t1
    CROSS APPLY(SELECT TOP 1 *
    FROM t
    WHERE id > t1.id
    ORDER BY id) t2


    and this gets me the Average



    WITH ct AS
    (
    SELECT
    CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
    THEN DATEDIFF(day, t1.[Time], t2.[Time])
    ELSE 0
    END as Days
    FROM
    t t1
    CROSS APPLY(SELECT TOP 1 *
    FROM t
    WHERE id > t1.id
    ORDER BY id) t2
    )
    SELECT
    SUM(days) / COUNT(*) Average
    FROM
    ct
    WHERE
    days <> 0


    This currently does not do any grouping of any sort so it just takes into account first In Review and then finds the next Approved and so on. I need to group it first by AppID to calculate individual AppId average and then do the average of different AppId's inside a bid and then do an average of the bid's



    (23390, 16, 'In Review', '20170307'), 
    (23390, 16, 'Approved', '20170309'),
    (23390, 16, 'In Review', '20171110'),
    (23390, 16, 'Approved', '20171112'),
    (23390, 17, 'In Review', '20171114'),
    (23390, 18, 'Approved', '20171112'),
    (23390, 16, 'Approved', '20171114'),
    (23391, 17, 'In Review', '20171112'),
    (23391, 18, 'Approved', '20171114')


    First group by BidID so 2 bids 23390 and 23391 then Avg with App Id 16,17 for first Bid and 18 for second bid and then calculate the Avg between the bids.



    Can someone please tell me how should I add the grouping.



    DBFiddle



    Thanks










    share|improve this question



























      0












      0








      0








      I am trying to do an average between different rows data as shown below.



      Here's my original Questions
      which McNets answered.



      So basically here is the table



      create table t (id int identity, BIDID int,  AppID int, AppStatus varchar(20), [Time] date);
      insert into t values
      (23390, 16, 'In Review', '20170307'),
      (23390, 16, 'Approved', '20170309'),
      (23390, 16, 'In Review', '20171110'),
      (23390, 16, 'Approved', '20171112'),
      (23390, 17, 'In Review', '20171114'),
      (23390, 18, 'Approved', '20171112'),
      (23390, 16, 'Approved', '20171114'),
      (23391, 17, 'In Review', '20171112'),
      (23391, 18, 'Approved', '20171114')


      I am trying to calculate the average between In Review and Approved values only first between an individual AppID and then doing the Average in different AppId's in a individual bid and then finally doing the Average between all the bids to come to a single number



      Here is the code for it



      SELECT
      CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
      THEN DATEDIFF(day, t1.[Time], t2.[Time])
      ELSE 0
      END as Days
      FROM
      t t1
      CROSS APPLY(SELECT TOP 1 *
      FROM t
      WHERE id > t1.id
      ORDER BY id) t2


      and this gets me the Average



      WITH ct AS
      (
      SELECT
      CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
      THEN DATEDIFF(day, t1.[Time], t2.[Time])
      ELSE 0
      END as Days
      FROM
      t t1
      CROSS APPLY(SELECT TOP 1 *
      FROM t
      WHERE id > t1.id
      ORDER BY id) t2
      )
      SELECT
      SUM(days) / COUNT(*) Average
      FROM
      ct
      WHERE
      days <> 0


      This currently does not do any grouping of any sort so it just takes into account first In Review and then finds the next Approved and so on. I need to group it first by AppID to calculate individual AppId average and then do the average of different AppId's inside a bid and then do an average of the bid's



      (23390, 16, 'In Review', '20170307'), 
      (23390, 16, 'Approved', '20170309'),
      (23390, 16, 'In Review', '20171110'),
      (23390, 16, 'Approved', '20171112'),
      (23390, 17, 'In Review', '20171114'),
      (23390, 18, 'Approved', '20171112'),
      (23390, 16, 'Approved', '20171114'),
      (23391, 17, 'In Review', '20171112'),
      (23391, 18, 'Approved', '20171114')


      First group by BidID so 2 bids 23390 and 23391 then Avg with App Id 16,17 for first Bid and 18 for second bid and then calculate the Avg between the bids.



      Can someone please tell me how should I add the grouping.



      DBFiddle



      Thanks










      share|improve this question
















      I am trying to do an average between different rows data as shown below.



      Here's my original Questions
      which McNets answered.



      So basically here is the table



      create table t (id int identity, BIDID int,  AppID int, AppStatus varchar(20), [Time] date);
      insert into t values
      (23390, 16, 'In Review', '20170307'),
      (23390, 16, 'Approved', '20170309'),
      (23390, 16, 'In Review', '20171110'),
      (23390, 16, 'Approved', '20171112'),
      (23390, 17, 'In Review', '20171114'),
      (23390, 18, 'Approved', '20171112'),
      (23390, 16, 'Approved', '20171114'),
      (23391, 17, 'In Review', '20171112'),
      (23391, 18, 'Approved', '20171114')


      I am trying to calculate the average between In Review and Approved values only first between an individual AppID and then doing the Average in different AppId's in a individual bid and then finally doing the Average between all the bids to come to a single number



      Here is the code for it



      SELECT
      CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
      THEN DATEDIFF(day, t1.[Time], t2.[Time])
      ELSE 0
      END as Days
      FROM
      t t1
      CROSS APPLY(SELECT TOP 1 *
      FROM t
      WHERE id > t1.id
      ORDER BY id) t2


      and this gets me the Average



      WITH ct AS
      (
      SELECT
      CASE WHEN t1.AppStatus = 'In Review' AND t2.AppStatus = 'Approved'
      THEN DATEDIFF(day, t1.[Time], t2.[Time])
      ELSE 0
      END as Days
      FROM
      t t1
      CROSS APPLY(SELECT TOP 1 *
      FROM t
      WHERE id > t1.id
      ORDER BY id) t2
      )
      SELECT
      SUM(days) / COUNT(*) Average
      FROM
      ct
      WHERE
      days <> 0


      This currently does not do any grouping of any sort so it just takes into account first In Review and then finds the next Approved and so on. I need to group it first by AppID to calculate individual AppId average and then do the average of different AppId's inside a bid and then do an average of the bid's



      (23390, 16, 'In Review', '20170307'), 
      (23390, 16, 'Approved', '20170309'),
      (23390, 16, 'In Review', '20171110'),
      (23390, 16, 'Approved', '20171112'),
      (23390, 17, 'In Review', '20171114'),
      (23390, 18, 'Approved', '20171112'),
      (23390, 16, 'Approved', '20171114'),
      (23391, 17, 'In Review', '20171112'),
      (23391, 18, 'Approved', '20171114')


      First group by BidID so 2 bids 23390 and 23391 then Avg with App Id 16,17 for first Bid and 18 for second bid and then calculate the Avg between the bids.



      Can someone please tell me how should I add the grouping.



      DBFiddle



      Thanks







      sql-server-2008






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 9 mins ago







      SP1

















      asked 16 mins ago









      SP1SP1

      1554




      1554






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fdba.stackexchange.com%2fquestions%2f231877%2fusing-group-by-in-ct-with-cross-apply-in-sql-server-2008%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • 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%2fdba.stackexchange.com%2fquestions%2f231877%2fusing-group-by-in-ct-with-cross-apply-in-sql-server-2008%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

          Ronny Ackermann

          Köttigit

          MySQL 8.0.15 starts normally but any connection hangs