complex select problem w history tables, date ranges

  • I working on a mobile/cell phone database. Unlike in the original db I will use english column names here for better understanding (I'm native german)

    There's one table, holding the SIM cards with the following (amongst others) columns:

    SIMCards

    TelNumber

    ID_User

    IMEI

    I also have 2 history tables, where data gets inserted, when the SIM-cards table gets updated:

    DeviceHistory

    ID_DeviceHistory

    TelNumber

    ChangeDate

    IMEIold

    IMEInew

    and

    UserHistory

    ID_UserHistory

    TelNumber

    ChangeDate

    UserOld

    UserNew

    The device is linked to the SIM card, but it would of course be nice to know, when a certain user had a device.

    I somehow have to query, when a user had a number and which device this number was assigned to in the same date range :blink:

    I thought about it the whole morning, but I just didnt wangle it. The data is there, and it certainly can be done ... but how?

  • Without some sample data it is difficult to tell what you want.

    Maybe something like:

    SELECT U1.UserNew

    &nbsp&nbsp&nbsp&nbsp,D.TelNumber

    &nbsp&nbsp&nbsp&nbsp,D.ChangeDate AS DeviceDate

    &nbsp&nbsp&nbsp&nbsp,D.IMEInew

    FROM DeviceHistory D

    &nbsp&nbsp&nbsp&nbspJOIN (

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspUserHistory U1

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspLEFT JOIN UserHistory U2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON U1.TelNumber = U2.TelNumber

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND U1.UserNew = U2.UserOld

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND U2.ChangeDate = (

    &nbsp&nbsp&nb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ON D.TelNumber = U1.TelNumber

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND D.ChangeDate >= U1.ChangeDate

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND (D.ChangeDate < U2.ChangeDate OR U2.ChangeDate IS NULL)

  • Michael,

    Please keep in mind to help your fellow SQLServerCentral members by posting table structure and sample data, so that we can be at our best in helping you.

    Please try out what Ken has provided and let us know if it works or not.

    Regards,

    Wameng Vang

    MCTS

  • mengus (10/4/2007)


    Michael,

    Please keep in mind to help your fellow SQLServerCentral members by posting table structure and sample data, so that we can be at our best in helping you.

    Please try out what Ken has provided and let us know if it works or not.

    Regards,

    Wameng Vang

    Oh, sorry. Of course.

    I will do that, as soon as I'm at work again!

    Thanks for the help so far!

  • Hello Ken,

    I tested the query you posted and it works just the way I wanted it to. Great, thanks a lot! :w00t:

    For future issues:

    I dont completely get how you did this. (I didn't even know that its possible to join via /> or /< )

    I find this very complicated and wondered how one can figure out such solution ... do you make a sketch, or is it just the experience??

  • SELECT U1.UserNew

    &nbsp&nbsp&nbsp&nbsp,D.TelNumber

    &nbsp&nbsp&nbsp&nbsp,D.ChangeDate AS DeviceDate

    &nbsp&nbsp&nbsp&nbsp,D.IMEInew

    Ken,

    How do you format the code in your post? I can't get this new forum tool to work for me.

    Tomm Carr
    --
    Version Normal Form -- http://groups.google.com/group/vrdbms

  • I too would like to know how you keep your formatting!!!:D

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Just doing some formatting tests... please ignore the following post...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Just a test... please ignore...

    FIGURE 1: Tally Table Code

    [font="Courier New"]

    drop&nbsptable&nbsptally

    --=====&nbspCreate&nbspand&nbsppopulate&nbspthe&nbspTally&nbsptable&nbspon&nbspthe&nbspfly

    &nbspSELECT&nbspTOP&nbsp11000&nbsp--equates&nbspto&nbspmore&nbspthan&nbsp30&nbspyears&nbspof&nbspdates

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspIDENTITY(INT,1,1)&nbspAS&nbspN

    &nbsp&nbsp&nbspINTO&nbspdbo.Tally

    &nbsp&nbsp&nbspFROM&nbspMaster.dbo.SysColumns&nbspsc1,

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMaster.dbo.SysColumns&nbspsc2

    --=====&nbspAdd&nbspa&nbspPrimary&nbspKey&nbspto&nbspmaximize&nbspperformance

    &nbsp&nbspALTER&nbspTABLE&nbspdbo.Tally

    &nbsp&nbsp&nbsp&nbspADD&nbspCONSTRAINT&nbspPK_Tally_N&nbsp

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspPRIMARY&nbspKEY&nbspCLUSTERED&nbsp(N)&nbspWITH&nbspFILLFACTOR&nbsp=&nbsp100

    --=====&nbspAllow&nbspthe&nbspgeneral&nbsppublic&nbspto&nbspuse&nbspit

    &nbsp&nbspGRANT&nbspSELECT&nbspON&nbspdbo.Tally&nbspTO&nbspPUBLIC[/font]

    Test complete... regard all further alarms 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Looks like any decent HTML editor or a Word macro to change spaces to the literal "&nbsp" (without the quotes) will do the trick.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • OK, My turn to test. ...

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Jeff Moden (10/11/2007)


    Looks like any decent HTML editor or a Word macro to change spaces to the literal "&nbsp" (without the quotes) will do the trick.

    Sorry folks... I didn't check my last post...

    The thing in quotes is supposed to be an ampersand {Shift-7} as an escape character followed by "nbsp" (without the quotes, of course).

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I tried using "& nbsp;", as well as html tags such as <pre> (what I had been using). They are ignored.

    Wait. Notice the space before the "nbsp;" above. The ampersand I have to generate using "& amp" (without the space), but if I butt the "nbsp;" against it, a non-breaking space actually appears in the post. So what we enter into the text box goes through (at least) two levels of processing.

    If you enter the code for non-breaking space and click "Preview" you will see the space in the preview. Click "Preview" again, without making any changes and the space disappears. Go back a page and you will see that the non-breaking space has been removed from the text box.

    Weird.

    Tomm Carr
    --
    Version Normal Form -- http://groups.google.com/group/vrdbms

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply