Sql 查找“产生的总收入”;男";“中的客户”;电子";类别但产出应显示按产品分类的总收入
我加入了3个表格,以获得电子产品类别下每个子类别的总收入Sql 查找“产生的总收入”;男";“中的客户”;电子";类别但产出应显示按产品分类的总收入,sql,sql-server,Sql,Sql Server,我加入了3个表格,以获得电子产品类别下每个子类别的总收入 select prod_subcat as subcat, sum(total_amt) as total_revenue from transection left join customer_details on transection.cust_id = customer_details.customer_ID left join [dbo].[Product_cat_info] on [dbo].[transection]
select prod_subcat as subcat, sum(total_amt) as total_revenue
from transection
left join customer_details
on transection.cust_id = customer_details.customer_ID
left join [dbo].[Product_cat_info]
on [dbo].[transection].[prod_cat_code] = [dbo].[Product_cat_info].[Prod_Cat_cod]
where Gender like 'M' and prod_Cat in (
select prod_Cat
from [dbo].[Product_cat_info]
where prod_Cat like 'Electronics'
)
group by [prod_subcat]
但在产出方面,我没有得到每个子类别的具体收入,而是得到每个子类别的一个共同价值(电子产品类别产生的总收入)
select prod_subcat as subcat, sum(total_amt) as total_revenue
from transection
left join customer_details
on transection.cust_id = customer_details.customer_ID
left join [dbo].[Product_cat_info]
on [dbo].[transection].[prod_cat_code] = [dbo].[Product_cat_info].[Prod_Cat_cod]
where Gender like 'M' and prod_Cat in (
select prod_Cat
from [dbo].[Product_cat_info]
where prod_Cat like 'Electronics'
)
group by [prod_subcat]
我得到的输出是
subcat total_revenue
Personal Appliances 5702069
Mobiles 5702069
Computers 5702069
Audio and video 5702069
Cameras 5702069
由于您已加入此表,请在中删除此子查询以导致更多重复,并首先替换此下面的查询。此外,请将prod_cat添加到group by
prod_Cat in (select prod_Cat from [dbo].
[Product_cat_info]
where prod_Cat like 'Electronics')
带有和类似产品的“电子设备”
SELECT prod_subcat as subcat,
gender,
sum(total_amt) as total_revenue
from tbl_Tran T1
left join tbl_Cust C1
on T1.cust_id = C1.customer_ID
left join Prod_cat_info P1
on T1.prod_cat_code = P1.prod_cat_code
and T1.prod_subcat_code=P1.prod_sub_cat_code
where Gender like 'M' and prod_Cat in (
select prod_Cat
from Prod_cat_info
where prod_Cat like 'Electronics'
)
group by prod_subcat,Gender
将tbl_tran替换为事务
如果还不算太晚,我建议更改您的桌子名称。交易中没有字母“e”。还有,为什么像“M”这样使用?换成“M”怎么样。Like用于通配符搜索。当您知道想要的值时,应该使用=。为了在这方面真正有所帮助,我们需要一些细节
prod_Cat like‘Electronics’
应该是prod_Cat=‘Electronics’
like是一项昂贵的操作;如果你确定prod_Cat=‘Electronics’就这样做。性别也一样。将帮助您更快地获得所需的结果集。您好,先生,我尝试了您建议的方法,但输出仍然相同。我知道您忘记在分组中添加产品类别了