?
Solved

Help Limiting Results of SELECT Statement

Posted on 2006-06-05
10
Medium Priority
?
203 Views
Last Modified: 2008-03-10
I have a SELECT Statement as listed below. It should list companies and contacts along with the associated distribution media.

The problem I have is that any number of contacts can have any combination of 5 distribution media - I need to pull out one record for each contact based on a field called DistributionMedia.MediaPriority - the lower values take precedence.

Based on the query below, is this possible?

Regards,
Danny

SELECT     CompMaint.CompMaintID, CompMaint.ptProductID, CompMaint.ptProdVerID, CompMaint.ptUserID, Company.CompID,
                      Company.CompName,
                      CASE WHEN CompMaint.ptPurMethodID = 2 THEN 'Rental Renewal (Auto Generated)' WHEN CompMaint.ptPurMethodID = 3 THEN 'Subscription Renewal (Auto Generated)'
                       ELSE 'Renewal (Auto Generated)' END AS OrderRef, DistributionMedia.MediaPriority
FROM         CompMaint INNER JOIN
                      Company ON CompMaint.ptCompID = Company.CompID INNER JOIN
                      CompanyContact_Link ON Company.CompID = CompanyContact_Link.ptCompanyID INNER JOIN
                      ContactDistMedia_Link ON CompanyContact_Link.ptContactID = ContactDistMedia_Link.ptContactID INNER JOIN
                      DistributionMedia ON ContactDistMedia_Link.ptMediaID = DistributionMedia.MediaID
WHERE     (CompMaint.ptPurMethodID = 2 OR CompMaint.ptPurMethodID = 3) AND (CompMaint.Renew = 1) AND (CompMaint.OnHold = 0) AND (CompMaint.ptDealerID IS NULL) AND  (Company.ptCompType = 2 OR Company.ptCompType = 4) AND (DistributionMedia.MediaID <> 4) AND (DistributionMedia.MediaID <> 5) AND (CompanyContact_Link.ptContactRoleID = 4)
0
Comment
Question by:LFMSupport
  • 5
  • 3
  • 2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16833999
please try this:

SELECT     cm.CompMaintID, cm.ptProductID, cm.ptProdVerID, cm.ptUserID, c.CompID, c.CompName,
CASE WHEN cm.ptPurMethodID = 2 THEN 'Rental Renewal (Auto Generated)'
         WHEN cm.ptPurMethodID = 3 THEN 'Subscription Renewal (Auto Generated)'
         ELSE 'Renewal (Auto Generated)'
         END AS OrderRef,
         dm.MediaPriority
FROM         CompMaint cm
INNER JOIN Company c
   ON cm.ptCompID = c.CompID
INNER JOIN CompanyContact_Link ccl
  ON c.CompID = ccl.ptCompanyID
INNER JOIN ContactDistMedia_Link cdml
   ON ccl.ptContactID = cdml.ptContactID
INNER JOIN DistributionMedia dm
   ON cdml.ptMediaID = dm.MediaID
WHERE     (cm.ptPurMethodID = 2 OR cm.ptPurMethodID = 3)
AND (cm.Renew = 1)
AND (cm.OnHold = 0)
AND (cm.ptDealerID IS NULL)
AND  (c.ptCompType = 2 OR c.ptCompType = 4)
AND (dm.MediaID <> 4)
AND (dm.MediaID <> 5)
AND (ccl.ptContactRoleID = 4)
AND dm.MediaID = ( SELECT TOP 1 DistributionMedia dmi
                                WHERE cdml.ptMediaID = dmi.MediaID
                                 AND (dmi.MediaID <> 4)
                                 AND (dmi.MediaID <> 5)
                                 ORDER BY dmi.MediaPriority ASC
                              )
                   
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16834073
That returns the same results as the original query I posted.

Danny
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16834149
I see. Let's refine the query a bit then:

SELECT     cm.CompMaintID, cm.ptProductID, cm.ptProdVerID, cm.ptUserID, c.CompID, c.CompName,
CASE WHEN cm.ptPurMethodID = 2 THEN 'Rental Renewal (Auto Generated)'
         WHEN cm.ptPurMethodID = 3 THEN 'Subscription Renewal (Auto Generated)'
         ELSE 'Renewal (Auto Generated)'
         END AS OrderRef,
         dm.MediaPriority
FROM         CompMaint cm
INNER JOIN Company c
   ON cm.ptCompID = c.CompID
INNER JOIN CompanyContact_Link ccl
  ON c.CompID = ccl.ptCompanyID
INNER JOIN ContactDistMedia_Link cdml
   ON ccl.ptContactID = cdml.ptContactID
INNER JOIN DistributionMedia dm
   ON cdml.ptMediaID = dm.MediaID
WHERE     (cm.ptPurMethodID = 2 OR cm.ptPurMethodID = 3)
AND (cm.Renew = 1)
AND (cm.OnHold = 0)
AND (cm.ptDealerID IS NULL)
AND  (c.ptCompType = 2 OR c.ptCompType = 4)
AND (dm.MediaID <> 4)
AND (dm.MediaID <> 5)
AND (ccl.ptContactRoleID = 4)
AND cdml.ptContactID = ( SELECT TOP 1 cdmli.ptContactID
                                  FROM DistributionMedia dmi
                               INNER JOIN ContactDistMedia_Link cdmli
                                 ON cdmli.ptContactID = cdml.ptContactID
                                WHERE cdmli.ptMediaID =
                                 AND (dmi.MediaID <> 4)
                                 AND (dmi.MediaID <> 5)
                                 ORDER BY dmi.MediaPriority ASC
                              )
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:LFMSupport
ID: 16834184
Again, that returns the same number of records.

There is actually an error in the statement you posted that I corrected that may be skewing the results. You wrote:

WHERE cdmli.ptMediaID = AND (dmi.MediaID <> 4)
AND (dmi.MediaID <> 5)
ORDER BY dmi.MediaPriority ASC
)

I changed this to read:

WHERE (dmi.MediaID <> 4)
AND (dmi.MediaID <> 5)
ORDER BY dmi.MediaPriority ASC
)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16834234
Actually, I got the conditions indeed wrong at that place.
Here a corrected version (note that is difficult to suggest such a complex query without sql server or the sample table data ready...)

SELECT     cm.CompMaintID, cm.ptProductID, cm.ptProdVerID, cm.ptUserID, c.CompID, c.CompName,
CASE WHEN cm.ptPurMethodID = 2 THEN 'Rental Renewal (Auto Generated)'
         WHEN cm.ptPurMethodID = 3 THEN 'Subscription Renewal (Auto Generated)'
         ELSE 'Renewal (Auto Generated)'
         END AS OrderRef,
         dm.MediaPriority
FROM         CompMaint cm
INNER JOIN Company c
   ON cm.ptCompID = c.CompID
INNER JOIN CompanyContact_Link ccl
  ON c.CompID = ccl.ptCompanyID
INNER JOIN ContactDistMedia_Link cdml
   ON ccl.ptContactID = cdml.ptContactID
INNER JOIN DistributionMedia dm
   ON cdml.ptMediaID = dm.MediaID
WHERE     (cm.ptPurMethodID = 2 OR cm.ptPurMethodID = 3)
AND (cm.Renew = 1)
AND (cm.OnHold = 0)
AND (cm.ptDealerID IS NULL)
AND  (c.ptCompType = 2 OR c.ptCompType = 4)
AND (dm.MediaID <> 4)
AND (dm.MediaID <> 5)
AND (ccl.ptContactRoleID = 4)
AND cdml.ptContactID = ( SELECT TOP 1 cdmli.ptContactID
                                  FROM DistributionMedia dmi
                               INNER JOIN ContactDistMedia_Link cdmli
                                 ON cdmli.ptMediaID = dmi.MediaID
                                WHERE cdmli.ptContactID = cdml.ptContactID
                                 AND (dmi.MediaID <> 4)
                                 AND (dmi.MediaID <> 5)
                                 ORDER BY dmi.MediaPriority ASC
                              )
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 1500 total points
ID: 16834288
like this ?

SELECT CM.CompMaintID, CM.ptProductID, CM.ptProdVerID, CM.ptUserID, C.CompID
      ,C.CompName
      ,CASE CM.ptPurMethodID
            when 2 THEN 'Rental Renewal (Auto Generated)'
            WHEN 3 THEN 'Subscription Renewal (Auto Generated)'
            ELSE 'Renewal (Auto Generated)'
            END AS OrderRef
      ,DM.MediaPriority
 FROM CompMaint as CM
INNER JOIN Company as C
   ON CM.ptCompID = C.CompID
INNER JOIN CompanyContact_Link as CCL
   ON C.CompID = CCL.ptCompanyID
INNER JOIN ContactDistMedia_Link as CDL
   ON CCL.ptContactID = CDL.ptContactID
INNER JOIN DistributionMedia as DM
   ON CDL.ptMediaID = DM.MediaID
WHERE (CM.ptPurMethodID in (2,3))
  AND (CM.Renew = 1)
  AND (CM.OnHold = 0)
  AND (CM.ptDealerID IS NULL)
  AND (C.ptCompType in (2,4))
  AND (DM.MediaID < 4 OR DM.MediaID > 5)
  AND (CCL.ptContactRoleID = 4)
  AND DM.MediaPriority = (Select Min(MediaPriority)
                            from DistributionMedia as x
                           where x.mediaid=cdl.ptMediaID
                          )


try to avoid using <> or NOT in constructs... as that will allow better index scan choices to be made
e.g.
DM.MediaID<>4
and DMMediaID <> 5
is the same as
AND (DM.MediaID < 4 OR DM.MediaID > 5)
which is easier for SQL Server to index scan...
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16834289
I can appreciate how difficult something like this is without sample data so I'm greatful for your efforts!

I will need to check this tonight - I'll post back in the morning.

Danny
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16834322
its the mediapriority that needs to be minimised per contact ...
not the contactid ....

am i correct..?
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16834377
Ye, that's correct.

For example, if I was in there with all five media types:

Danny - Email - Priority 1
Danny - CD - Priority 2
Danny - Floppy - Priority 3
Danny - Web Download - Priority 4

I would only want to pull out the email record.

HTH
Danny
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16840603
As a small follow-up,

The last queries posted by both Lowfatspread and angelIII return the same set of results. Lowfatspread, thanks for the info RE <> and NOT IN - didn't know that!

Thanks for your efforts so far.
Danny
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

839 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