SQL Server-如何根据两个或多列的数据组合过滤数据
我试图根据两个字段(例如产品类别和年份)在仪表板中过滤数据。
我有数据,其中一些产品在2007年,2008年和2009年出售,而另一些产品仅在2008年才出售。我想排除数据,其中只有2008年的销售年份销售数据。
例如,
品牌 | 年 |
---|---|
玩具 | 2008 |
鞋类 | 2008 |
炊具 | 2008 |
TOYS | 2008 |
TOYS | TOOSS 2008 |
2007鞋类 | 2007 |
鞋类 | 鞋类2009 |
2007年衣服 | 2008年 |
期望值输出:
Brandname | Year |
---|---|
Toys | 2008 |
鞋类 | 2008 |
2009 TOYS | TOYS 2009 |
TOYS | 2009 |
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,
BrandName | Year |
---|---|
Toys | 2008 |
Footwear | 2008 |
Cookware | 2008 |
Toys | 2009 |
Toys | 2007 |
Footwear | 2009 |
Footwear | 2007 |
Clothes | 2008 |
Expected output:
BrandName | Year |
---|---|
Toys | 2008 |
Footwear | 2008 |
Toys | 2009 |
Toys | 2007 |
Footwear | 2009 |
Footwear | 2007 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下解决方案。
SQL
输出
Please try the following solution.
SQL
Output