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 6733707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:05+00:00 2026-05-26T10:48:05+00:00

ok this is a head wrecker and I will try to keep it brief.

  • 0

ok this is a head wrecker and I will try to keep it brief.

I have two tables, Security and SecurityAudit
The Audit table keeps a copy of any changes to the Security table only if changes are made. The SecurityId is the Foriegn key. So the most recent record is always in the Security table and if there is a change then its written to the Audit table and the ValidToDate is set to the date the record was valid up to

[Security](
    [SecurityID] [bigint] IDENTITY(10000,1) NOT NULL,
    [Security] [nvarchar](255) NOT NULL,
    [ISIN] [nvarchar](20) NOT NULL,
    [CountryOfIncID] [bigint] NOT NULL,
    [SecurityTypeID] [bigint] NOT NULL,
    [SubTypeID] [bigint] NOT NULL,
    [ISQ] [bigint] NOT NULL,
    [MemberStateID] [bigint] NULL,
    [ManualOverride] [bit] NULL,

here is the Audit table

[SecurityAudit](
          [SecurityAuditID] [bigint] IDENTITY(10000,1) NOT NULL,
          [SecurityID] [bigint] NOT NULL,
          [Security] [nvarchar](255) NOT NULL,
          [CountryOfIncID] [bigint] NOT NULL,
          [SubTypeID] [bigint] NOT NULL,
          [ISQ] [bigint] NOT NULL,
          [MemberStateID] [bigint] NULL,
          [ValidToDateID] [datetime] Not NULL

So my problem is I now want to get all the securities that where in play at a particular point in time. This means get all the securities that have never had a change from the security table and then the relevant security from the Audit table for that point in time.

I thought I would do this using a CTE, and Union and a Rank but I,ve hit a wall. Its looking like this so far…

With
    cteAllSecurities (RowNum,SecurityID, Security, ISQ, CountryOfIncorporationID, ValidToDate)
    AS
    (
        SELECT RowNum=row_number() OVER (Partition By Security order by ValidToDate desc),
            sa.SecurityID, sa.Security, sa.ISQ, sa.CountryOfIncorporationID, CONVERT(varchar(30), sa.ValidToDate,106)
          FROM
            SecurityAudit sa 
        UNION 
        SELECT 0 as RowNum,
            s.SecurityID, s.Security, s.ISQ, s.CountryOfIncorporationID, CONVERT(varchar(30), GetUtcDate(),106) as ValidToDate
          FROM 
            Security s
    )
    SELECT RowNum, SecurityID, Security, ISQ, CountryOfIncorporationID, ValidToDate
    FROM cteAllSecurities
    order by securityid
    Where row_number() = 1

so I union the Securites table which has the last with the Audit and give them a row number. I was then hoping to get all the securities that have a row number of 0 and no child records… ie a child count of 0 in the audit table and the Audit Records that are less than the date I’m looking for and take the top one.

So I,ve got so far. Am I on the right tracks, over cooking it, or just plan mad. Any help would be greatly appreciated.

Regards M

  • 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-26T10:48:05+00:00Added an answer on May 26, 2026 at 10:48 am

    I think you should be able to get the results you’re looking for with a query such as this:

    AllSecurity (Security, ISQ, CountryOfIncorporationID, ValidToDate)
    AS (
        SELECT Security, ISQ, CountryOfIncID, CONVERT(DATETIME, '9999-12-31 23:59:59.997')
        FROM Security
            UNION
        SELECT Security, ISQ, CountryOfIncID, ValidToDate
        FROM SecurityAudit
    )
    SELECT TOP 1 Security, ISQ, CountryOfIncorporationID, ValidToDate
    FROM AllSecurity A
    WHERE ValidToDate = (
        SELECT MIN(ValidToDate)
        FROM AllSecurity B
        WHERE B.Security = A.Security
          AND ValidToDate > @SearchDate
    )
    

    You’ll obviously want to replace @SearchDate with the value you’re looking for.

    This query will union the “current” security table, setting the ValidToDate to the maximum DATETIME value that SQL Server accepts, and unions it with the Audit Table. Then, using a subquery, we get the “closest” (minimum) ValidToDate that’s larger than the date we’re looking at.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been smashing my head with this for a while. I have 2 completely
I have been banging my head on this one all day. The C++ project
I'm hittig my head on this and i will be glad for any help.
I've been scratching my head over this for an hour... I have a list
I'm trying to head this one off at the pass. I've got two database
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have this in the head of my document: <script type=text/javascript> var myString= location.href;
So lets say I have an array containing a hash like this: [{head=> {title=>$20,000
In the index file I have this : <div id=head> <h1>bla bla bla bla
I've been knocking my head against this for some time now. I'm not really

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.