Average of Counts - sql-server

I Have a table called totals and the data looks like:
ACC_ID Data_ID Mon Weeks Total_AR_Count Total_FR_Count Total_OP_Count
23 9 01/2011 4 172 251 194
42 9 01/2011 4 2 16 28
75 9 01/2011 4 33 316 346
75 9 07/2011 5 1 12 20
42 9 09/2011 5 25 758 25
I want the output to be as Average of all the counts grouped by ACC_ID and Data_ID:
ACC_ID Data_ID Avg_AR_Count Avg_FR_Count Avg_OP_Count
23 9 172 251 194
42 9 13.5 387 26.5
75 9 17 164 183
How can do this?

Your description of what you want just about writes the SQL:
SELECT ACC_ID, Data ID, AVG(Total_AR_Count) AS Avg_AR_Count, AVG(Total_FR_Count) AS Avg_FR_Count...
FROM table
GROUP BY ACC_ID, Data_ID

Related

Update previous rows based on another row

I have a question and i need to update the empty rows based on the rows with value
In this case, i need to update the hours, mins and secs based on every 4th row
For ex: rownum 4 has 8hours, 1mins, 9 sec.
So, my update to previous row should be 8hrs, 1min, 6 sec from rownum 1, then, for rownum 5 it should continue the same procedure
See rownum 8 has 8hours, 1mins, 13 sec.
The previous 3 rows should be 8hrs, 1min, 10 sec from rownum 5
How to have this in a loop or with partition by or any suggestion in SQL server.
You can do this with window functions and converting your hours, minutes and seconds to a time value. Converting to a tim is important to make sure you wrap around the appropriate time boundaries and don't end up with 61 seconds in a minute etc.
Depending on the data and your real world environment you will probably need to add the Flight and maybe some other columns into the partition bys to ensure you are working correctly scoped windows of data.
Query
declare #t table(rn int,timeframe int,h int,m int,s int);
insert into #t values
(1,1,null,null,null)
,(2,1,null,null,null)
,(3,1,null,null,null)
,(4,1,23,59,45)
,(5,2,null,null,null)
,(6,2,null,null,null)
,(7,2,null,null,null)
,(8,2,23,59,49)
,(9,3,null,null,null)
,(10,3,null,null,null)
,(11,3,null,null,null)
,(12,3,23,59,53)
,(13,4,null,null,null)
,(14,4,null,null,null)
,(15,4,null,null,null)
,(16,4,23,59,57)
,(17,5,null,null,null)
,(18,5,null,null,null)
,(19,5,null,null,null)
,(20,5,0,0,1)
,(21,6,null,null,null)
,(22,6,null,null,null)
,(23,6,null,null,null)
,(24,6,0,0,5)
;
with d as
(
select rn
,timeframe
,dateadd(second
,rn - max(rn) over (partition by timeframe)
,max(timefromparts(h,m,s,0,0)) over (partition by timeframe)
) as t
from #t
)
select rn
,timeframe
,datepart(hour,t) as h
,datepart(minute,t) as m
,datepart(second,t) as s
from d
order by rn;
Output
rn
timeframe
h
m
s
1
1
23
59
42
2
1
23
59
43
3
1
23
59
44
4
1
23
59
45
5
2
23
59
46
6
2
23
59
47
7
2
23
59
48
8
2
23
59
49
9
3
23
59
50
10
3
23
59
51
11
3
23
59
52
12
3
23
59
53
13
4
23
59
54
14
4
23
59
55
15
4
23
59
56
16
4
23
59
57
17
5
23
59
58
18
5
23
59
59
19
5
0
0
0
20
5
0
0
1
21
6
0
0
2
22
6
0
0
3
23
6
0
0
4
24
6
0
0
5

Adding Character String with Numbers / Concatenate Number with String in Matlab Array

How to add character ',' or '+' in a matlab 2d array.
I've tried the following way.
clc
clear all
close all
min=0;
max=1052;
random_int = randi([min max],5,10)
% random_int=[515,586,942,742;353,588,916,436]
% load('Random_Int_x.mat')
% random_int
[m,n]=size(random_int);
for i=1:1:m
allOneString = sprintf('%d,' , random_int(i,:));
allOneString= allOneString(1:end-1)% strip final comma
Str_1(i,:)=allOneString
% allOneString= strjoin(arrayfun(#(x) num2str(x),random_int(i,:),'UniformOutput',false),',');
end
Str_1
Example of Input / Matrix
random_int =
2 9 7 7 9 8 2 5 7 5
6 1 9 9 6 1 9 4 1 0
5 0 8 8 5 6 9 0 4 6
0 9 9 8 7 5 6 3 7 8
8 4 2 0 5 5 1 8 2 6
Output:
Str_1 =
5×19 char array
'2,9,7,7,9,8,2,5,7,5'
'6,1,9,9,6,1,9,4,1,0'
'5,0,8,8,5,6,9,0,4,6'
'0,9,9,8,7,5,6,3,7,8'
'8,4,2,0,5,5,1,8,2,6'
This works properly with random number between 0-9.. However if I put input above 9 --> 10 .. then matlab throws matrix dimension error.
Subscripted assignment dimension mismatch.
Error in Number_with_String (line 14)
Str_1(i,:)=allOneString;
For Input above 9:
random_int =
76 96 88 23 26 25 92 5 61 86
87 69 32 36 86 39 46 21 55 69
42 26 56 69 55 97 91 78 76 41
74 74 24 3 46 52 29 70 88 4
7 48 13 69 15 12 79 91 90 24
Expecting output:
'76,96,88,23,26,25,92,5,61,86'
'87,69,32,36,86,39,46,21,55,69' ... etc
Any suggestion to resolve this ..
Here's a way:
random_int = randi([0 500],5,10); % example data
y = mat2cell(random_int, ones(1,size(random_int,1)), size(random_int,2)); % split into rows
y = cellfun(#(x) sprintf('%i,', x), y, 'UniformOutput', false); % strings with commas
y = cellfun(#(s) s(1:end-1), y, 'UniformOutput', false); % remove last comma from each
Example result:
>> y
y =
5×1 cell array
'74,281,294,376,124,203,211,170,242,334'
'488,268,31,84,404,74,205,178,215,20'
'120,242,390,37,113,199,140,375,395,469'
'455,94,115,476,28,20,365,213,181,31'
'130,62,138,421,261,105,114,226,398,90'
I would recommend you use string which shipped in 16b. You can convert the result to char or cellstr if you need.
>> min=0; max=1052;
>> random_int = randi([min max],5,10)
random_int =
532 145 857 264 616 793 558 494 327 688
736 157 256 648 578 400 820 12 556 725
938 271 978 498 965 597 983 354 174 787
1010 885 368 370 300 79 136 170 633 474
576 267 207 874 797 56 598 836 276 88
>> str = join(string(random_int),',')
str =
5×1 string array
"532,145,857,264,616,793,558,494,327,688"
"736,157,256,648,578,400,820,12,556,725"
"938,271,978,498,965,597,983,354,174,787"
"1010,885,368,370,300,79,136,170,633,474"
"576,267,207,874,797,56,598,836,276,88"
>> char(str)
ans =
5×39 char array
'532,145,857,264,616,793,558,494,327,688'
'736,157,256,648,578,400,820,12,556,725 '
'938,271,978,498,965,597,983,354,174,787'
'1010,885,368,370,300,79,136,170,633,474'
'576,267,207,874,797,56,598,836,276,88 '
clc
clear all
close all
min=0;
max=1052;
random_int = randi([min max],200,10);
[m,n]=size(random_int);
for i=1:1:m
allOneString = sprintf('%d,' , random_int(i,:));
allOneString= allOneString(1:end-1); % strip final comma
Str_1{i}=allOneString;
end
Str_1=Str_1'

Sum of multiple variables by group

I have a dataset with over 900 observations, each observation represents the population of a sub-geographical area for a given year by gender (male, female, all) and 20 different age groups.
I have dropped the variable for the sub-geographical area and I want to collape into the greater geographical area (called Geo).
I am having a difficult time doing a SUM or PROC MEANS because I have so many age groups to sum up and I am trying to avoid writing them all out. I want to collapse across the group year, geo, sex so that I only have 3 observations per Geo (my raw data could have as many as 54 observations).
This is an example of what a tiny section of the raw data looks like:
Year Geo Sex Age0005 Age0610 Age1115 (etc)
2010 1 1 92 73 75
2010 1 2 57 81 69
2010 1 3 159 154 144
2010 1 1 41 38 43
2010 1 2 52 41 39
2010 1 3 93 79 82
2010 2 1 71 66 68
2010 2 2 63 64 70
2010 2 3 134 130 138
2010 2 1 32 35 34
2010 2 2 29 31 36
2010 2 3 61 66 70
This is how I want it to look:
Year Group Sex Age0005 Age0610 Age1115 (etc)
2010 1 1 133 111 118
2010 1 2 109 122 08
2010 1 3 252 233 226
2010 2 1 103 101 102
2010 2 2 92 95 106
2010 2 3 195 196 208
Any ideas? Please help!
You don't have to write out each variable name individually - there are ways of getting around that. E.g. if all of the age group variables that need to be summed up start with age then you can use a : wildcard to match them:
proc summary nway data = have;
var age:;
class year geo sex;
output out = want sum=;
run;
If your variables don't have a common prefix, but are all next to each other in one big horizontal group in your dataset, you can use a double dash list instead:
proc summary nway data = have;
var age005--age1115; /*Includes all variables between these two*/
class year geo sex;
output out = want sum=;
run;
Note also the use of sum= - this means that each summarised variable is reproduced with its original name in the output dataset.
I personally like to use proc sql for this, since it makes it very clear what you're summing and grouping by.
data old ;
input Year Geo Sex Age0005 Age0610 Age1115 ;
datalines;
2010 1 1 92 73 75
2010 1 2 57 81 69
2010 1 3 159 154 144
2010 1 1 41 38 43
2010 1 2 52 41 39
2010 1 3 93 79 82
2010 2 1 71 66 68
2010 2 2 63 64 70
2010 2 3 134 130 138
2010 2 1 32 35 34
2010 2 2 29 31 36
2010 2 3 61 66 70
;
run;
proc sql ;
create table new as select
year
, geo label = 'Group'
, sex
, sum(age0005) as age0005
, sum(age0610) as age0610
, sum(age1115) as age1115
from old
group by geo, year, sex ;
quit;

reading and printing a .csv file like a 2D matrix with both integer and float values in c

Reading a file in c with .csv as extension. The file consisting of both integer and float type data values. Is there any way to read the csv file. Any help is appreciated.
The data is as follows:
Application_No. Actual_Effort (in PM) No of Processes No of Tasks No of partnerLinks Task Variables Element Variables Event Variables Script Developer's Skills Developer's Confidence TPSS TS TCC
1 918.28 1 3 5 33 7 2 3 3.5 1 8 135 143
2 8891.513 3 9 3 100 15 6 12 3 1 36 1197 1233
3 22479.261 5 15 23 125 25 10 20 3 1 190 2700 2890
4 2961.131 2 4 9 70 13 4 17 2 0 72 416 488
5 19650.198 7 14 19 130 28 12 5 2.5 0 231 2450 2681
6 377.75 1 2 4 22 8 2 2 3 1 6 68 74
7 2671.93 1 5 12 55 12 6 4 2 0 17 385 402
8 966.15 3 3 6 31 8 5 7 2.5 0 27 153 180
9 3765.81 2 6 17 73 14 2 3 3.5 1 46 552 590
10 7467.11 4 8 21 87 19 13 1 2 0 116 960 1076

SQL Query Help after 13th row Dynamic and in 12th row static values with a formula

I have below table structure with the similar to below output.
S_NO_ T_P O_P H_P L_P C_P SC_12
1 1 509.75 515 508 512.4500122
2 2 511.7000122 511.7000122 506.1499939 506.5499878
3 4 507.1499939 510.25 507.1499939 510.25
4 5 510 512.3499756 509.2999878 512.3499756
5 3 512.5 512.5 511.1499939 512
6 8 512.25 512.5 510.1000061 510.9500122
7 1 510.5499878 511.7999878 510 511.7999878
8 2 511.1000061 511.8500061 508.1499939 508.8999939
9 5 508.8999939 510 508.5 509.9500122
10 6 509.8999939 509.8999939 508.5 508.8500061
11 8 509.5 511.2000122 509 510.5
12 9 510.5 511.7999878 510.1000061 510.2000122 **510.4**
13 12 510.2999878 511.3500061 510.25 510.75 510.25
14 14 510.3500061 512 510.3500061 510.9500122 510.62
15 15 510.9500122 511.7999878 510.6000061 511.1000061 510.69
16 17 511.0499878 511.3500061 509.1000061 509.1000061 510.42
17 13 509.5 509.5 508.1000061 508.5 510.13
18 13 508.4500122 508.9500122 507 507 509.8
19 19 507 508.2000122 503.2999878 503.2999878 509.09
20 11 504 505 503.5 504.6499939 508.74
21 17 505.4500122 506.3500061 504 504.7000122 508.3
22 18 504.7000122 505.5 504.2000122 505.5 508.02
23 19 505.3500061 505.7000122 503.1000061 503.6499939 507.45
I want write a SQL Query to get results in C12WR column.
And I want on row of C12WR Column use a static value which is in “SC_12” (In the shown table the value is “510.4”) and in the SC_12 Column it should calculate the below formula after 13th row number and it continue it till the end of the table records
After 13th Row in C12WR Column = (the value of above row*11 + Current row value from C_P Column) /12
The final output should be similar to below
S_NO_ T_P O_P H_P L_P C_P SC_12 c12-WWR
1 1 509.75 515 508 512.4500122
2 2 511.7000122 511.7000122 506.1499939 506.5499878
3 4 507.1499939 510.25 507.1499939 510.25
4 5 510 512.3499756 509.2999878 512.3499756
5 3 512.5 512.5 511.1499939 512
6 8 512.25 512.5 510.1000061 510.9500122
7 1 510.5499878 511.7999878 510 511.7999878
8 2 511.1000061 511.8500061 508.1499939 508.8999939
9 5 508.8999939 510 508.5 509.9500122
10 6 509.8999939 509.8999939 508.5 508.8500061
11 8 509.5 511.2000122 509 510.5
12 9 510.5 511.7999878 510.1000061 510.2000122 510.4 510.4
13 12 510.2999878 511.3500061 510.25 510.75 510.25 510.3833344
14 14 510.3500061 512 510.3500061 510.9500122 510.62 510.4138898
15 15 510.9500122 511.7999878 510.6000061 511.1000061 510.69 510.4585667
16 17 511.0499878 511.3500061 509.1000061 509.1000061 510.42 510.51202
17 13 509.5 509.5 508.1000061 508.5 510.13 510.3943521
18 18 508.4500122 508.9500122 507 507 509.8 510.2364895
19 19 507 508.2000122 503.2999878 503.2999878 509.09 509.966782
20 11 504 505 503.5 504.6499939 508.74 509.4112158
21 17 505.4500122 506.3500061 504 504.7000122 508.3 509.0144473
22 18 504.7000122 505.5 504.2000122 505.5 508.02 508.6549111
23 19 505.3500061 505.7000122 503.1000061 503.6499939 507.45 508.3920018
I don't know what you mean by a static value which is in “SC_12” But if you have that value in the table then use this:
select T_P, O_P, H_P, L_P, C_P, SC_12
case when t1.S_NO_ > 12
then cast ((t2.SC_12 * 11 + t1.C_P)/12 as varchar(50))
else ''
end as [c12-WWR]
from t as t1
inner join t as t2 on t2.S_NO_ = t1.S_NO_ -1
order by t1.S_NO_

Resources