需要帮助构建单个表的 SQL 查询

发布于 2024-11-08 00:46:30 字数 400 浏览 1 评论 0原文

我有一个包含以下字段的表:

ID 
brandID
productID

我想知道有多少个不同的brandID在此表中至少有一条记录同时包含productID 1和productID 2。例如:

ID|brandID|productID
1 | 1     | 1
2 | 1     | 2
3 | 1     | 2
4 | 2     | 2
5 | 3     | 1
6 | 3     | 1

在上面的示例表中,只有brandID 1有一行包含productID 1 和productID 2。brandID 2 和3 仅包含一种产品。

如何构建查询来获取同时包含 ProductID 1 和 2 的不同品牌 ID 的计数?

I have a table with the following fields:

ID 
brandID
productID

I want to know how many distinct brandIDs have at least one record in this table with both productID 1 and productID 2. For example:

ID|brandID|productID
1 | 1     | 1
2 | 1     | 2
3 | 1     | 2
4 | 2     | 2
5 | 3     | 1
6 | 3     | 1

In the above sample table, only brandID 1 has a row with productID 1 and productID 2. brandIDs 2 and 3 only have one product or the other.

How do I build a query to get this count of distinct brandIDs that contain both productID 1 and 2?

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

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

发布评论

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

评论(6

隔岸观火 2024-11-15 00:46:30
select COUNT(*)
from (
    select brandID
    from MyTable
    where productID in (1, 2)
    group by brandID
    having count(distinct productID) = 2
) a
select COUNT(*)
from (
    select brandID
    from MyTable
    where productID in (1, 2)
    group by brandID
    having count(distinct productID) = 2
) a
冰葑 2024-11-15 00:46:30

如果你的表名为 t,它应该是这样的:

select count(distinct t1.brandID)
from t t1, t t2
where t1.brandID = t2.brandID and t1.productID = 1 and t2.productID = 2

If your table is named t, it should be something like this:

select count(distinct t1.brandID)
from t t1, t t2
where t1.brandID = t2.brandID and t1.productID = 1 and t2.productID = 2
嗼ふ静 2024-11-15 00:46:30

使用子选择通过 productID=1 获取不同的 brandID,然后获取计数:

SELECT COUNT(DISTINCT(brandID)) FROM tbl WHERE BrandID IN (从表中选择不同的品牌ID,其中产品ID=1)和产品ID=2;

Use a sub-select to get distinct brandID with productID=1, and then get the count:

SELECT COUNT(DISTINCT(brandID)) FROM tbl WHERE brandID IN (SELECT DISTINCT brandID FROM tbl WHERE productID=1) AND productID=2;

皇甫轩 2024-11-15 00:46:30
select distinct brandid, count(1) from sampletable 
 where productid in (1, 2) 
 group by brandid having count(1) > 1
select distinct brandid, count(1) from sampletable 
 where productid in (1, 2) 
 group by brandid having count(1) > 1
不必你懂 2024-11-15 00:46:30

对于 SQL Server 2005 及更高版本:

DECLARE @t TABLE(ID INT, brandID INT, productID INT)
INSERT INTO @t
SELECT 1, 1, 1 UNION ALL 
SELECT 2, 1, 2 UNION ALL 
SELECT 3, 1, 2 UNION ALL 
SELECT 4, 2, 2 UNION ALL 
SELECT 5, 3, 1 UNION ALL 
SELECT 6, 3, 2 UNION ALL 
SELECT 7, 3, 4 UNION ALL 
SELECT 8, 3, 5

SELECT * FROM @t    

;WITH cte
AS
(
    SELECT  ROW_NUMBER() OVER(PARTITION BY brandID ORDER BY productID) AS RN, *
    FROM    @t
    WHERE   productID IN (1,2)
)
SELECT  DISTINCT brandID
FROM    cte
WHERE   RN > 1

for SQL Server 2005 and up:

DECLARE @t TABLE(ID INT, brandID INT, productID INT)
INSERT INTO @t
SELECT 1, 1, 1 UNION ALL 
SELECT 2, 1, 2 UNION ALL 
SELECT 3, 1, 2 UNION ALL 
SELECT 4, 2, 2 UNION ALL 
SELECT 5, 3, 1 UNION ALL 
SELECT 6, 3, 2 UNION ALL 
SELECT 7, 3, 4 UNION ALL 
SELECT 8, 3, 5

SELECT * FROM @t    

;WITH cte
AS
(
    SELECT  ROW_NUMBER() OVER(PARTITION BY brandID ORDER BY productID) AS RN, *
    FROM    @t
    WHERE   productID IN (1,2)
)
SELECT  DISTINCT brandID
FROM    cte
WHERE   RN > 1
飘逸的'云 2024-11-15 00:46:30

这就是我要做的:

select distinct brandid from t
where brandid in (select distinct brandid from t where productid = 1)
and brandid in  (select distinct brandid from t where productid = 2)

This is what I would do:

select distinct brandid from t
where brandid in (select distinct brandid from t where productid = 1)
and brandid in  (select distinct brandid from t where productid = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文