Generate random sequence in Oracle - oracle

I am trying to generate random numbers into 2 columns where the first column is from_number and second column in to_number.
My query looks as following
select to_char(5001 + (level-1)),
to_char(5005 + (level-1))
from dual
connect by level <= 100;
My output is for the above query is:
5001 5005
5002 5006
5003 5007
5004 5008
5005 5009
and so on...
But my output should be like following:
5001 5005
5006 5010
5011 5015
5016 5020
and so on...
The second-row 'from_number' should be the first-row 'to_number'+1
How can achieve this?
Thanks in advance.

Note that what you are using here is not a random sequence. It is a fixed sequence. To know how to generate random number read this
Now coming back to your question, you can do it by playing a little with level. Note I reduced the <=100 to <=20 as we are using a multiplier of 5 so the maximum value you will get is 5005 + 20*5 - 5 = 6000. Change it back to <=100 if you want total of 100 rows.
select
to_char(5001 + (level*5) - 5 ),
to_char(5005 + (level*5) - 5)
from dual
connect by level <= 20;

Related

Calculate Daily/Monthly Energy consumption in SQL Server

I have a SQL Server table Energy_CC with two columns: time [int] (Epoch time) and E_CC14 [float]. Every 30 minutes, the total amount of my energy (kWh) is appended to the data table - something like this:
time
E_CC14
1670990400
5469.00223
1670992200
5469.02791
1670994000
5469.056295
1670995800
5469.082706
1670997600
5469.10558
1670999400
5469.128534
I tried this SQL statement:
SELECT
MONTH(DATEADD(SS, i.time, '1970-01-01')),
i.E_CC14 AS mwh,
iprev.E_CC14 AS prevmwh,
(i.E_CC14 - iprev.E_CC14) AS diff
FROM
Energy_CC i
LEFT OUTER JOIN
Energy_CC iprev ON MONTH(iprev.time) = MONTH(i.time) - MONTH(DATEADD(month, -1, i.time))
AND DAY(iprev.time) = 1
WHERE
DAY(i.time) = 1
GROUP BY
MONTH(i.time), i.E_CC14, iprev.E_CC14;
I expect the result for monthly like this :
time
E_CC14
DECEMBER-22
10223
Any help would be greatly appreciated.

Dynamic number system in Qlik Sense

My data consists of large numbers, I have a column say - 'amount', while using it in charts(sum of amount in Y axis) it shows something like 1.4G, I want to show them as if is billion then e.g. - 2.8B, or in millions then 80M or if it's in thousands (14,000) then simply- 14k.
I have used - if(sum(amount)/1000000000 > 1, Num(sum(amount)/1000000000, '#,###B'), Num(sum(amount)/1000000, '#,###M')) but it does not show the M or B at the end of the figure and also How to include thousand in the same code.
EDIT: Updated to include the dual() function.
This worked for me:
=dual(
if(sum(amount) < 1, Num(sum(amount), '#,##0.00'),
if(sum(amount) < 1000, Num(sum(amount), '#,##0'),
if(sum(amount) < 1000000, Num(sum(amount)/1000, '#,##0k'),
if(sum(amount) < 1000000000, Num(sum(amount)/1000000, '#,##0M'),
Num(sum(amount)/1000000000, '#,##0B')
))))
, sum(amount)
)
Here are some example outputs using this script to format it:
=sum(amount)
Formatted
2,526,163,764
3B
79,342,364
79M
5,589,255
5M
947,470
947k
583
583
0.6434
0.64
To get more decimals for any of those, like 2.53B instead of 3B, you can format them like '#,##0.00B' by adding more zeroes at the end.
Also make sure that the Number Formatting property is set to Auto or Measure expression.

ORACLE Program code to use filter and pivot table

IN EXCEL SHEET FOR THE BELOW INPUT, I HAVE TO USE FILTER TO “NET” FIRST WHERE NET=APB AND NEED TO FILTER “CODE VALUES” AS WDL, LRTF & NEED TO USE “PIVOT TABLE” TO GET OUTPUT WITH COUNT AS:
BUT I NEED CODE IN ORACLE TO RUN FOR THE FOLLOWING OUTPUT:-
INPUT:
STTID
AMOUNT
NET
CODE
SVPC12309A
5000
NFS
SOP
SVPC12309A
10000
NFS
WDL
000DHP11291
2500
APB
WDL
SVPC12309A
3000
CMV
LRTF
SVPC12309A
3000
CMV
WDL
DHP12341
4500
APB
LRTF
DHP23451
9500
APB
LRTF
DHP12341
5500
APB
LRTF
OUTPUT AS:
STTID
LRTF
WDL
TOTAL
000DHP11291
0
1
1
DHP12341
2
0
2
DHP23451
1
0
1
It appears you want something like
select sttid,
sum( case when code = 'LRTF' then 1 else 0 end ) ltrf,
sum( case when code = 'WDL' then 1 else 0 end ) wdl,
sum( case when code in ('WDL', 'LTRF') then 1 else 0 end) total
from your_table_name
group by sttid

How to extract all rows, for which row a particular criteria is met? Details in description

I am trying to load a set of policy numbers in my Target based on below criteria using Informatica PowerCenter.
I want to select all those rows of policy numbers, for which policy the Rider = 0
This is my source: -
Policy Rider Plan
1234 0 1000
1234 1 1010
1234 2 3000
9090 0 2000
9090 2 2545
4321 3 2000
4321 1 2000
Target should look like this: -
Policy Rider Plan
1234 0 1000
1234 1 1010
1234 2 3000
9090 0 2000
9090 2 2545
The policy number 4321 would not be loaded.
If I use filter as Rider = 0, then I miss out on below rows: -
1234 1 1010
1234 2 3000
9090 0 2000
9090 2 2545
What would be ideal way to load this kind of data using PowerCenter Designer?
Take the same source in one more qualifier in same mapping, use a filter as Rider=0 to get list of unique policy numbers that has Rider=0, then use a joiner with your regular source on policy number. This should work.
Another method, sort your data based on policy and Rider, and use variable ports with condition similar to below.
v_validflag=IIF(v_policy_prev!=policy, IIF(Rider=0, 'valid','invalid'), v_validflag)
v_policy_prev=policy
Then filter valid records.
There are many options. Here are two...
First:
It'll look like:
// AGGREGATOR \\
SOURCE >> SOURCE QUALIFIER >> SORTER << >> JOINER >> TARGET
\\============//
Connect all ports from Source Qualifier (SQ) to SORTER transformation (or sort in SQ itself) and define sorting Key for ‘Policy’ and ‘Rider’. After that split stream into two pipelines:
- Connect ‘Policy’ and ‘Rider’ to FILTER transformation and filter records by ‘Rider’ = 0. - After that link ‘Policy’ (only) to AGGREGATOR and set Group By to ‘YES’ for ‘Policy’. - Add a new port with FIRST or MAX function for ‘Policy’ port. This is to remove duplicate ‘Policy’-es.- Indicate ‘Sorted Input’ in the AGGREGATOR properties.- After that link ‘Policy’ from AGR to JOINER as Master in Port tab.
2.- Second stream, from SORTER, directly link to above JOINER (with aggregated ‘Policy’) as Detail. - Indicate ‘Sorted Input’ in the JOINER properties. - Set Join Type as ‘Normal Join’ and Join Condition as POLICY(master)=POLICY(detail) in JOINER properties.
... Target
Second option:
Just Override SQL in Source Qualifier...
WITH PLC as (
select POLICY
from SRC_TBL
where RIDER=0)
select s.POLICY, s.RIDER, s.PLAN
from PLC p left JOIN SRC_TBL s on s.POLICY = p.POLICY;
may vary depend on your source table constructions...

Return whole string if it cannot be split using substr / instr

My data looks like this
READINGS
----------
100 - Hot
200 - Cold
300 - Warm
80
8989970
I need to return just the first part of each line like so
READINGS
----------
100
200
300
80
8989970
I've tried the following
SUBSTR(READINGS,0,instr(READINGS,' - ')-1)
But if the instr() failes to find ' - ' the field ends up null like so
READINGS
----------
100 - Hot
200 - Cold
300 - Warm
null
null
Any advice on how to solve this?
You can use a case. Something like:
CASE WHEN instr(READINGS,' - ') = 0 THEN READINGS
ELSE SUBSTR(READINGS,0,instr(READINGS,' - ')-1) END

Resources