SQL Server-如何根据两个或多列的数据组合过滤数据

发布于 2025-02-08 21:04:45 字数 1362 浏览 1 评论 0原文

我试图根据两个字段(例如产品类别和年份)在仪表板中过滤数据。

我有数据,其中一些产品在2007年,2008年和2009年出售,而另一些产品仅在2008年才出售。我想排除数据,其中只有2008年的销售年份销售数据。

例如,

品牌
玩具2008
鞋类2008
炊具2008
TOYS2008
TOYSTOOSS 2008
2007鞋类2007
鞋类鞋类2009
2007年衣服2008年

期望值输出:

BrandnameYear
Toys2008
鞋类2008
2009 TOYSTOYS 2009
TOYS2009
TOYS 2007 TOMEWEAR 2009鞋类2007
鞋类2007

我尝试了SQL,但基本上不在SQL上工作

Select BrandName, Concat(BrandName,Year(UpdateDate)), SOUNDEX(Concat(BrandName,Year(UpdateDate))) as Data from Dbo.DimProduct
Group by BrandName, Concat(BrandName,Year(UpdateDate))
having Count(SOUNDEX(Concat(BrandName,Year(UpdateDate)))) > 1
Order by SOUNDEX(Concat(BrandName,Year(UpdateDate)))

,如果只有一个唱片(例如Chald2008和Cookware2008),则将其排除在结果集之外。

I was trying to filter out data in the dashboard based on two fields, for example, product category and year.

I have data where some products are sold in 2007, 2008, and 2009, while others are sold only in 2008. I want to exclude data where sale data for products with only 2008 year as the sale year.

For example,

BrandNameYear
Toys2008
Footwear2008
Cookware2008
Toys2009
Toys2007
Footwear2009
Footwear2007
Clothes2008

Expected output:

BrandNameYear
Toys2008
Footwear2008
Toys2009
Toys2007
Footwear2009
Footwear2007

I tried following SQL but does not seem to work

Select BrandName, Concat(BrandName,Year(UpdateDate)), SOUNDEX(Concat(BrandName,Year(UpdateDate))) as Data from Dbo.DimProduct
Group by BrandName, Concat(BrandName,Year(UpdateDate))
having Count(SOUNDEX(Concat(BrandName,Year(UpdateDate)))) > 1
Order by SOUNDEX(Concat(BrandName,Year(UpdateDate)))

Basically, if there is only one record like Clothes2008 and Cookware2008, exclude them from the resultset.

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

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

发布评论

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

评论(1

梦情居士 2025-02-15 21:04:45

请尝试以下解决方案。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, ProductCategory VARCHAR(30), _year CHAR(4));
INSERT INTO @tbl (ProductCategory, _year) VALUES
('Toys', '2008'),
('Footwear', '2008'),
('Cookware', '2008'),
('Toys', '2009'),
('Toys', '2007'),
('Footwear', '2009'),
('Footwear', '2007'),
('Clothes', '2008');
-- DDL and sample data population, end

;WITH rs AS
(
    SELECT * 
       , total_count = COUNT(*) OVER (PARTITION BY ProductCategory ORDER BY ProductCategory)
       , _2008_count = SUM(IIF (_year='2008', 1, 0)) OVER (PARTITION BY ProductCategory ORDER BY ProductCategory)
    FROM @tbl
)
SELECT ID, ProductCategory, _year 
FROM rs
WHERE total_count <> _2008_count
ORDER BY ID;

输出

+----+-----------------+-------+
| ID | ProductCategory | _year |
+----+-----------------+-------+
|  1 | Toys            |  2008 |
|  2 | Footwear        |  2008 |
|  4 | Toys            |  2009 |
|  5 | Toys            |  2007 |
|  6 | Footwear        |  2009 |
|  7 | Footwear        |  2007 |
+----+-----------------+-------+

Please try the following solution.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, ProductCategory VARCHAR(30), _year CHAR(4));
INSERT INTO @tbl (ProductCategory, _year) VALUES
('Toys', '2008'),
('Footwear', '2008'),
('Cookware', '2008'),
('Toys', '2009'),
('Toys', '2007'),
('Footwear', '2009'),
('Footwear', '2007'),
('Clothes', '2008');
-- DDL and sample data population, end

;WITH rs AS
(
    SELECT * 
       , total_count = COUNT(*) OVER (PARTITION BY ProductCategory ORDER BY ProductCategory)
       , _2008_count = SUM(IIF (_year='2008', 1, 0)) OVER (PARTITION BY ProductCategory ORDER BY ProductCategory)
    FROM @tbl
)
SELECT ID, ProductCategory, _year 
FROM rs
WHERE total_count <> _2008_count
ORDER BY ID;

Output

+----+-----------------+-------+
| ID | ProductCategory | _year |
+----+-----------------+-------+
|  1 | Toys            |  2008 |
|  2 | Footwear        |  2008 |
|  4 | Toys            |  2009 |
|  5 | Toys            |  2007 |
|  6 | Footwear        |  2009 |
|  7 | Footwear        |  2007 |
+----+-----------------+-------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文