SQL统计数量问题

hu0336 2012-01-18 03:55:02
表中有P_Money(金额),P_InTime(时间),Shop_ID(门店编号),P_PayType(付款类型)四列

下面代码是要查询某个门店某个类型一年每个月的销售额是多少,没有月记录的也要求能查询出来,不过为0

select m.month , isnull(n.P_Money,0) P_Money from
(
select 1 month union
select 2 month union
select 3 month union
select 4 month union
select 5 month union
select 6 month union
select 7 month union
select 8 month union
select 9 month union
select 10 month union
select 11 month union
select 12 month
) m
left join
(select datepart(mm,P_InTime) P_InTime , sum(P_Money) P_Money from Pay_View where datediff(yy,P_InTime,getdate()) = 0 group by datepart(mm,P_InTime)) n
on m.month = n.P_InTime



查询结果:

1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 30
11 30
12 40


如何将上面的代码改为查询一年12个月每个月的数量,不统计总金额了,就统计付款次数,怎么改为统计?
...全文
147 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 2012-01-18
  • 打赏
  • 举报
回复
count(*) 看看了。
hu0336 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 josy 的回复:]
确认以下查询是否有数据?

SQL code

select * from DingDan where TaoXi_ID=100003 and datediff(yy,D_Time1,getdate()) = 0
[/Quote]

select * from DingDan where TaoXi_ID=100003 and datediff(yy,D_Time1,getdate()) > 0
这个是有数据的,你上面的因为我没有本月的,所以查不到,可以查到以前的
百年树人 2012-01-18
  • 打赏
  • 举报
回复
确认以下查询是否有数据?
select * from DingDan where TaoXi_ID=100003 and datediff(yy,D_Time1,getdate()) = 0
hu0336 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acherat 的回复:]
楼主想问什么,那查询有问题么?结果没的显示是0呀~!最后三个月有销售额。
[/Quote]

我要查询的是数量,每个月的付款次数统计,不是金额统计
AcHerat 2012-01-18
  • 打赏
  • 举报
回复
楼主想问什么,那查询有问题么?结果没的显示是0呀~!最后三个月有销售额。
hu0336 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 roy_88 的回复:]
--sum(P_Money) 改為 count(P_Money)

統計次數
[/Quote]
按你说的不行啊,执行结果每个月都是0

create proc DingDan_GetTaoXiCount
@ShopID int,TaoXiID int
as
begin
select m.month , isnull(n.TaoXi_ID,0) TaoXi_ID from
(
select 1 month union
select 2 month union
select 3 month union
select 4 month union
select 5 month union
select 6 month union
select 7 month union
select 8 month union
select 9 month union
select 10 month union
select 11 month union
select 12 month
) m
left join
(select datepart(mm,D_Time1) D_Time1 , count(TaoXi_ID) TaoXi_ID from DingDan where TaoXi_ID=100003 and datediff(yy,D_Time1,getdate()) = 0 group by datepart(mm,D_Time1)) n
on m.month = n.D_Time1
end
go
中国风 2012-01-18
  • 打赏
  • 举报
回复
--sum(P_Money) 改為 count(P_Money)

統計次數

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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