SQL Server, merging Views

Posted on 2012-08-15
Last Modified: 2012-08-21
I have been working a lengthy custom project revolving around custom VB apps and a SQL database.

I’m nearing the end and I have hit a brick wall.

I have created two views with data I need to populate my final report with.

The first view is Aquisition records, the second is disposition records.

For simplicity, each view has  columns as follows

Aquisition View
Serial_Number, Aquire_From, Aquire_Date

Disposition View
Serial_Number, Disposed_To, Disposed_Date

These views keep track of our Serialized Product during the production process.  A serial number can travel several times between the plant, vendors, or customer returns. So there can be multiple entries in each view for the same number.

I would like to be able to merge these two views into a report as in the attached sample.

AS you see a serial number can have multiple transactions before being sold.  If a serial number hasn’t been sold yet or no disposition has been made against it yet, the disposition is blank and is considered in our inventory.

Every transaction has a date associated with it (date/time to the millisecond). I haven’t been able to match records up to achieve this report yet.

Please let me know if more info is required.
Thank you.
Question by:mossmis
    LVL 75

    Expert Comment

    by:Anthony Perkins
    Something like this perhaps:
    SELECT  a.Serial_Number,
    FROM    Aquisition a
            LEFT JOIN Disposition d ON a.Serial_Number = d.Serial_Number

    Open in new window


    Author Comment


    If it were only that easy. I need to match transactions based on sequence. For every acquisition I need to find the next disposition in date sequence that matches with that serial number. That query you gave would have worked if there were only one transaction per serial number in each of the views, but there are multiple transactions per serial number.
    LVL 75

    Expert Comment

    by:Anthony Perkins
    I am afraid, I am not following you.

    Accepted Solution

    I think I might have figured it out, however, I don't know if it will be efficient.
    It's a little hard to explain, but maybe you can see in this code:

    select MA.serial_number,
    MA.transaction_date as ACQ_DATE, 
    MA.T_From as ACQ_FROM, 
    (select top 1 MD.transaction_date from DISP_VIEW MD where MD.serial_number = MA.serial_number
    and MD.transaction_date > MA.transaction_date order by md.transaction_date) as DISP_DATE,
    (select top 1 MD.TRANSFER_TO from DISP_VIEW MD where MD.serial_number = MA.serial_number
    and MD.transaction_date > MA.transaction_date order by md.transaction_date) as DISP_To
    from ACQ_VIEW MA

    Open in new window


    Author Closing Comment

    my select statement I posted seems to work. I did have an issue with one of the views that I fixed, but other than that, it seems OK.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now