SQL 2008 R2 查询统计

Jakey_Ch 2014-06-27 05:24:23
有如下一个表:

查询规则:
1、根据station_id和ng_code统计数据
2、test_count字段为该station_id的测试总数
3、ng_count字段为该station_id且为该ng_code的数量
想要的效果如下:
prduct station_id test_count ng_count ng_code
P_001 S_001 6 2 N_001
P_001 S_001 6 1 N_002
P_001 S_002 4 2 N_001
P_001 S_002 4 1 N_002
P_001 S_002 4 1 N_003

TableName: [statistics_table]
万分感谢!
...全文
190 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jakey_Ch 2014-06-30
  • 打赏
  • 举报
回复
引用 4 楼 ap0405140 的回复:
try this
[/code]

USE [XXXX]

SELECT
a.product,
a.station_id,
a.test_count,
b.ng_count,
c.ng_code_count,
c.ng_code,
cast(cast(round(c.ng_code_count/cast(b.ng_count as decimal(6, 1))*100, 2) as numeric(9, 2)) as varchar(10))+'%' as 'percentage'
FROM
(
SELECT
product,
station_id,
count(1) 'test_count'
FROM
[statistics_table]
WHERE
result is not NULL
--and product = 'P_001'
--and station_id = 'S_001'
GROUP BY
product,
station_id
) a
inner join
(
SELECT
product,
station_id,
count(1) 'ng_count'
FROM
[statistics_table]
WHERE
result = 'FALSE'
GROUP BY
product,
station_id
) b
ON
a.product = b.product
and a.station_id = b.station_id
inner join
(
SELECT
product,
station_id,
ng_code,
count(1) 'ng_code_count'
FROM
[statistics_table]
WHERE
result = 'FALSE'
GROUP BY
product,
station_id,
ng_code
) c
ON
a.product = c.product
and a.station_id = c.station_id


当我给查询加上其他条件后,排序就乱掉了
WHERE
result is not NULL
--and product = 'P_001'

未加product限制前

加了限制后:


然后我想再加个累计百分比的字段,该如何写呢:

累计百分比:
如图所示,按照从上往下,累计起百分比,比如S_002的,第一个ng_code占百分50,累计为50,
第二个ng_code占百分25,累计为(50+25 = )75,最后个为(50+25+25=)100。
万分感谢~~


Jakey_Ch 2014-06-30
  • 打赏
  • 举报
回复
引用 3 楼 fredrickhu 的回复:
select
    [product], [station_id],
    count()over(partition by station_id) as  test_count    ,
    count(1) as ng_count ,ng_code
from
    [statistics_table] 
group by
    [product],[station_id],[ng_code]
--TRY
谢谢! count(*) over(partition by station_id) as test_count ,这句有语法错误,count缺少参数
引用 4 楼 ap0405140 的回复:
try this,

select a.product,a.station_id,a.test_count,
       b.ng_count,b.ng_code
 from
(select product,
        station_id,
        count(1) 'test_count'
 from [表名]
 group by product,station_id) a
inner join
(select product,station_id,ng_code,count(1) 'ng_count'
 from [表名]
 where result='FALSE'
 group by product,station_id,ng_code) b 
on a.product=b.product and a.station_id=b.station_id
谢谢! 这个能得到我想要的结果。
唐诗三百首 2014-06-27
  • 打赏
  • 举报
回复
try this,

select a.product,a.station_id,a.test_count,
b.ng_count,b.ng_code
from
(select product,
station_id,
count(1) 'test_count'
from [表名]
group by product,station_id) a
inner join
(select product,station_id,ng_code,count(1) 'ng_count'
from [表名]
where result='FALSE'
group by product,station_id,ng_code) b
on a.product=b.product and a.station_id=b.station_id
--小F-- 2014-06-27
  • 打赏
  • 举报
回复
select
    [product], [station_id],
    count()over(partition by station_id) as  test_count    ,
    count(1) as ng_count ,ng_code
from
    [statistics_table] 
group by
    [product],[station_id],[ng_code]
--TRY
Jakey_Ch 2014-06-27
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:
两个子查询求和,分别group by prduct station_id ng_code和prduct station_id,两个子查询做联结,条件是prduct station_id
能不能写一下额?谢谢了~那个子查询是这样么?怎么联结额?
--SELECT 
--	[product],	
--	[station_id],
--	[ng_code]
--FROM
--	[statistics_table]	
--WHERE
--	[product] = 'P_001'
--	and [station_id] = 'S_001'
--	GROUP BY [product],[station_id],[ng_code]
	
SELECT 
    [product],	
	[station_id]
FROM
	[statistics_table]	
WHERE
	[product] = 'P_001'
	and [station_id] = 'S_001'
	GROUP BY [product],[station_id]
xdashewan 2014-06-27
  • 打赏
  • 举报
回复
两个子查询求和,分别group by prduct station_id ng_code和prduct station_id,两个子查询做联结,条件是prduct station_id
真正没有任何时间限制 真正的访客页面停留时间统计 系统支持所有不同类型及版本的浏览器 具有成熟的稳定性和可靠性 系统支持海量访问数据的统计分析 并拥有丰富的特色优势功能 真正实时的访问统计系统 全球唯一可以统计当前实时在线访客的系统 并且可以与任意访客发起沟通对话 唯一可以统计并发在线访客的系统 告诉您真实的网站负载 鼠标点击热点功能实时查看 支持任意时间段 多种查询条件的统计分析展现 准确告诉您页面热点情况 为您提供广告战略决策支持 准确把握每一个访客的访问行为轨迹 了解客户心理 更好的为其提供个性化服务 未知地区信息按需更新功能(不存在未知地区) 让您准确把握营销区域 提升销售机会 多指标 浏览量 访问量 停留时间 独立访客 二跳率 跳出率 综合评价功能 让您准确把握各网站 各栏目专题的排名评比效果 给你最准确的业绩考核参考 自动统计简报邮件 让您一上班就可以准确把握网站访问情况 做到运筹帷幄 心中有数 废话不多说 有用得上的赶紧下载吧 http: www adjyc com free html 软件程序免费下载安装 无任何使用时间限制 一次安装 永久使用 功能强不强 一试便知 支持的操作系统 Microsoft Windows Server 2003 x86 and x64 Editions Microsoft Windows Server 2008 x86 and x64 Editions Microsoft Windows Server 2008 R2 Editions Microsoft Windows Server 2012 Editions 支持的数据库 Microsoft SQL Server 2008 x86 and x64 Editions Microsoft SQL Server 2008 R2 x86 and x64 Editions Microsoft SQL Server 2012 x86 and x64 Editions 微软即将发布sqlserver2014也是支持的 ">真正没有任何时间限制 真正的访客页面停留时间统计 系统支持所有不同类型及版本的浏览器 具有成熟的稳定性和可靠性 系统支持海量访问数据的统计分析 并拥有丰富的特色优势功能 真正实时的访问统计系统 全球唯 [更多]

27,580

社区成员

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

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