SQL组合键查询

发布于 2024-07-13 14:26:01 字数 473 浏览 14 评论 0原文

再次引用这个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 )

to reference this again SQL products/productsales

how could i do something like that when the primary key is made up of two columns and not one?

so products has two columns as PK, and productsales has two columns as FK.

here is the solution with a 1-column key:

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔少女心 2024-07-20 14:26:01

沿着这些思路的东西可能会起作用

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 

Something along these lines could work

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 
绝不放开 2024-07-20 14:26:01

也许像这样的事情会做:

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 )

Maybe something like this would do:

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 )
舟遥客 2024-07-20 14:26:01

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

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
     )

此方法往往会比 Mr. 给出的带有 GROUP BY 方法的 INNER JOIN 表现更差。 Brownstone,但在某些情况下,根据您的 @cutoff_quantity 值和表大小,它可能会表现得更好。

It looks like there are a few answers above that will work, but just to throw another solution at you in case it works better in your case:

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
     )

This method will tend to perform worse than the INNER JOIN with a GROUP BY method given by Mr. Brownstone, but in some situations depending on your value of @cutoff_quantity and table sizes it could perform better.

新雨望断虹 2024-07-20 14:26:01

尝试:

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 )

Try:

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 )
旧夏天 2024-07-20 14:26:01

我喜欢这样做:

;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 

I like to do it like this:

;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 
层林尽染 2024-07-20 14:26:01

用一键重构

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 )

Refactored with one key

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 )

Same on two keys:

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文