Solved

Problem with view.. need help ASAP

Posted on 2003-11-11
3
234 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks ScottPletcher it seems to be working fine now.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how the fundamental information of how to create a table.

743 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