I want to calculation subtraction field energy with next value energy I just try with my query but I think result is still wrong
For my code
SELECT
datapm.id,
datapm.tgl,
CONVERT ( CHAR ( 5 ), datapm.stamp , 108 ) stamp,
datapm.pmid,
datapm.vavg,
datapm.pf,
( CAST (datapm.energy AS FLOAT) - (select top 1 CAST (energy AS FLOAT) from datapm as dt2 where dt2.id > datapm.id and dt2.tgl=datapm.tgl)) as energy
FROM
datapm
GROUP BY
datapm.id,
datapm.tgl,
datapm.stamp,
datapm.pmid,
datapm.vavg,
datapm.pf,
datapm.energy
ORDER BY tgl desc
My sample
id pmdi tgl stamp vavg pf energy
787 SDPEXT_2 2021-09-06 06:00:00.0000000 407.82 0.98 1408014.25
788 SDPEXT_2 2021-09-06 07:00:00.0000000 403.31 0.85 1408041.00
789 SDPEXT_2 2021-09-06 08:00:00.0000000 408.82 0.87 1408081.75
Result I want
id pmdi tgl stamp vavg pf energy
787 SDPEXT_2 2021-09-06 06:00:00.0000000 407.82 0.98 -2.675
788 SDPEXT_2 2021-09-06 07:00:00.0000000 403.31 0.85 -4.075
789 SDPEXT_2 2021-09-06 08:00:00.0000000 408.82 0.87 -11.012
remove the GROUP BY in your query, you are not using any aggregate function
If energy is already in numeric data type, don't convert to float.
use LEAD() to get the next row value
SELECT . . .
(d.energy - LEAD (d.energy) OVER (PARTITION BY d.tgl
ORDER BY d.id)) / 10
FROM datapm d
not sure what is the actual formula, but looking at the result, you need to divide by 10 to obtain it
I'm looking to round values like
2.39 -> 2.40
4.66 -> 4.70
2.11 -> 2.15
2.10 -> 2.10
2.50 -> 2.50
5.89 -> 5.90
How can I manage this in SQL?
Thanks
problem solved :
SELECT FLOOR ( (1.11 + 0.04) * 20) / 20 FROM DUAL;
test:
WITH t (my_number)
AS (SELECT 3.1001 FROM DUAL
UNION ALL
SELECT 2.39 FROM DUAL
UNION ALL
SELECT 4.66 FROM DUAL
UNION ALL
SELECT 2.11 FROM DUAL
UNION ALL
SELECT 2.10 FROM DUAL
UNION ALL
SELECT 2.50 FROM DUAL
UNION ALL
SELECT 5.89 FROM DUAL)
SELECT my_number, FLOOR ( (my_number + 0.04) * 20) / 20 round_on_number
FROM t;
3.1001 3.1
2.39 2.4
4.66 4.7
2.11 2.15
2.1 2.1
2.5 2.5
5.89 5.9
I would like to create a column that will get the total hours based on the store column and the hours column. See below table. So it will total up rep1,2,3 from store 142 and total rep 1,2 from store 356. Then I would also like to devide hours into total to get a contribution% column
Date store rep hours total cont%
--------------------------------------------------
x 142 rep1 5 11 0.45
x 142 rep2 2 11 0.18
x 142 rep3 4 11 0.36
x 356 rep1 4 7 0.57
x 356 rep2 3 7 0.42
Thank you!
You want window functions:
select t.*, sum(hours) over (partition by store) as total,
t.hours * 1.0 / sum(hours) over (partition by store) as cont_percent
from t;
If a decimal value is 0.40 I'd like to TO_CHAR(0.40) and get '0.40' back. Likewise, 50 should be to_char'd to '50'. As simple as what goes in, comes out, but as a CHAR, in all cases. So far I've tried the following:
select to_char(a, '99D90') test1,
to_char(a, '90D90') test2,
to_char(a, 'FM90D99') test3,
rtrim(to_char(a, 'FM90D99'), to_char(0, 'D')) test4
from (
select 50 a from dual
union all select 50.57 from dual
union all select 5.57 from dual
union all select 0.35 from dual
union all select 0.4 from dual
union all select 0.80 from dual
union all select .88 from dual
)
order by a;
returns:
TEST1 TEST2 TEST3 TEST4
------ ------ ------ ------
.35 0.35 0.35 0.35
.40 0.40 0.4 0.4
.80 0.80 0.8 0.8
.88 0.88 0.88 0.88
5.57 5.57 5.57 5.57
50.00 50.00 50. 50
50.57 50.57 50.57 50.57
As you can see, what may work in one case doesn't in another. Do I have to test for whole numbers, decimal numbers, then leading and trailing zeros and apply a different formatter for each? Is there just a simple cast-like construct? (tried cast too btw, similar issues).
Solved
Oracle does not store or display formatting (even on non-persisted values as shown below). A Formatter must be applied for anything other than that.
0.4, .4 and 0.4000 are all the same. It is just the way you want to display the number.
As I understand, you want to preserve the rows as string, and not just display them. Then you could handle the conditions using a CASE expression.
TRUNC(NUMBER) truncates the decimal portion and gives you a whole number. This will be the driving condition.
So, when TRUNC(NUMBER) = 0, it means it has only decimal part, and we need to append a zero in the beginning, else just convert it into text.
For example,
SQL> WITH DATA AS(
2 select 50 a from dual
3 union all select 50.57 from dual
4 union all select 5.57 from dual
5 union all select 0.35 from dual
6 union all select 0.4 from dual
7 UNION ALL SELECT 0.80 FROM dual
8 UNION ALL SELECT .88 FROM dual
9 )
10 SELECT
11 CASE
12 WHEN TRUNC(A) = 0
13 THEN ltrim(to_char(a, '0D99'), ' ')
14 ELSE TO_CHAR(a)
15 END text
16 FROM DATA;
TEXT
----------------------------------------
50
50.57
5.57
0.35
0.40
0.80
0.88
7 rows selected.
SQL>
I'm pretty new to SQL.
I have a database with records based on road/milepoints. My goal is to get an average value every 52.8 ft along the road. My related table has data every 15 ft, this table of course has a foreign key relating it to the primary table.
If I wanted to pull out the average value every 52.8 ft, along a given milepost, how would I go about this?
Example Data:
RecID Begin_MP End_MP
100 0 0.56
RecID MP Value1 Value2
100 0 159 127.7
100 0.003 95.3 115.3
100 0.006 82.3 107
100 0.009 56.5 74.5
100 0.011 58.1 89.1
100 0.014 95.2 78.8
100 0.017 108.9 242.5
100 0.02 71.8 73.3
100 0.023 84.1 80.2
100 0.026 65.5 66.1
100 0.028 122 135.8
100 0.031 99.9 230.7
100 0.034 95.7 111.5
100 0.037 127.3 74.3
100 0.04 140.7 543.1
The first Data is an example of a Road. The second subset of data are the values I need to query out every 52.8 ft.
Thank you
You could group the data in 52.8 feet blocks. One way to do that is to divide the distance by 52.8, and round that to a whole number. That way, 25 belongs to group 1, 100 belongs to group 2, 110 belongs to group 3, and so on.
In SQL Server, you'd write this like:
select
52.8 * cast(dist/52.8 as int) as Distance
, avg(value1)
, avg(value2)
from YourTable
group by cast(dist/52.8 as int)
Below is an example with your data. Because the data runs from 0 to 0.04, I've made it calculate averages per 0.01 feet block:
declare #Road table (RecID int, Begin_MP float, End_MP float)
insert into #Road select 100, 0, 0.56
declare #Values table (RecID int, MP float, Value1 float, Value2 float)
insert into #Values values
(100, 0 , 159 , 127.7),
(100, 0.003, 95.3 , 115.3),
(100, 0.006, 82.3 , 107),
(100, 0.009, 56.5 , 74.5),
(100, 0.011, 58.1 , 89.1),
(100, 0.014, 95.2 , 78.8),
(100, 0.017, 108.9, 242.5),
(100, 0.02 , 71.8 , 73.3),
(100, 0.023, 84.1 , 80.2),
(100, 0.026, 65.5 , 66.1),
(100, 0.028, 122 , 135.8),
(100, 0.031, 99.9 , 230.7),
(100, 0.034, 95.7 , 111.5),
(100, 0.037, 127.3, 74.3),
(100, 0.04 , 140.7, 543.1);
select
r.RecID
, cast(v.MP/0.01 as int)*0.01 as StartMP
, AVG(v.Value1) as AvgVal1
, AVG(v.Value2) as AvgVal2
from #Road as r
left join #Values as v
on r.RecID = v.RecID
group by r.RecID, cast(v.MP/0.01 as int)
This prints:
RecID StartMP AvgVal1 AvgVal2
100 0.00 98,275 106,125
100 0.01 87,4 136,8
100 0.02 85,85 88,85
100 0.03 107,63 138,83
100 0.04 140,7 543,1