Thread: pgsql-function
AAA_key | AP_KEY | FIRSTOCCURRENCE | SEVERITY |
111 | 418 | 3/4/2014 0:00 | 5 |
111 | 418 | 3/4/2014 0:05 | 0 |
112 | 12 | 3/4/2014 0:40 | 4 |
112 | 12 | 3/4/2014 0:45 | 0 |
113 | 13 | 3/4/2014 1:05 | 3 |
113 | 13 | 3/4/2014 1:10 | 0 |
114 | 114 | 3/4/2014 1:30 | 2 |
114 | 114 | 3/4/2014 1:35 | 0 |
115 | 35 | 3/4/2014 2:10 | 1 |
115 | 35 | 3/4/2014 2:15 | 0 |
116 | 116 | 3/4/2014 10:14 | 4 |
116 | 116 | 3/4/2014 10:19 | 0 |
117 | 127 | 3/4/2014 11:45 | 3 |
117 | 127 | 3/4/2014 11:49 | 0 |
118 | 118 | 3/4/2014 12:10 | 2 |
118 | 118 | 3/4/2014 12:14 | 0 |
119 | 19 | 3/4/2014 12:35 | 1 |
119 | 19 | 3/4/2014 12:39 | 0 |
119 | 120 | 3/4/2014 0:00 | 4 |
119 | 120 | 3/4/2014 1:40 | 0 |
On Tue, Apr 14, 2015 at 03:39:38AM -0700, Rishi Ranjan wrote: > i have data like below and where ap_key is same for two different > first_occurence column . here i need to write a function which can > calculate the difference of two timestamp values for a Ap_Key and then with > difference it should multiply the severity 1. Why does it have to be a function? Can't it simply be a query? 2. Which pg version do you use? 3. Which severity to use? from the earlier FIRSTOCCURRENCE, or later, or simply higher/lower SEVERITY? Basically what you're looking for is most likely some group by with min/max. depesz -- The best thing about modern society is how easy it is to avoid contact with it. http://depesz.com/
y= ((y2-y1)/(x2-x1 ))x+ (y1- (y2-y1)/(x2-x1 ) x1 )
where x1,y1,x2,y2 parameters are defined and whose value need to be picked based on value of x (being calculated from above scenario provided with sample data)
so help me to decide in what way we can proceed
3) from above data in firstoccurence column there is up time and down time of some device and we need to choose higher severity value .
On Tue, Apr 14, 2015 at 03:39:38AM -0700, Rishi Ranjan wrote:
> i have data like below and where ap_key is same for two different
> first_occurence column . here i need to write a function which can
> calculate the difference of two timestamp values for a Ap_Key and then with
> difference it should multiply the severity
1. Why does it have to be a function? Can't it simply be a query?
2. Which pg version do you use?
3. Which severity to use? from the earlier FIRSTOCCURRENCE, or later, or
simply higher/lower SEVERITY?
Basically what you're looking for is most likely some group by with
min/max.
depesz
--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/
we are using version 9.4.2) The result of above calculation i need to use in different calculation . means output of this calculation will let us to determine the value of some parameters of other equation like belowy= ((y2-y1)/(x2-x1 ))x+ (y1- (y2-y1)/(x2-x1 ) x1 )
where x1,y1,x2,y2 parameters are defined and whose value need to be picked based on value of x (being calculated from above scenario provided with sample data)
That will be in this format only. We thinked for normalization part but we got clearance that data will be in this format and have to do processing
we are using version 9.4.2) The result of above calculation i need to use in different calculation . means output of this calculation will let us to determine the value of some parameters of other equation like belowy= ((y2-y1)/(x2-x1 ))x+ (y1- (y2-y1)/(x2-x1 ) x1 )
where x1,y1,x2,y2 parameters are defined and whose value need to be picked based on value of x (being calculated from above scenario provided with sample data)So, is the real data likely to be as perfect as your sample data or do you need something more robust? With the clean data using the lag window function will readily solve your problem. But if you could guarantee this clean of data I would probably have simply created two columns (and yes, I do know what normalization is) for timestamps - one of which can be null: the "second" one) and perform the query directly.David J.
That will be in this format only. We thinked for normalization part but we got clearance that data will be in this format and have to do processing
On Apr 14, 2015 10:58 PM, "David G. Johnston" <david.g.johnston@gmail.com> wrote:we are using version 9.4.2) The result of above calculation i need to use in different calculation . means output of this calculation will let us to determine the value of some parameters of other equation like belowy= ((y2-y1)/(x2-x1 ))x+ (y1- (y2-y1)/(x2-x1 ) x1 )
where x1,y1,x2,y2 parameters are defined and whose value need to be picked based on value of x (being calculated from above scenario provided with sample data)So, is the real data likely to be as perfect as your sample data or do you need something more robust? With the clean data using the lag window function will readily solve your problem. But if you could guarantee this clean of data I would probably have simply created two columns (and yes, I do know what normalization is) for timestamps - one of which can be null: the "second" one) and perform the query directly.David J.
On Tue, Apr 14, 2015 at 03:39:38AM -0700, Rishi Ranjan wrote: > i have data like below and where ap_key is same for two different > first_occurence column . here i need to write a function which can > calculate the difference of two timestamp values for a Ap_Key and then with > difference it should multiply the severity > > AAA_key AP_KEY FIRSTOCCURRENCE SEVERITY > 111 418 3/4/2014 0:00 5 > 111 418 3/4/2014 0:05 0 > 112 12 3/4/2014 0:40 4 > 112 12 3/4/2014 0:45 0 > 113 13 3/4/2014 1:05 3 > 113 13 3/4/2014 1:10 0 > 114 114 3/4/2014 1:30 2 > 114 114 3/4/2014 1:35 0 > 115 35 3/4/2014 2:10 1 > 115 35 3/4/2014 2:15 0 > 116 116 3/4/2014 10:14 4 > 116 116 3/4/2014 10:19 0 > 117 127 3/4/2014 11:45 3 > 117 127 3/4/2014 11:49 0 > 118 118 3/4/2014 12:10 2 > 118 118 3/4/2014 12:14 0 > 119 19 3/4/2014 12:35 1 > 119 19 3/4/2014 12:39 0 > 119 120 3/4/2014 0:00 4 > 119 120 3/4/2014 1:40 0 Given this data, why don't you simply: select AAA_key, AP_KEY, max(SEVERITY), max(FIRSTOCCURRENCE) - min(FIRSTOCCURRENCE) from table group by AAA_key, AP_KEY; and then do whatever math you need on severity or FIRSTOCCURRENCE differences. depesz
On Tue, Apr 14, 2015 at 03:39:38AM -0700, Rishi Ranjan wrote:
> i have data like below and where ap_key is same for two different
> first_occurence column . here i need to write a function which can
> calculate the difference of two timestamp values for a Ap_Key and then with
> difference it should multiply the severity
>
> AAA_key AP_KEY FIRSTOCCURRENCE SEVERITY
> 111 418 3/4/2014 0:00 5
> 111 418 3/4/2014 0:05 0
> 112 12 3/4/2014 0:40 4
> 112 12 3/4/2014 0:45 0
> 113 13 3/4/2014 1:05 3
> 113 13 3/4/2014 1:10 0
> 114 114 3/4/2014 1:30 2
> 114 114 3/4/2014 1:35 0
> 115 35 3/4/2014 2:10 1
> 115 35 3/4/2014 2:15 0
> 116 116 3/4/2014 10:14 4
> 116 116 3/4/2014 10:19 0
> 117 127 3/4/2014 11:45 3
> 117 127 3/4/2014 11:49 0
> 118 118 3/4/2014 12:10 2
> 118 118 3/4/2014 12:14 0
> 119 19 3/4/2014 12:35 1
> 119 19 3/4/2014 12:39 0
> 119 120 3/4/2014 0:00 4
> 119 120 3/4/2014 1:40 0
Given this data, why don't you simply:
select
AAA_key,
AP_KEY,
max(SEVERITY),
max(FIRSTOCCURRENCE) - min(FIRSTOCCURRENCE)
from
table
group by
AAA_key,
AP_KEY;
and then do whatever math you need on severity or FIRSTOCCURRENCE
differences.