[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with view.. need help ASAP

Posted on 2003-11-11
3
Medium Priority
?
284 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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:Ralf Klatt
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

872 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