Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6353059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:20:35+00:00 2026-05-24T22:20:35+00:00

Not sure where to start… But basically I have a report table, an account

  • 0

Not sure where to start… But basically I have a report table, an account table, and an account history table. The account history table will have zero or more records, where each record is the state of the account cancelled flag after it changed.
There is other stuff going on, but basically i am looking to return the account detail data, with the state of account cancelled bit on the start date and enddate as different columns.

What is the best way to do this?

I have the following working query below

(Idea) Should I do seperate joins on history table, 1 for each date?

I guess I could do it in three separate queries ( Get Begin Snapshot, End Snapshot, Normal Report query with a join to each snapshot)

something else?

Expected output:

AccountID, OtherData, StartDateCancelled, EndDateCancelled

Test Tables:

DECLARE @Report TABLE (ReportID INT, StartDate DATETIME, EndDate DATETIME)
DECLARE @ReportAccountDetail TABLE( ReportID INT, Accountid INT, Cancelled BIT )
DECLARE @AccountHistory TABLE( AccountID INT, ModifiedDate DATETIME, Cancelled BIT )

INSERT INTO @Report
SELECT 1,'1/1/2011', '2/1/2011'
--
INSERT INTO @ReportAccountDetail
SELECT 1 AS ReportID, 1 AS AccountID, 0 AS Cancelled
UNION
SELECT 1,2,0
UNION
SELECT 1,3,1
UNION
SELECT 1,4,1
--
INSERT INTO @AccountHistory
SELECT 2 AS CustomerID, '1/2/2010' AS ModifiedDate, 1 AS Cancelled
UNION--
SELECT 3, '2/1/2011', 1
UNION--
SELECT 4, '1/1/2010', 1
UNION
SELECT 4, '2/1/2010', 0
UNION
SELECT 4, '2/1/2011', 1

Current Query:

SELECT Accountid, OtherData,
 MAX(CASE WHEN BeginRank = 1 THEN CASE WHEN BeginHistoryExists = 1 THEN HistoryCancelled ELSE DefaultCancel END ELSE NULL END ) AS StartDateCancelled,
 MAX(CASE WHEN EndRank = 1 THEN CASE WHEN EndHistoryExists = 1 THEN HistoryCancelled ELSE DefaultCancel END ELSE NULL END ) AS EndDateCancelled
FROM
(
SELECT c.Accountid,
'OtherData' AS OtherData,
--lots of other data
ROW_NUMBER() OVER (PARTITION BY c.AccountID ORDER BY
    CASE WHEN ch.ModifiedDate <= Report.StartDate THEN 1 ELSE 0 END DESC, ch.ModifiedDate desc) AS BeginRank,
CASE WHEN ch.ModifiedDate <= Report.StartDate THEN 1 ELSE 0 END AS BeginHistoryExists,
ROW_NUMBER() OVER ( PARTITION BY c.AccountID ORDER BY
    CASE WHEN ch.ModifiedDate <= Report.EndDate THEN 1 ELSE 0 END DESC, ch.ModifiedDate desc) AS EndRank,
CASE WHEN ch.ModifiedDate <= Report.EndDate THEN 1 ELSE 0 END AS EndHistoryExists,
    CAST( ch.Cancelled AS INT) AS HistoryCancelled,
    0 AS DefaultCancel
FROM
@Report AS Report
INNER JOIN @ReportAccountDetail AS C ON Report.ReportID = C.ReportID
--Others joins related for data to return
LEFT JOIN @AccountHistory AS CH ON CH.AccountID = C.AccountID
WHERE Report.ReportID = 1
) AS x
GROUP BY AccountID, OtherData

Welcome input on writing stack overflow questions. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:20:36+00:00Added an answer on May 24, 2026 at 10:20 pm

    ROW_NUMBER() often suprises me and out-performs my expectations. In this case, however, I’d be tempted to just use correlated sub-queries. At least, I’d test them against the alternatives.

    Note: I would also use real tables, with real indexes, and a realistic volume of fake data. (If it’s worth posting this question, I’m assuming that it’s worth testing this realistically.)

    SELECT
      [Report].ReportID,
      [Account].AccountID,
      [Account].OtherData,
      ISNULL((SELECT TOP 1 Cancelled FROM AccountHistory WHERE AccountID = [Account].AccountID AND ModifiedDate <= [Report].StartDate ORDER BY ModifiedDate DESC), 0) AS StartDateCancelled,
      ISNULL((SELECT TOP 1 Cancelled FROM AccountHistory WHERE AccountID = [Account].AccountID AND ModifiedDate <= [Report].EndDate   ORDER BY ModifiedDate DESC), 0) AS EndDateCancelled
    FROM
      Report                 AS [Report]
    LEFT JOIN
      ReportAccountDetail    AS [Account]
        ON [Account].ReportID = [Report].ReportID
    ORDER BY
      [Report].ReportID,
      [Account].AccountID
    

    Note: For whatever reason, I’ve found that TOP 1 and ORDER BY is faster than MAX().

    In terms of your suggested answer, I’d modify it slightly to just use ISNULL instead of trying to make the Exists columns work.

    I’d also join on the “other data” after all of the working out, rather than inside the inner-most query, so as to avoid having to group by all the “other data”.

    WITH
      HistoricData AS
    (
      SELECT
        Report.ReportID,
        c.Accountid,
        c.OtherData,
        ROW_NUMBER() OVER (PARTITION BY c.ReportID, c.AccountID ORDER BY CASE WHEN ch.ModifiedDate <= Report.StartDate THEN 1 ELSE 0 END DESC, ch.ModifiedDate DESC) AS BeginRank,
        ROW_NUMBER() OVER (PARTITION BY c.ReportID, c.AccountID ORDER BY ch.ModifiedDate DESC) AS EndRank,
        CH.Cancelled
      FROM
        @Report AS Report
      INNER JOIN
        @ReportAccountDetail AS C
          ON Report.ReportID = C.ReportID
      LEFT JOIN
        @AccountHistory AS CH
          ON  CH.AccountID     = C.AccountID
          AND CH.ModifiedDate <= Report.EndDate
    )
    ,
      FlattenedData AS
    (
      SELECT
        ReportID,
        Accountid,
        OtherData,
        ISNULL(MAX(CASE WHEN BeginRank = 1 THEN Cancelled END), 0) AS StartDateCancelled,
        ISNULL(MAX(CASE WHEN EndRank   = 1 THEN Cancelled END), 0) AS EndDateCancelled
      FROM
        [HistoricData]
      GROUP BY
        ReportID,
        AccountID,
        OtherData
    )
    SELECT
      *
    FROM
      [FlattenedData]
    LEFT JOIN
      [OtherData]
        ON Whatever = YouLike
    WHERE
      [FlattenedData].ReportID = 1
    

    And a final possible version…

    WITH
      ReportStartHistory AS
    (
      SELECT
        *
      FROM
      (
        SELECT
          [Report].ReportID,
          ROW_NUMBER() OVER (PARTITION BY [Report].ReportID, [History].AccountID ORDER BY [History].ModifiedDate) AS SequenceID,
          [History].*
        FROM
          Report                 AS [Report]
        INNER JOIN
          AccountHistory         AS [History]
            ON [History].ModifiedDate <= [Report].StartDate
      )
        AS [data]
      WHERE
        SequenceID = 1
    )
    ,
      ReportEndHistory AS
    (
      SELECT
        *
      FROM
      (
        SELECT
          [Report].ReportID,
          ROW_NUMBER() OVER (PARTITION BY [Report].ReportID, [History].AccountID ORDER BY [History].ModifiedDate) AS SequenceID,
          [History].*
        FROM
          Report                 AS [Report]
        INNER JOIN
          AccountHistory         AS [History]
            ON [History].ModifiedDate <= [Report].EndDate
      )
        AS [data]
      WHERE
        SequenceID = 1
    )
    SELECT
      [Report].ReportID,
      [Account].*,
      ISNULL([ReportStartHistory].Cancelled, 0) AS StartDateCancelled,
      ISNULL([ReportEndHistory].Cancelled,   0) AS EndDateCancelled
    FROM
      Report                     AS [Report]
    INNER JOIN
      Account                    AS [Account]
    LEFT JOIN
      [ReportStartHistory]
        ON  [ReportStartHistory].ReportID  = [Report].ReportID
        AND [ReportStartHistory].AccountID = [Account].AccountID
    LEFT JOIN
      [ReportEndHistory]
        ON  [ReportEndHistory].ReportID    = [Report].ReportID
        AND [ReportEndHistory].AccountID   = [Account].AccountID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really interested in speech-to-text algorithms, but I'm not sure where to start studying
I know this is possible but I'm not really sure where to start. Has
Not sure where to start, but I had gotten the most recent version of
Not sure if I'm even calling this right but I wanted to start adding
I need start off with code because I am not sure what terminology to
I'm not sure exactly how to explain this, so I'll just start with an
I start GVIM in not-maximized window mode and split its window horizontally making sure
Not sure how to ask a followup on SO, but this is in reference
Not sure what exactly is going on here, but seems like in .NET 1.1
Not sure if the title is quite right for the question but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.