Solved

Problem with view.. need help ASAP

Posted on 2003-11-11
3
261 Views
Last Modified: 2012-08-13
Here is my view...

--Begin View
Select Addresses.IDNumber, Addresses.AddressID,
      'OfficeID' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then COffice.OfficeID
                  When Addresses.IDNumber > 999999 Then MOffice.OfficeID
            End,
      'OfficeName' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then COffice.OfficeName
                  When Addresses.IDNumber > 999999 Then MOffice.OfficeName
            End,
      'OfficeLocation' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then COffice.OfficeLocation
                  When Addresses.IDNumber > 999999 Then MOffice.OfficeLocation
            End,
      'MemberType' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then Campgrounds.Account
                  When Addresses.IDNumber > 999999 Then Members.MemberType
            End,
      'Account' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then Campgrounds.Account
                  When Addresses.IDNumber > 999999 Then Members.Account
            End,
      'Name' =
            Case
                  When Addresses.IDNumber Between 1 and 999999 Then Campgrounds.Campground
                  When Addresses.IDNumber > 999999 Then Cast(Replace(RTrim(LTrim(Members.Title + ' ' + Members.Firstname + ' ' + Members.Middlename + ' ' + Members.Lastname + ' ' + Members.Suffix)), '  ', ' ') As varchar(100))
            End,
      Addresses.AddressType, Addresses.ShipTo, Addresses.Street1, Addresses.Street2, Addresses.City,
      Addresses.Abrv, Addresses.State, Addresses.Zipcode, Addresses.Country,
            
      Addresses.PostalZone, Addresses.Mailings, Addresses.Packages, Addresses.Commissions,
      Addresses.Important, Addresses.StartDate, Addresses.EndDate
From
      ptblAddresses As Addresses left outer join
            ptblMembers As Members on Addresses.IDNumber = Members.IDNumber left outer join
            ptblCampgrounds As Campgrounds on Addresses.IDNumber = Campgrounds.IDNumber left outer join
            ptblOffices As MOffice On Members.Office = MOffice.OfficeID and MOffice.OfficeID = '1' left outer join
            ptblOffices As COffice On COffice.OfficeID = '1'
Where (MOffice.OfficeID = '1' or COffice.OfficeID = '1') And Addresses.EndDate > GetDate()
--End View

The way i'm storing addresses is every address that has been mailed to has been saved and cannot be deleted (to look up where packages, etc... went and not having the "New" address replace the one in which it was actually sent.)  When a change is made to the address the enddate gets set to GetDate() thereby making it no longer effective.  A person can have 2 address types.  Temporary and Permanent (self explanitory).  A temporary address takes precedence over the permanent address if GetDate is between Addresses.StartDate and Addresses.EndDate else should display the Permanent address.

The problem lies in that it is returning 2 results for members with an active temporary address.  (Both Home and Temporary).  Now the EndDate on the permanent address is always 12/31/2050 while active so I could get the result by using Top 1 and order by Addresses.EndDate (SHOULD return the temporary address).  However whenever I specify the Top clause nothing is being returned at all (and the query takes 10 seconds in leiu of .3 seconds with above query).

Generally this view is called specifying IDNumber(s) (using in clause) and by various bitfields (mailing, packages, important, commissions) being set to true.

This is used for batches of members and/or campgrounds (why stored procedure won't work correctly) in which I need 1 address per distinct idnumber.  Any ideas?  I would greatly appreciate it...

-ketapillar
0
Comment
Question by:ketapillar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 9726239
I think you could expand your WHERE clause to check the addressType and only select a 'Permanent' address if a 'Temporary' address didn't exist, like so:

Where (MOffice.OfficeID = '1' or COffice.OfficeID = '1') And Addresses.EndDate > GetDate()
And (Addresses.AddressType = 'Temporary' OR NOT EXISTS(
      SELECT 1
      FROM Addresses A2
      WHERE A2.IDNumber = Addresses.IDNumber
      AND A2.AddressType = 'Temporary'
      AND A2.EndDate > GETDATE()))
0
 
LVL 15

Expert Comment

by:Raisor
ID: 9727862
Hi,

Which address is the primary address? "Temp" or "Home"?

If it is "Temp" I would state like:

create/alter procedure yourProcedure
(
     @yourCondition int (or whatever)
)
as
declare @chkTemp int
select @chkTemp = count(yourValue) from yourTable where yourData = @yourCondition
if @chkTemp = 0
     begin
          ... (yourScriptA)
     end
if @chkTemp > 0
     begin
          ... (yourScriptB)
     end

If "Home" is the important condition you'll just have to change "chkTemp" to "chkHome" in the above sequence.

Best regards, Raisor

0
 
LVL 3

Author Comment

by:ketapillar
ID: 9741562
Thanks ScottPletcher it seems to be working fine now.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

737 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