how to sort a column by slash in sql server - sorting

I don't get how to sort or order by column that contains values as following
abc/aa
aa
bb/cba
bb/aa
cc
Now I need the values in the column to be displayed as the values containing slash to be displayed last and those that don't have slash to be displayed at first.
Required Output
aa
cc
cba
abc/aa
bb/aa
bb/cba
Please guide me
Thanks in Advance

You don't provide your query, but the form will be
DECLARE #Tbl TABLE (CharVal VARCHAR(50))
INSERT INTO #Tbl VALUES ('abc/aa'),('aa'),('bb/cba'),('bb/aa'),('cc')
SELECT CharVal FROM #Tbl
ORDER BY CASE WHEN PATINDEX('%/%',CharVal) > 0 THEN 1 ELSE 0 END, CharVal
Output:
CharVal
aa
cc
abc/aa
bb/aa
bb/cba
EDIT: Corrected 0/1 reversal in the CASE statement that resulted in incorrect sort order, thanks #Aaron_Bertrand! Also added populating a temp table with the data and showing the output.

Related

Supress leading zeros from oracle table extract to a file

I am extracting data from oracle table to a text file and I have below number columns. When I select the below columns to a file it gives me all leading zeros which I wanted to suppress.
Select ltrim(col_1,'0'),ltrim(col_2,'0'),ltrim(col_3,'0') from table1
Datatype:
Col_1 ---NUMBER(10,2),
Col_2 ---NUMBER(38,0),
Col_3 ---NUMBER(15,1)
Current Output:
00000303.44|0| 00000000000008.2
00000000.00|26| 00000000000030.2
00000473.40|0| 00000000000010.0
Expected Output:
303.44|0|8.2
0|26|30.2
473.4|0|10
Please let me know if i need to change the datatype to get the Expected output. I even tried TO_CHAR(TRIM(LEADING 0 FROM col_name) i did not get the expected output.
This is caused by the datatypes set in the last output stage of your datastage job. When a column is set a decimal, datastage will fill the remaining positions with leading zeros up to the size if your decimal field.
The easiest way to get around this is to place a transform prior to the file output stage and convert all the columns to a varchar at the last stage trimming all the leading zeros.
Since the data is not in number and possibly in varchar/varchar2;
conversion is required; you can use to_number to address this;
Using one of your sample data in below case
select
to_number(00000000000008.2) as num1,
to_number('00000000000008.2') as chr1,
trim(00000000000008.2) as num2,
trim('00000000000008.2') as chr2,
ltrim(00000000000008.2,'0') as num3,
ltrim('00000000000008.2','0') as char3
from dual

Strange Behavior of oracle query

I am not an oracle expert. I faced a very strange problem but do not know why this occur.
My query is
SELECT hc.id, hc.owner_name, hc.national_id, hc.phone_no, hc.location, hc.status, hc.expiry_status, od.office_title AS issuer, hc.create_date, hc.email, hc.LATTITUDE, hc.LONGITUDE, hc.HASAD_NO, hc.NUMBERATION, hc.BREEDING_TYPE, hc.PROGENY, hc.office_id, hc.issuer_id, hc.expiry_status, hc.status FROM health_cards hc
LEFT JOIN office_details od ON od.office_id = hc.issuer_id AND od.lang = :lang
WHERE hc.id = :search_data_num OR hc.national_id = :search_data_num or hc.phone_no = :search_data_num OR hc.owner_name LIKE :search_data ORDER BY hc.create_date DESC, hc.id desc OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY
When i m running this query i m getting following error
ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
But if i remove OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY from my query it work perfectly.
I want to know the reason why this query not working with offset statement.
You have repeated column hc.status.
`select 1 as "A" as "A" from dual` - execute OK;
`select * from (select 1 as "A"
, 2 as "A"
from dual);` - ORA-00918: column ambiguously defined
If you and offset, oracle probably does something similar.
You have the column hc.expiry_status twice in your select list.
The problem is that we allow this in a select list, but not within an inline view. When you add the row limiting clause, Oracle transforms the query and the transformation uses an inline view. There is a bug, 13687511 which is marked as fixed.
Meanwhile, the workaround is to either not select it twice, or alias the column(s).

Duplicate removal from a table using Informatica

I have a scenario to be implemented in informatica where I need to remove duplicate records from a table based on PK. But I need to keep the 1st occurrence of the PK values and remove the others(in case of duplicate PK).
For example, If my source has 1,1,1,2,3,3,4,5,4. I want to see my target data as 1,2,3,4,5. I have to read data from the same table and need to load into the same table., no new table can be introduced. please help me with your inputs.
Thanks in Advance!
I suppose you want the first occurrence because there are other (data) columns in addition to the key you entered. Therefore you want
1,b
1,c
1,a
2,d
3,c
3,d
4,e
5,f
4,b
Turned into
1,b
2,d
3,c
4,e
5,f
??
In that case try this mapping layout:
SRC -> SQ -> SRT -> AGG -> TGT
SEQ /
Where the sorter is set to sort on the KEY,sequence_port (desc)
And the aggregator is set to group by the KEY, and the sequence_port should not go out of the sorter
Hope you can follow me :)
There are multiple ways to do this, the simplest would be too do it in the SQL override.
Assuming the example quoted above, the SQL would be like this. General idea is to set a row number for a primary key ( so if you have 3 rows with same pk they will have 1,2,3 as row numbers before being reset for the next pk)
SQL:
select * from (
Select primary_key,column2 row_number() over (partition by primary_key order by primary_key) as distinct_key) where distinct_key=1
Before:
1,b
1,c
1,a
2,d
3,c
3,d
Intermediate query:
1,c,1
1,a,2
2,d,1
3,c,1
3,d,2
output:
1,c
2,d
3,d
I am able to achieve this by following the below steps.
1. Passing Sorted data(keys are EMP_ID, MOBILE, DEPTID) to an expression.
2. Creating the following variable ports in the expression and getting the counts.
V_CURR_EMP_ID = EMP_ID
V_CURR_MOBILE = MOBILE
V_CURR_DEPTID = DEPTID
V_COUNT =
IIF(V_CURR_EMP_ID=V_PREV_EMP_ID AND V_CURR_MOBILE=V_PREV_MOBILE AND V_CURR_DEPTID=V_PREV_DEPTID ,V_COUNT+1,1)
V_PREV_EMP_ID = EMP_ID
V_PREV_MOBILE = MOBILE
V_PREV_DEPTID = DEPTID
O_COUNT =V_COUNT
3. In the next transformation which is filter, I am taking only the records which have count more than 1 and deleting them using update strategy(DD_DELETE).
Here is the mapping flow.
SQ->SRTR->EXP->FIL->UPD->TGT
Also, when I tried to delete them using aggregator , it is deleting only the first occurrence of duplicates but not all.
Thanks again for your inputs!

How to insert data from the same row by extracting text in Oracle PL/SQL?

I have the below table:
(AddressID,ShortAddress,FullAddress).
Now, The FullAdress column normally contains address like this:
Bellvue East,204-Park Avenue,Zip-203345.
I need to write a script, which will extract the first part before the first ',' in the full address and insert into ShortAddress column.
So, the Table Data before executing the script:
AddressID|ShortAddress|FullAddress
1 | NULL |Bellvue East,204-Park Avenue,Zip-203345,United Kingdom
2 | NULL |Salt Lake,Sector-50/A,Noida,UP,India
And after executing the script, it should be:
AddressID|ShortAddress|FullAddress
1 |Bellvue East|Bellvue East,204-Park Avenue,Zip-203345,United Kingdom
2 |Salt Lake|Salt Lake,Sector-50/A,Noida,UP,India
I need to write it in Oracle PL/SQL.
Any help will be highly appreciated.
Thanks in advance.
Try this UPDATE:
UPDATE yourTable
SET ShortAddress = COALESCE(SUBSTR(FullAddress, 1, INSTR(FullAddress, ',') - 1),
FullAddress)
This update query will assign the first CSV term in the full address to the short address. If no comma be present, then it will assign the entire full address.

more efficent way of reading data from two table and writing them in a new one using batch

I'm trying to write a spring batch to move data from two tables to a single table. I'm having a problem now and I thought of many ways to solve this problem but I'm still wondering if there is a more efficent solution to my problem?
Basically the problem is, I have two tables lets call them table A and table B and their structure is as the following:
table A
column 1A column 2A
======== ========
bmw 123555
nissan 123456777
audi 12888
toyota 9800765
kia 85834945
table B
column 1B column 2B
======== ========
12 caraudi
123456 carnissan
123 carbmw
0125 carvvv
88963 carbbn
what I'm trying to do is to create a table c from the batch's wrtier which holds all the data from table B (column 1B and column 2B)and column 1A only without losing any data from both tables and without writing duplicated data based on column 2A and column 1B. column A and column B have only one column in common (coulmn 1B == column 2A) but column 2A has a 3 digits suffix added to each id so if we do a join and compare I have to use a substr method and it will be very slow coz I have huge tables.
The other solution I thinked of is to have a reader for table A and write all results to tempA table without the suffix, then another reader that compare tables tempA and table B and write the data to table c as the following
table c
column 1A ( can be nullable because not all the records in column 2A exists in column 1B)
column 1B
column 2B
so the table will look like this
table C
column 1c column 2c column 3c
========= ========= =========
12 caraudi audi
123456 carnissan nissan
123 carbmw bmw
0125 carvv
88963 carbbn
9800765 toyota
85834945 kia
is this the bet way to solve the problem? or is there any other way that is more efficient?
thanks in advance!
Before giving up on a LEFT OUTER JOIN from tableA to tableB (or a FULL OUTER JOIN if your query conditions require it) consider using db2expln or the Visual Explain utility in IBM Data Studio to determine the cost of some alternative ways to perform a "begins with" match on VARCHAR columns:
ON a.col2a LIKE b.col1b || '___'
ON a.col2a >= b.col1b || '000' AND a.col2a <= b.col1b || '999'
If 1b is a CHAR column, you might need to trim off its trailing spaces before concatenating additional characters to it: RTRIM( b.col1b ) || '000'
Assuming column 1b is indexed, one prefix-based matching predicate or another is bound to make a join between those two tables less expensive than creating, populating, and joining to your own temp table. If I'm wrong (or there are other complicating factors) and a temp table ends up being the best option, be sure to use a declared global temporary table (DGTT) so you can avoid the logging overhead of populating it.

Resources