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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ordered_use
2 UNIT CONVERTED DWELLING
28706 51
2 UNIT DWELLING 2 UNITS
99 44
3 UNIT DWELLING APARTMENT
31 4733
APARTMENT APARTMENT BLDG
38 37
APARTMENT BUILDING APARTMENT UNIT
2042 37
APPARTMENT BUILDING APT
54 357
APT BLDG APT BUILDING
78 49
APT. APT. BLDG
41 61
APT. BUILDING ARENA
35 67
BANK BOWLING ALLEY
302 267
BUNGALOW CAR DEALERSHIP
85 62
CHURCH CLUB
94 40
COLLEGE COMMERCIAL
196 410
COMMERCIAL/RESIDENTIAL COMMUNITY CENTRE
56 131
COMMUNITY HALL CONDO
31 223
CONDOMINIUM CONVERTED DWELLING
42 42
CONVERTED HOUSE CONVERTED HOUSE - 2 UNITS
149 124
CONVERTED HOUSE - 3 UNITS CONVERTED HOUSE (2 UNITS)
56 35
CONVERTED HOUSE 2 UNITS CONVERTED HOUSE, 2 UNITS
38 84
CONVERTED HOUSE, 3 UNITS DAYCARE
42 31
DENTAL OFFICE DETACHED - SFD
87 513
DETACHED - SINGLE FAMILY DWELLING DETACHED HOUSE
97 130
DETACHED SFD DUPLEX
190 145
ELEMENTARY SCHOOL FIRE HALL
859 41
FITNESS CENTRE FUNERAL HOME
48 36
GARAGE GAS STATION
63 130
GROCERY STORE GROUP HOME
51 45
HAIR SALON HOME FOR THE AGED
46 49
HOSPITAL HOTEL
971 215
HOUSE IND
1249 219
INDUSTRIAL INDUSTRIAL
1725 35
INDUSTRIAL BUILDING INDUSTRIAL MANUFACTURING
51 91
INDUSTRIAL WAREHOUSE INSTITUTIONAL
61 48
LAB LABORATORY
56 46
LIBRARY LONG TERM CARE FACILITY
91 74
LUMBER YARD MANUFACTURING
53 55
MEDICAL OFFICE MIXED USE
247 539
MIXED USE MIXED USE (COMMERCIAL)
40 34
MIXED USE (RETAIL) MIXED USE BUILDING
74 297
MIXED USE BUILDING/NON RESIDENTIAL MIXED USE NON RES
37 93
MIXED USE NON RES (RETAIL) MIXED USE RES & NON RES
52 59
MIXED-USE MULTI UNIT
202 54
MULTI UNIT BUILDING MULTI USE
70 381
MULTI USE, NON RES MULTI USE/NON RES
134 36
MULTI USE/NON RESIDENTIAL MULTIPLE UNIT
49 149
MULTIPLE UNIT BUILDING MUSEUM
40 40
N/A NONE
650 264
NOT KNOWN NURSING HOME
58 55
OFF OFFICE
181 9698
OFFICE OFFICE BLD
50 46
OFFICE BUILDING OFFICE SPACE
177 39
OFFICE/RETAIL OFFICE/WAREHOUSE
36 95
OFFICES OTHER
63 54
PARK PARKING GARAGE
137 149
PARKING LOT PERSONAL SERVICE SHOP
126 49
PLACE OF WORSHIP POLICE STATION
516 34
PROF. OFFICE RECREATIONAL
65 46
REPAIR GARAGE RES
91 242
RESIDENTIAL RESIDENTIAL - SFD
488 561
RESIDENTIAL CONDO REST
39 46
RESTAURANT RESTAURANT > 30 SEATS
1074 42
RESTAURANT GREATER THAN 30 SEATS RESTAURANT LESS THAN 30 SEATS
145 69
RESTAURANT UNDER 30 SEATS RESTAURANT, GREATER THAN 30 SEATS
47 42
RET RETAIL
81 4001
RETAIL RETAIL MALL
46 61
RETAIL PLAZA RETAIL STORE
96 796
RETAIL/OFFICE RETAIL/RESIDENTIAL
32 99
ROOMING HOUSE ROW HOUSE
89 38
SCHOOL SECONDARY SCHOOL
594 246
SEMI SEMI DETACHED
209 218
SEMI DETACHED - SFD SEMI DETACHED - SINGLE FAMILY DWELLING
212 50
SEMI DETACHED SFD SEMI-DETACHED
46 71
SEMI-DETACHED - SFD SEMI-DETACHED DWELLING
241 172
SEMI-DETACHED HOUSE SEMI-DETACHED SFD
56 155
SEMI-DETACHED SINGLE FAMILY DWELLING SFD
90 26479
SFD - DETACHED SFD - DETCAHED
3817 79
SFD - ROWHOUSE SFD - SEMI
76 206
SFD - SEMI DETACHED SFD - SEMI-DETACHED
353 209
SFD - SEMIDETACHED SFD - TOWNHOUSE
158 131
SFD DET SFD DETACEHD
495 39
SFD DETACHED SFD DETATCHED
755 231
SFD ROWHOUSE SFD SEMI
31 857
SFD SEMI DETACHED SFD SEMI-DETACHED
59 167
SFD TOWNHOUSE SFD-DETACHED
155 8148
SFD-DETACHED SFD-ROWHOUSE
37 56
SFD-SEMI SFD-SEMI DETACHED
1189 613
SFD-SEMI-DETACHED SFD-TOWNHOUSE
313 526
SINGLE SINGLE FAMILY
41 64
SINGLE FAMILY DETACHED SINGLE FAMILY DETACHED DWELLING
1615 222
SINGLE FAMILY DETACHED HOUSE SINGLE FAMILY DWELLING
58 2673
SINGLE FAMILY SEMI-DETACHED SINGLE-FAMILY DETACHED HOUSE
54 107
SINGLE-FAMILY SEMI-DETACHED HOUSE STADIUM
53 37
STUDENT RESIDENCE SUBWAY STATION
34 44
SURFACE PARKING LOT/EXISTING COMMERCIAL BUILDING TAKE OUT RESTAURANT
57 34
THEATRE TOWNHOUSE
38 198
TOWNHOUSE - SFD TOWNHOUSES
97 31
TRANSIT STATION TRIPLEX
70 54
UNION STATION UNIVERSITY
52 359
UNIVERSITY OF TORONTO VACANT
42 15010
VACANT VACANT (AFTER DEMO)
77 36
VACANT COMMERCIAL VACANT COMMERCIAL UNIT
37 63
VACANT INDUSTRIAL VACANT LAND
32 1107
VACANT LOT VACANT RETAIL
447 112
VACANT RETAIL UNIT VACANT SINGLE FAMILY DWELLING
46 82
VACANT SPACE VACANT UNIT
120 117
VACNT WAREHOUSE
42 526
WAREHOUSE/OFFICE WATER TREATMENT PLANT
54 46
Apartment <- (ordered_use[6]+ ordered_use[7]+ ordered_use[8] + ordered_use[9] + ordered_use[10] + ordered_use[11] + ordered_use[12] + ordered_use[13] + ordered_use[14] + ordered_use[15] + ordered_use[16] + ordered_use[17] + ordered_use[30] + ordered_use[31] + ordered_use[33] + ordered_use[34] + ordered_use[35] + ordered_use[36] + ordered_use[37] + ordered_use[38] + ordered_use[39] + ordered_use[84] + ordered_use[85] + ordered_use[90] + ordered_use[91])
I am trying to convert anything that looks like an apartment,building,condo, unit and etc therefore I combined everything which looks similar but my question is, how can I replace those with my combined data of Apartment
To get something to work with I pasted your text into the space between the quotes of:
ordered_use <- read.fwf(textConnection("___"), widths=c(50,50), stringsAsFactors=FALSE)
And then trimmed blank-space and extracted every other row of the odd items and applied as.numeric to the even rows>
ordered_use[] <- lapply(ordered_use, trim)
ord2 <- data.frame(
nams <- c( ordered_use[ c(TRUE,FALSE), "V1"], ordered_use[ c(TRUE,FALSE), "V2"]),
nums=as.numeric(c( ordered_use[ c(FALSE,TRUE), "V1"], ordered_use[ c(FALSE,TRUE), "V2"]) )
> head(ord2)
nams nums
1 28706
2 2 UNIT DWELLING 99
3 3 UNIT DWELLING 31
4 APARTMENT 38
5 APARTMENT BUILDING 2042
6 APPARTMENT BUILDING 54
To extract items with "APT" or "CONDO" use grepl
> ord2[ grepl("APART|APPART|APT|CONDO", ord2$nams) , ]
nams nums
4 APARTMENT 38
5 APARTMENT BUILDING 2042
6 APPARTMENT BUILDING 54
7 APT BLDG 78
8 APT. 41
9 APT. BUILDING 35
16 CONDOMINIUM 42
60 RESIDENTIAL CONDO 39
110 APARTMENT 4733
111 APARTMENT BLDG 37
112 APARTMENT UNIT 37
113 APT 357
114 APT BUILDING 49
115 APT. BLDG 61
122 CONDO 223
I cannot tell whether your item numbers match up since you probably have a table object and I have two columns that are not arranges the same as yours.
> sum( ord2[ grepl("APART|APPART|APT|CONDO", ord2$nams) ,"nums" ])
[1] 7866
You should post the output of dput(head(ordered_use, 20)) if you want an answer tailored to the type of object you have.
I have to show my table data in sort order by design_no
Here is my data
design_no fname meter rate s m l xl
---------------------------------------------------------------
3092 2111-1 432.00 235.00 32 33 21 21
3092 2111-1 498.75 235.00 38 37 24 24
3092 2111-1 460.50 235.00 31 35 23 24
3092 2111 501.75 245.00 37 38 25 24
I want show it like this..
design_no fname meter rate pcs
---------------------------------------------------
3092 2111 501.75 245.00 124
3092 2111-1 1391.25 235.00 343
Kindy help me
SELECT design_no,fname,SUM(meter),rate,SUM(s)+SUM(m)+SUM(l)+SUM(xl)
FROM tab
GROUP BY design_no,fname,rate
What behaviour do you want if the rate is different for the same design_no and fname?
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