Solved

Problem with view.. need help ASAP

Posted on 2003-11-11
3
239 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
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

867 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

16 Experts available now in Live!

Get 1:1 Help Now