34,590
社区成员
发帖
与我相关
我的任务
分享
s_ParterCategory s_sonCategory Brand_name SKU_name Specifications PIMcount FK_Iid
头发护理用品 洗发水 飘柔 收款机(OA) 15ml 2123 1
头发护理用品 洗发水 飘柔 收款机(OA) 15ml 212 1
口腔护理用品 牙膏 佳洁士 收款机(OA2) 15ml 212 2
select FK_Iid ,s_ParterCategory,s_sonCategory, COUNT(distinct Brand_name) as brandCount,
count(distinct SKU_name ) SkuCount,COUNT(distinct Brand_name+SKU_name+Specifications) as pgSku_name,SUM(PIMcount) as pgPimcount
from F_Commodity
where Brand_name in( '飘柔','海飞丝','潘婷','沙宣','伊卡璐','威娜','佳洁士')
group by FK_Iid ,s_sonCategory,s_ParterCategory
FK_Iid s_ParterCategory s_sonCategory brandCount SkuCount pgSku_name pgPimcount
----------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------- ----------- ----------- -----------
1 头发护理用品 洗发水 1 1 1 2335
2 口腔护理用品 牙膏 1 1 1 212
(2 行受影响)
select FK_Iid ,s_ParterCategory,s_sonCategory,COUNT(distinct Brand_name+SKU_name+Specifications) as pgSku_name
from F_Commodity
where Brand_name in( '飘柔','海飞丝','潘婷','沙宣','伊卡璐','威娜','佳洁士')
group by FK_Iid ,s_sonCategory,s_ParterCategory
select FK_Iid ,s_ParterCategory,s_sonCategory,COUNT(distinct Brand_name+SKU_name+Specifications) as pgSku_name
from F_Commodity
group by FK_Iid ,s_sonCategory,s_ParterCategory
FK_Iid s_ParterCategory s_sonCategory pgSku_name
----------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- -----------
1 头发护理用品 洗发水 1
2 口腔护理用品 牙膏 1
(2 行受影响)
--按道理应该是一个count、一个count的算,然后连接在一起
--方法比较笨,等高手贴简便方法
if OBJECT_ID('F_Commodity') is not null
drop table F_Commodity
go
create table F_Commodity (
s_ParterCategory nvarchar(10)
,s_sonCategory nvarchar(10)
,Brand_name nvarchar(10)
,SKU_name nvarchar(10)
,Specifications nvarchar(10)
,PIMcount int
,FK_Iid int
)
go
insert F_Commodity
select '头发护理用品','洗发水','飘柔','收款机(OA)','15ml',2123,1 union all
select '头发护理用品','洗发水','飘柔','收款机(OA)','15ml',212,1 union all
select '口腔护理用品','牙膏','佳洁士','收款机(OA2)','15ml',212,2
go
select
a.*, b.pgSku_name
from
(
select
FK_Iid
,s_ParterCategory
,s_sonCategory
,brandCount = count(distinct Brand_name)
,SkuCount = count(distinct SKU_name),
sum(case when (s_ParterCategory='头发护理用品' and Brand_name in ('飘柔','海飞丝','潘婷','沙宣','伊卡璐','威娜'))
or (s_ParterCategory='口腔护理用品' and Brand_name ='佳洁士')
then PIMcount
else 0
end) pgPimcount
from
F_Commodity
group by
FK_Iid ,s_sonCategory,s_ParterCategory
) a
left join
(
select FK_Iid, s_ParterCategory, s_sonCategory,
sum(case when (s_ParterCategory='头发护理用品' and Brand_name in ('飘柔','海飞丝','潘婷','沙宣','伊卡璐','威娜'))
or(s_ParterCategory='口腔护理用品' and Brand_name ='佳洁士')
then 1
else 0
end) pgSku_name
from
(select distinct
s_ParterCategory,s_sonCategory,Brand_name,SKU_name,Specifications,FK_Iid
from
F_Commodity
)a
group by
FK_Iid ,s_sonCategory,s_ParterCategory
) b
on a.FK_Iid = b.FK_Iid and a.s_sonCategory = b.s_sonCategory and a.s_ParterCategory = b.s_ParterCategory
--结果
/*
FK_Iid s_ParterCategory s_sonCategory brandCount SkuCount pgPimcount pgSku_name
----------- ---------------- ------------- ----------- ----------- ----------- -----------
1 头发护理用品 洗发水 1 1 2335 1
2 口腔护理用品 牙膏 1 1 212 1
(2 行受影响)
*/
/*
*************************************
* T-MAC 小编 *
* -->努力成长中 *
* -->梦想DBA *
*************************************
*/
if OBJECT_ID('F_Commodity') is not null
drop table F_Commodity
go
create table F_Commodity (s_ParterCategory varchar(100) ,s_sonCategory varchar(100), Brand_name varchar(100), SKU_name varchar(100),Specifications varchar(10),PIMcount int,FK_Iid int)
insert F_Commodity select
'头发护理用品', '洗发水', '飘柔', '收款机(OA)' , '15ml' , 2123 , 1 union select
'头发护理用品', '洗发水', '飘柔', '收款机(OA)' , '15ml' , 212 , 1 union select
'口腔护理用品', '牙膏' , '佳洁士', '收款机(OA2)' , '15ml' , 212 , 2
go
select FK_Iid ,s_ParterCategory,s_sonCategory,
sum(case when s_ParterCategory='头发护理用品' and (Brand_name='飘柔' or Brand_name='海飞丝' OR Brand_name ='潘婷' or Brand_name='沙宣' or Brand_name ='伊卡璐' OR Brand_name='威娜') then 1
when s_ParterCategory='口腔护理用品' and (Brand_name ='佳洁士' )then 1
else 0 end) pgSku_name
from
(select distinct
s_ParterCategory,s_sonCategory,Brand_name,SKU_name,Specifications,FK_Iid
from
F_Commodity
)a
group by
FK_Iid ,s_sonCategory,s_ParterCategory
select FK_Iid
,s_ParterCategory
,s_sonCategory,
sum(case when s_ParterCategory='头发护理用品' and (Brand_name='飘柔' or Brand_name='海飞丝' OR Brand_name ='潘婷' or Brand_name='沙宣' or Brand_name ='伊卡璐' OR Brand_name='威娜') then 1
when s_ParterCategory='口腔护理用品' and (Brand_name ='佳洁士' )then 1
else 0 end) pgSku_name
from
(select distinct
s_ParterCategory,s_sonCategory,Brand_name,SKU_name,Specifications,PIMcount,FK_Iid
from
F_Commodity
)a
group by
FK_Iid ,s_sonCategory,s_ParterCategory