一个表的复杂查询,紧急在线!!!!!!!!!!1

lyszjs 2005-04-08 10:39:51
表字段:
门店号,销售时间, 销售额, 会员号

001 2004-07-07 2323
001 2004-07-07 32323 0125
001 2004-08-07 323 0453

会员号可以为空,
现在要查询出,在一段时间内,有多少是以会员身份消费的,会员的销售额,以及门店的总销售额
还有就是会员销售占总销售的比例。
要查询的字段:门店号,消费会员数,会员销售额,总销售额,占比(会员销售额/总销售额)
...全文
144 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
天地客人 2005-04-08
  • 打赏
  • 举报
回复
select
门店号,
消费会员数 = count(distinct 会员号) - max(case when 会员号 is null then 1 else 0 end),
会员销售额 = sum(case when 会员号 is not null then 销售额 else 0 end),
总销售额 = isnull(sum(销售额),0),
占比 = (sum(case when 会员号 is not null then 销售额 else 0 end)*1.0)/(case when isnull(sum(销售额),0)=0 then 1 else sum(销售额) end)
from

where
销售时间 Between '20005-4-1' and '2005-4-10' --时间范围条件
group by
门店号
yonghengdizhen 2005-04-08
  • 打赏
  • 举报
回复
yo yoo,这也叫复杂查询啊
xluzhong 2005-04-08
  • 打赏
  • 举报
回复
select
门店号,
消费会员数 = count(distinct 会员号) - max(case when 会员号 is null then 1 else 0 end),
会员销售额 = sum(case when 会员号 is not null then 销售额 else 0 end),
总销售额 = isnull(sum(销售额),0),
占比 = (sum(case when 会员号 is not null then 销售额 else 0 end)*1.0)/(case when isnull(sum(销售额),0)=0 then 1 else sum(销售额) end)
from

where
... --时间范围条件
group by
门店号
chenqianlong 2005-04-08
  • 打赏
  • 举报
回复
select
门店号,
消费会员数=count(distinct(会员号)) --這裡可能出錯
会员销售额=sum(case when 会员号 is not null then 销售额 else 0 end),
总销售额=sum(销售额),
占比= cast(cast( 会员销售额/总销售额*100 as decimal(16,2)) as varchar(10)))+'%' --2位小數
from 表
where 在一段时间内
group by 门店号
子陌红尘 2005-04-08
  • 打赏
  • 举报
回复
select
门店号,
消费会员数 = count(distinct 会员号) - max(case when 会员号 is null then 1 else 0 end),
会员销售额 = sum(case when 会员号 is not null then 销售额 else 0 end),
总销售额 = isnull(sum(销售额),0),
占比 = (sum(case when 会员号 is not null then 销售额 else 0 end)+0.0)/(case when isnull(sum(销售额),0)=0 then 1 else sum(销售额) end)
from

where
... --时间范围条件
group by
门店号

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧