Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with view.. need help ASAP

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

609 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