2

再次引用此SQL products/productsales

当主键由两列而不是一列组成时,我怎么能做这样的事情?

所以 products 有两列作为 PK,productsales 有两列作为 FK。

这是使用 1 列键的解决方案:

SELECT p.[name]
FROM products p
WHERE p.product_id in (SELECT s.product_id
    FROM productsales s
    WHERE s.[date] between @dateStart and @dateEnd
    GROUP BY s.product_id
    HAVING Sum(s.quantity) > @X )
4

6 回答 6

3

这些方面的东西可能会起作用

SELECT p.[name]
FROM products p
JOIN (SELECT s.key1, s.key2
    FROM productsales s
    WHERE s.[date] between @dateStart and @dateEnd
    GROUP BY s.product_id
    HAVING Sum(s.quantity) > @X ) as a on a.key1 = p.key1 and a.key2 = p.key2 
于 2009-02-10T11:54:40.607 回答
2

看起来上面有一些答案会起作用,但只是为了向您抛出另一个解决方案,以防它在您的情况下效果更好:

SELECT
     P.name
FROM
     Products P
WHERE
     EXISTS
     (
          SELECT
               *
          FROM
               ProductSales PS
          WHERE
               PS.product_id = P.product_id AND
               PS.date BETWEEN @date_start AND @date_end
          GROUP BY
               PS.product_id
          HAVING
               SUM(PS.quantity) > @cutoff_quantity
     )

此方法的性能往往比使用 Brownstone 先生给出的 GROUP BY 方法的 INNER JOIN 更差,但在某些情况下,取决于您的 @cutoff_quantity 值和表大小,它可能会执行得更好。

于 2009-02-10T15:17:23.503 回答
2

也许这样的事情会做:

SELECT p.first_col_id,p.second_col_id 
FROM products p
JOIN productsales s
   ON s.first_col_id = p.first_col_id 
  AND s.second_col_id = p.second_col_id 
  AND s.[date] between @dateStart and @dateEnd
GROUP BY p.first_col_id,p.second_col_id 
HAVING Sum(s.quantity) > @X )
于 2009-02-10T11:58:03.920 回答
1

尝试:

SELECT p.[name]
FROM products p
WHERE (p.product_key1, p.product_key2) in
   (SELECT s.product_key1, s.product_key2
    FROM productsales s
    WHERE s.[date] between @dateStart and @dateEnd
    GROUP BY s.product_key1, s.product_key2
    HAVING Sum(s.quantity) > @X )
于 2009-02-10T12:01:42.257 回答
1

我喜欢这样做:

;WITH t 
     AS (SELECT s.product_id 
         FROM   productsales s 
         WHERE  s.[date] BETWEEN @dateStart AND @dateEnd 
         GROUP  BY s.product_id 
         HAVING Sum(s.quantity) > @X) 
SELECT p.[name] 
FROM   products p 
       JOIN t 
         ON p.pk1 = t.fk1 
            AND p.pk2 = t.fk2 
于 2009-02-10T12:05:26.680 回答
0

一键重构

SELECT s.product_id, p.[name]
FROM products p JOIN productsales s ON p.product_id=s.product_id
WHERE s.[date] between @dateStart and @dateEnd
GROUP BY s.product_id, p.[name]
HAVING Sum(s.quantity) > @X )

两个键相同:

SELECT s.k1, s.k2, p.[name]
FROM products p JOIN productsales s ON (p.k1=s.k1 AND p.k2=s. k2)
WHERE s.[date] between @dateStart and @dateEnd
GROUP BY s.k1, s.k2, p.[name]
HAVING Sum(s.quantity) > @X )
于 2009-02-10T12:02:29.243 回答