Access Query ported to SQL

  • There shouldn't be any null records in the serial number column. But you made a good point. The join was wrong. It should have been:

    Where VehiclesToUpdate.SerialNumber is null. When I did that, 157,511 records came up. But there seemed to be no rhyme or reason for these. I'll do some more invesitgation.

    As for the stats. 209,809 total records in VD. Only 177,303 records came up in the VehiclesToUpdate. That leaves 32,506 not 157,511. We may be dealing with duplicates, which I will also investigate and let you know.

  • As far as I know, there are no null Serial Numbers. However, your reply made me take another look at the query and I should have typed:

    Where VehiclesToUpdate.SerialNumber Is Null

    When I did that, I returned 157,511 records. I assume these are VINs that are not in the VehiclesToUpdate that should be.

    As for stats, there are 209,809 records in the VD table. VehiclesToUpdate returns 177,303 records which leaves 32,506 - NOT 157,511. The next thing I will check is duplicates. That could account for some records, but probably not that many.

  • grovelli-262555 (6/20/2013)


    I'm well versed in Access but know very little T-SQL. What does "ROW_NUMBER() over(partition by" do?

    Here is entry from BOL: http://msdn.microsoft.com/en-us/library/ms186734.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • As far as I know, there are no nulls in the Serial Number column. You do bring up a good point though. I took another look at my criterion for the query and discovered that I should have typed:

    Where VehiclesToUpdate.SerialNumber Is Null rather than where VehicleDetail.Serial.Number Is Null. When I executed it this way, I returned 157,511.

    As for the stats, total records in the VehicleDetail are 209,809. However VehiclesToUpdate returns 177,303 which leaves 32,506 NOT 157,511.

    This is my third attempt to post. I hope it works this time.

  • As far as I know, there are no nulls in the Serial Number column. You do bring up a good point though. I took another look at my criterion for the query and discovered that I should have typed:

    Where VehiclesToUpdate.SerialNumber Is Null rather than where VehicleDetail.Serial.Number Is Null. When I executed it this way, I returned 157,511.

    As for the stats, total records in the VehicleDetail are 209,809. However VehiclesToUpdate returns 177,303 which leaves 32,506 NOT 157,511.

    This is my third attempt to post. I hope it works this time.

  • Sorry, feel free to delete the redundant posts.

  • Sean, you may not be there today but I made an interesting discovery. I tried four groupings for totals. For example, I took the group represented by:

    Select *

    From VehiclesToUpdate

    Where Region='Central' And ProgYear='13' And RiskNonriskFlag='R' And MFG = 'MIT' And LeaseCycle='1' And ModelName='LANCER'

    ...and I got 202 units. Then taking that same grouping I tried adding the UnitCount in the RetrunsPros. Guess what. 202 units. I tried this for three other groups and they all matched perfectly. Then I tried it for the total unit count and they didn't match, but that could be explained by the gap cars (source='G') I'm getting closer. I thought you would like to hear the good news.

  • realvilla (6/21/2013)


    Sean, you may not be there today but I made an interesting discovery. I tried four groupings for totals. For example, I took the group represented by:

    Select *

    From VehiclesToUpdate

    Where Region='Central' And ProgYear='13' And RiskNonriskFlag='R' And MFG = 'MIT' And LeaseCycle='1' And ModelName='LANCER'

    ...and I got 202 units. Then taking that same grouping I tried adding the UnitCount in the RetrunsPros. Guess what. 202 units. I tried this for three other groups and they all matched perfectly. Then I tried it for the total unit count and they didn't match, but that could be explained by the gap cars (source='G') I'm getting closer. I thought you would like to hear the good news.

    That is good news. Sounds like you are pretty close to having it figured out.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean, my question is - if the VehiclesToUpdate is obtained from the partitions in the VehicleDetail, why wouldn't they match the VehicleDetail? As you can see from the results, there are not enough cars to satisfy what was stipulated for returns.

    Select count(*), 'Update' as SQLName from VehiclesToUpdate Where Region='Southeast' And ProgYear='12' And LeaseCycle='1'And ModelName='ALTIMA S' And MFG='NIS' And RiskNonRiskFlag='R'

    GO

    Select Sum(UnitCount), 'RCount' as SQLName from ReturnsPros Where Region='Southeast' And ProgYear='12' And LeaseCycle='1'And ModelName='ALTIMA S' And MFG='NIS' And RiskNonRiskFlag='R'

    GO

    Select count(*), 'Detail' as SQLName from VehicleDetail Where Region='Southeast' And ProgYear='12' And LeaseCycle='1'And ModelName='ALTIMA S' And MFG='NIS' And RiskNonRiskFlag='R'

    GO

    The results were:

    101 for VehiclesToUpate

    101 setup from ReturnsPros

    46 in VehicleDetail

  • Sean, I devised a VBA procedure that creates SQL Code. I tried running the code below and look at the results following.

    Select count(*), 'Update' as SQLName from VehiclesToUpdate Where Region='Central' And ProgYear='12' And LeaseCycle='1'And ModelName='GRCHEROKE LAR 4' And MFG='CHR' And RiskNonRiskFlag='R'

    GO

    Select Sum(UnitCount), 'RCount' as SQLName from ReturnsPros Where Region='Central' And ProgYear='12' And LeaseCycle='1'And ModelName='GRCHEROKE LAR 4' And MFG='CHR' And RiskNonRiskFlag='R'

    GO

    Select count(*), 'Detail' as SQLName from VehicleDetail Where Region='Central' And ProgYear='12' And LeaseCycle='1'And ModelName='GRCHEROKE LAR 4' And MFG='CHR' And RiskNonRiskFlag='R'

    GO

    VehiclesToUpdate: 101

    ReturnsPros: 101

    VehicleDetail: 46

    I'm trying to think of a way to change VehiclesToUpdate. There are obvioiusly not enough records to accomodate ReturnsPros.

  • I can't seem to find in this thread now but iirc you said that the returns was data from an outside source? How do you validate that the returns they are sending are realistic? Seems to me that the returns data is more than went out the door.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean, here is the "unsolved mystery". When I run your program pretty much as written, I get about 70,000 rows that don't update (null Return dates). Included in those rows is the following grouping.

    Yet when I tag this code right after where v.RowNum <= r.UnitCount, I get 623 records for VehiclesToUdate, ReturnsPros and VehicleDetail the way I should.

    How can this be?

    Select count(*) as UpdateCount, 'Update' as SQLName from VehiclesToUpdate Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

    Select Sum(UnitCount) as ReturnsCount, 'RCount' as SQLName from ReturnsPros Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

    Select count(*) as VDCount, 'Detail' as SQLName from VehicleDetail Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

  • realvilla (6/27/2013)


    Sean, here is the "unsolved mystery". When I run your program pretty much as written, I get about 70,000 rows that don't update (null Return dates). Included in those rows is the following grouping.

    Yet when I tag this code right after where v.RowNum <= r.UnitCount, I get 623 records for VehiclesToUdate, ReturnsPros and VehicleDetail the way I should.

    How can this be?

    Select count(*) as UpdateCount, 'Update' as SQLName from VehiclesToUpdate Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

    Select Sum(UnitCount) as ReturnsCount, 'RCount' as SQLName from ReturnsPros Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

    Select count(*) as VDCount, 'Detail' as SQLName from VehicleDetail Where Region='Central' And ProgYear='13' And LeaseCycle='1'And ModelName='FUSION I4 SE' And MFG='FRD' And RiskNonRiskFlag='R'

    GO

    Wow this is going to be tough to figure out from here without a sample dataset where we can reproduce this. Can you make a smaller dataset where this issue occurs? It has been several days since I looked this in detail and don't really remember all the details. It will take me a bit to get back up to speed and I am swamped at work right now. I will try to get back here early next week and see what is happening. Sorry I can't help much more right now.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean, I know you're busy so any help you give me is appreciated. It doesn't go with the territory that you have to support something you gave me as a sample but maybe you're intrigued enough to at least want to discover what the problem is.

    I have tried so many things. The latest attempt was to try the code on a smaller sample of data. So I named a table with only FUSION I4 SE cars as "VehicleDetail" and renamed the original table as VehicleDetailTemp.

    I ran your original code only changing the following (new line boldface):

    Update VehiclesToUpdate

    Set NewReturnDate = ReturnDate

    Where ReturnDate Is Null

    There were 3728 records with a null return date. Only 683 of them actually updated. The funny thing is that VehiclesToUpdate is 3728 before the update occurs. So why would the update query be selective? Is it because of duplicate records in VehiclesToUpdate?

    I think your idea about sending you a data set is great. However, isn't there a limit as to how many records can be sent? What size dataset would you need?

  • Correction:

    WHERE NewReturnDate is null

Viewing 15 posts - 31 through 45 (of 58 total)

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