从表创建统计数据
Creating statistical data from a table
我有一个包含 20 列测量值的表格。我想将表"转换"为包含 20 行的表,其中包含平均、最小、最大、标准开发、计数类型的信息列。 还有另一个类似的问题,但它是针对"R"语言的。其他问题在这里。
我可以对每列执行以下操作(使用 C++ 处理结果):
Select Count(Case When [avgZ_l1] <= 0.15 and avgZ_l1 > 0 then 1 end) as countValue1,
Count(case when [avgZ_l1] <= 0.16 and avgZ_l1 > 0.15 then 1 end) as countValue2,
Count(case when [avgZ_l1] <= 0.18 and avgZ_l1 > 0.16 then 1 end) as countValue3,
Count(case when [avgZ_l1] <= 0.28 and avgZ_l1 > 0.18 then 1 end) as countValue4,
Avg(avgwall_l1) as avg1, Min(avgwall_l1) as min1, Max(avgZ_l1) as max1,
STDEV(avgZ_l1) as stddev1, count(*) as totalCount from myProject.dbo.table1
但我不想处理 50,000 条记录 20 次(每列一次)。我认为可以将表"透视"到一侧并同时处理数据。 我见过"透视"的例子,但它们似乎都基于整数类型字段、月份编号或设备 ID。转换表后,我可以用C++获取每一行。也许这真的只是"插入...选择。。。从'语句。
最快的(执行时间)方法是简单地创建一个很长的选择语句,该语句返回我想要的所有列的所有信息吗?我们最终可能会有 500,000 行。 我正在使用C++和SQL 2014。
欢迎任何想法或意见。我只是不想让我的天真代码被用作如何不做某事的光辉榜样...... ;)...
如果您的表看起来与您在 r 中发送的代码相同,那么以下查询应该适合您。它会选择您请求的数据并同时对其进行透视。
create table #temp(ID int identity(1,1),columnName nvarchar(50));
insert into #temp
SELECT COLUMN_NAME as columnName
FROM myProject.INFORMATION_SCHEMA.COLUMNS -- change myProject to the name of your database. Unless myProject is your database
WHERE TABLE_NAME = N'table1'; --change table1 to your table that your looking at. Unless table1 is your table
declare @TableName nvarchar(50) = 'table1'; --change table1 to your table again
declare @loop int = 1;
declare @query nvarchar(max) = '';
declare @columnName nvarchar(50);
declare @endQuery nvarchar(max)='';
while (@loop <= (select count(*) from #temp))
begin
set @columnName = (select columnName from #temp where ID = @loop);
set @query = 'select t.columnName, avg(['+@columnName+']) as Avg ,min(['+@columnName+']) as min ,max(['+@columnName+'])as max ,stdev(['+@columnName+']) as STDEV,count(*) as totalCount from '+@tablename+' join #temp t on t.columnName = '''+@columnName+''' group by t.columnName';
set @loop += 1;
set @endQuery += 'union all('+ @query + ')';
end;
set @endQuery = stuff(@endQuery,1,9,'')
Execute(@endQuery);
drop table #temp;
它会创建一个 #temp 表,该表将列标题的值存储在 ID 旁边。然后,它在循环遍历您拥有的列数时使用 ID。然后,它会生成一个查询,该查询选择您想要的内容,然后将其合并在一起。此查询将适用于任意数量的列,这意味着如果您添加或删除更多列,它应该给出正确的结果。
使用此输入:
age height_seca1 height_chad1 height_DL weight_alog1
1 19 1800 1797 180 70
2 19 1682 1670 167 69
3 21 1765 1765 178 80
4 21 1829 1833 181 74
5 21 1706 1705 170 103
6 18 1607 1606 160 76
7 19 1578 1576 156 50
8 19 1577 1575 156 61
9 21 1666 1665 166 52
10 17 1710 1716 172 65
11 28 1616 1619 161 66
12 22 1648 1644 165 58
13 19 1569 1570 155 55
14 19 1779 1777 177 55
15 18 1773 1772 179 70
16 18 1816 1809 181 81
17 19 1766 1765 178 77
18 19 1745 1741 174 76
19 18 1716 1714 170 71
20 21 1785 1783 179 64
21 19 1850 1854 185 71
22 31 1875 1880 188 95
23 26 1877 1877 186 106
24 19 1836 1837 185 100
25 18 1825 1823 182 85
26 19 1755 1754 174 79
27 26 1658 1658 165 69
28 20 1816 1818 183 84
29 18 1755 1755 175 67
它将产生以下输出:
avg min max stdev totalcount
age 20 17 31 3.3 29
height_seca1 1737 1569 1877 91.9 29
height_chad1 1736 1570 1880 92.7 29
height_DL 173 155 188 9.7 29
weight_alog1 73 50 106 14.5 29
希望这对您有所帮助并对您有用。 :)
相关文章:
- 从 bmp 文件数据创建 MFC CBitmap
- 如何在 QML 中使用从 c++ 公开的序列数据创建图表
- c++用输入数据创建数组
- 避免在统计数据和重命名之间进行TOCTOU(检查时间,使用时间)
- 是否有一些有意义的统计数据来证明保持有符号整数算术溢出未定义是合理的
- Magick++ ImageMagick 7.0.5-4 Q16 的统计数据x86_64问题
- 使用 2D 数据创建 3D 直方图(OpenCV?
- 如何使C++代码更快:使用 CFile 从相机数据创建.csv文件
- 在调试中,如何知道对函数的重复调用中参数的统计数据(max-min,average,distribution..)
- 使用 C++ 从 16 位数据创建灰度图像
- 如何根据过滤的数据创建笛卡尔产品范围
- C 双重调度,出厂模式或一种从接收到的序列数据创建派生对象的方法
- 为 ppm/pgm 图像数据创建存储
- 如何从二进制数据创建 QIcon
- 基于输入数据创建 STL 向量 - 标量或复杂类型
- DirectX11如何从未签名的char*数据创建Texture2D
- 使用相机的初始数据创建d3d11Texture2D
- 我可以收集mmap读/写统计数据吗
- OpenCV C++:从存储在 int 向量的索引中为垫子类型数据创建列范围
- 从表创建统计数据