1

I have 2 tables.

Manufacturers

  • idManufacturer

Coupons

  • idCoupon

  • idManufacturer

  • discountPercent

  • couponCode

  • startRange

  • endRange

I need to get all manufacturers along with the coupon with the greatest "discountPercent", if any are availabe for that manufacturer. Each manufacturer should only have one coupon.

This is the SQL I have, which works, besides in specific scenarios (see below):

    DECLARE @maxdiscount TABLE
(
      idmanufacturer INT
    , discperc INT
)

INSERT INTO
    @maxdiscount
SELECT
    p.idmanufacturer
    , max(discperc) 
FROM
    cp_manufacturers p
    LEFT OUTER JOIN coupons c ON p.idmanufacturer = c.idmanufacturer
GROUP BY
    p.idmanufacturer


SELECT
    m.idmanufacturer
    , m.discperc
    , couponcode
FROM
    @maxdiscount m 
    LEFT OUTER JOIN coupons c ON  c.idmanufacturer = m.idmanufacturer AND c.discperc = m.discperc  

The problem is when there are two coupons for a manufacturer with the SAME discountPercent, then 2 coupons are returned for that manufacturer. In this case, I only want to pull up the coupon with the larger start/end range.

I'm stuck on this part, any help would be appreciated.

Thank you.

4

4 回答 4

3

You could change last part

SELECT
    m.idmanufacturer
    , m.discperc
    , c.couponcode

FROM
    @maxdiscount m 
    LEFT OUTER JOIN coupons c ON  c.idmanufacturer = m.idmanufacturer AND c.discperc = m.discperc 
    WHERE c.couponcode IS NULL OR
       (c.endRange - c.startRange) = 
       (SELECT MAX(c1.endRange - c1.startRange) 
        FROM coupon c1 
        WHERE c1.idmanufacturer = c.idmanufacturer and c1.discperc = c.disperc)
于 2012-09-05T17:19:39.327 回答
2

Try this

  Select m.*, c.*
  From Manufacturers m
     Left Join Coupons c 
        On c.id_Coupon = 
           (Select id_Coupon
            From Coupons
            Where idManufacturer = m.idManufacturer
                And discountPercent = 
                   (Select Max(discountPercent)
                    From Coupons 
                    Where idManufacturer = m.idManufacturer)
                And EndRange-StartRange =
                   (Select Max(EndRange-StartRange) rangeLength
                    From Coupons
                    Where idManufacturer = m.idManufacturer
                        And discountPercent = 
                           (Select Max(discountPercent)
                            From Coupons 
                            Where idManufacturer = m.idManufacturer)))

this shorter version might also work, if there is only one row wth that manufacturer, Discount percent, and Range length...

  Select m.*, c.*
  From Manufacturers m
     Left Join Coupons c 
       On idManufacturer = m.idManufacturer
          And discountPercent = 
             (Select Max(discountPercent)
              From Coupons 
              Where idManufacturer = m.idManufacturer)
          And EndRange-StartRange =
             (Select Max(EndRange-StartRange) rangeLength
              From Coupons
              Where idManufacturer = m.idManufacturer
                  And discountPercent = 
                     (Select Max(discountPercent)
                      From Coupons 
                      Where idManufacturer = m.idManufacturer))
于 2012-09-05T17:22:37.867 回答
1

If you are using SQL server 2005+ then try:

Chnage your Insert statement to

;WITH CTE AS(
      SELECT *,ROW_NUMBER() OVER(PARTITION BY P.IDMANUFACTURER ORDER BY DISCPERC DESC,
                                   DATEDIFF(DD,STARTRANGE,ENDRANGE) DESC) AS ROW_NUM
      FROM   CP_MANUFACTURERS P     
      LEFT OUTER JOIN  COUPONS C 
      ON     P.IDMANUFACTURER = C.IDMANUFACTURER )
INSERT INTO @MAXDISCOUNT 
SELECT  IDMANUFACTURER ,DISCPERC FROM CTE
WHERE   ROW_NUM=1
于 2012-09-05T17:24:34.947 回答
1

Without the start/end stuff (which is rather vague ...)

WITH guess AS (
        SELECT idmanufacturer
        , MAX(disperc) AS disperc
        FROM coupons
        GROUP BY idmanufacturer
        )
SELECT
    m.idmanufacturer
    , COALESCE(g.discperc, 0)
FROM maxdiscount m  
LEFT JOIN guess g ON  g.idmanufacturer = m.idmanufacturer 
        ;
于 2012-09-05T17:50:51.963 回答