再求个sql语句

sdyqingdao 2008-09-06 04:29:09
table tblProducts有两个字段:
....
MSRP int
M_Price int
......

现在我要找出符合MSRP>66 and MSRP<70的M_Price的每个不同值的个数,比如查询得到:
M_Price
75
75
68
null
74
75
76
null

则应该得到1个68,1个74,3个75,1个76。null值被忽略掉

请教sql语句?
...全文
90 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2008-09-06
  • 打赏
  • 举报
回复
select price,count(*) from tb where price between x and y
group by price
xingyanguang0433 2008-09-06
  • 打赏
  • 举报
回复
他想要的是这样的
"每个不同值的个数" count(distinct M_Price)
fzcheng 2008-09-06
  • 打赏
  • 举报
回复

SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70
GROUP BY M_Price


ChinaITOldMan 2008-09-06
  • 打赏
  • 举报
回复
SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70
GROUP BY M_Price

select M_Price
sum (case when M_Price>66 and M_Price<70) as '次数'
from tblProducts group by M_Price
都不错
ws_hgo 2008-09-06
  • 打赏
  • 举报
回复
select M_Price 
sum (case when M_Price>66 and M_Price<70) as '次数'
from tblProducts group by M_Price

随手写的
思路是这样的
幸运的意外 2008-09-06
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
select
@sql=isnull(@sql+ ',','') + 'case when M_Price=' + cast(M_Price as varchar(10)) + ' then count(M_Price) end as [' + cast(M_Price as varchar(10)) + '] ' +char(13)
from
@tb
set @sql='select ' +@sql + 'from @tb where MSRP>60 and MSRP<70 group by M_Price'
exec (@sql)

internetroot 2008-09-06
  • 打赏
  • 举报
回复
晕,看错题意了
是这样
SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70
GROUP BY M_Price
aaajedll 2008-09-06
  • 打赏
  • 举报
回复

SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70 and M_Price is not null
GROUP BY M_Price
internetroot 2008-09-06
  • 打赏
  • 举报
回复
select distinct m_price from tblProducts where MSRP>66 and MSRP <70
aaajedll 2008-09-06
  • 打赏
  • 举报
回复

SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70 and M_Price is not null
GROUP BY M_Price
internetroot 2008-09-06
  • 打赏
  • 举报
回复
select distinct m_price from tblProducts where MSRP>66 and MSRP <70
aaajedll 2008-09-06
  • 打赏
  • 举报
回复

SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70 and M_Price is not null
GROUP BY M_Price
Garnett_KG 2008-09-06
  • 打赏
  • 举报
回复


SELECT M_Price,COUNT(M_Price) as cnt
FROM tblProducts
WHERE M_Price>66 AND M_Price<70
GROUP BY M_Price


34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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