SQL - split numeric into 2 columns? - sql

I am trying to split some numeric keys in my table into separate columns (to help save space in SSAS, lower cardinality)
My data looks like the below..
LeadKey
1
2
3
5522
83746623
I want to split these into 2 columns... with 4 digits in each column. (where applicable, as anything 1>9999 won't have anything populated in the 2nd column)
So an example output of the above would be the below..
LeadKey Split1 Split2
1 1
2 2
35566 3556 6
5522 5522
83746623 8374 6623
How could I achieve this? I have split columns easily before using substring and a known character.. but never had to do a split like this. Does anyone have an approach to handle this?

Here is a solution in case you have the LeadKey numbers as int.
select LeadKey
,left(LeadKey, 4) Split1
,right(LeadKey, case when len(LeadKey)-4 < 0 then 0 else len(LeadKey)-4 end) Split2
from t
LeadKey
Split1
Split2
1
1
2
2
35566
3556
6
5522
5522
83746623
8374
6623
Fiddle

In this example, I used left for the Split1, and show the values past the 4th position for the Split2:
I've included a testing temporary table to hold our the testing values.
Feel free to adjust the code to work with your situation.
DECLARE #thelist TABLE
(
LeadKey int
);
INSERT INTO #thelist (LeadKey)
select 1 union all
select 2 union all
select 35566 union all
select 5522 union all
select 83746623
select cast(x1.LeadKey as varchar(19)),
Left(x1.LeadKey, 4) as 'Split1',
(case when len(x1.LeadKey) > 4 then right(x1.LeadKey, len(x1.LeadKey) - 4)
else '' end
) as 'Split2'
from #thelist as x1

Related

Select the column not like and combine column in one table [duplicate]

This question already has answers here:
How to SUM two fields within an SQL query
(9 answers)
Closed 10 months ago.
My question is how can I select the currency not equal to USD and CNY and rename as others. Also, combining it into a new column AMT_Others.
ID Name Date AMT_HKD AMT_JPY AMT_USD AMT_TWD AMT_CNY
1 Amy 01/04/2022 0 5000 0 0 0
2 Bill 01/03/2022 200 0 0 0 0
3 Cathy 02/02/2022 0 0 80 2000 200
Result:
ID Name Date AMT_Others
1 Amy 01/04/2022 5000
2 Bill 01/03/2022 200
3 Cathy 02/02/2022 2000
my code: (It cannot generate what I want, what should be added? Thanks)
select ID, Name, Date, (AMT_HKD, AMT_JPY and AMT_TWD) as AMT_Others
basically add them up as long as we have only one non zero value for all currencies except USD and CNY
Select ID, Name, Date,
AMT_HKD + AMT_JPY + AMT_TWD as AMT_Others
from yourtable
You can get your desired result by using + instead of comma or AND:
SELECT id, name, ydate, amt_hkd + amt_jpy + amt_twd AS amt_others FROM yourtable;
But this is quite unpleasant in case there are null values in some of your columns. In this case, the sum would be null, too, even if some of the columns are not null. You can use COALESCE to prevent this and to force building a sum. Null values will be replaced by 0 (or another value if you change it):
SELECT id, name, ydate,
COALESCE(amt_hkd,0) + COALESCE(amt_jpy,0) + COALESCE(amt_twd,0) AS amt_others
FROM yourtable;
You can check this and see the difference between the two query results here: db<fiddle
You should also prevent table names or column names that are SQL key words, like the "date" column in your example. That'
s why I renamed it in my answer. Of course, if you can't change this, you have to live with it, but if you can, you should choose another name.
the other answers will work, unless there can be null values, if there is a row where one of these columns is ´null` then the result will also be null
select 1 + 2 -- output is 3
select 1 + 2 + null -- output is null
solution to this problem
isnull(AMT_HKD, 0) -- easy but slow
COALESCE(amt_hkd, 0) -- easy but still not the fastest solution
case when amt_hkd is null then 0 else amt_hkd end -- fastest
SELECT ID, Name, Date,
AMT_HKD + AMT_JPY + AMT_TWD as AMT_Others
FROM tb_name;
This will work

How do I replace NULL in datediff with a text

I have a 2 different date columns and I'm using datediff to calculate the years of working. But there is nulls when the people is still working I wonder how can I replace it with a text 'Still Working'
So this is the table that I have
ID DateofHire DateofTermination
1 2011-07-05 NULL
2 2015-03-30 2016-06-16
3 2011-07-05 2012-09-24
4 2008-01-07 NULL
I have use the datediff formula and it shows like this :
DATEDIFF(year,DateofHire,DateofTermination) as 'Years of working'
ID Years_of_Working
1 NULL
2 1
3 1
4 NULL
How can I replace the NULL after I wrote a datediff query i wanted to replace it with 'Still Working'
The desired table would be like this:
ID Years_of_Working
1 Still working
2 1
3 1
4 Still Working
I assume that you will get Years_of_Working as Integer values. Replacing the attribute of the table with a different type is inconsistent.
I would just replace NULL values with 0, which would implicitly mean, that these people are still working.
So if the original table is called hire_info, then solution would look like this:
with diff_years as (
select ID,DATEDIFF(year,DateofHire,DateofTermination) as 'Years of working'
from hire_info
)
select ID, ISNULL(Years_of_Working,0) Years_of_Working
from diff_years
You can use this query. You should change the table name to your own table name.
SELECT id, if(isnull(DateofTermination),"Still working",TIMESTAMPDIFF(year,DateofHire,DateofTermination)) as 'Years of working' from date_diff;

Finding a value in multiple columns in Oracle table

I have a table like below
ID NUMBER 1 NUMBER 2 NUMBER 3 LOC
1-14H-4950 0616167 4233243 CA
A-522355 1234567 TN
A-522357 9876543 WY
A-522371 1112223 WA
A-522423 1234567 2345678 1234567 NJ
A-A-522427 9876543 6249853 6249853 NJ
and I have a bunch of values (1234567, 9876543, 0616167, 1112223, 999999...etc) which will be used in where clause, if a value from where clause found in one of the three Number columns (Number 1 or Number 2 Number 3) then I will have to write that to output1 (its like VLOOKUP of Excel).
If the value is found in more than one of the three columns then it will be different output2 with a flag as MultipleMatches. If the value is not found in any of the three columns then it should be in Output2 with flag as No Match. I tried using self join and or clauses, but not able to get what I want.
I want to write the SQL to generate both outputs. Outputs will include all the columns from the above table. For eg:
Output 1 from above sample data will look like
ID NUMBER 1 NUMBER 2 NUMBER 3 LOC
1-14H-4950 0616167 4233243 CA
A-522371 1112223 WA
Output 2 will be like:
ID NUMBER 1 NUMBER 2 NUMBER 3 LOC Flag
A-522423 1234567 2345678 1234567 NJ Multiple Match
A-A-522427 9876543 6249853 6249853 NJ Multiple Match
1234 No Match
I want to write the SQL to generate both outputs.
One SELECT operator cannot produce two output sets.
The main question is, why split the output when that the difference is only in the FLAG column? If you really need two different output of the result, then you can do this:
(Rightly) create a common cursor for the query, where the FLAG column will be calculated and split the output screens already in the UI.
drop table test_dt;
create table test_dt as
select '1-14h-4950' id,null num1,616167 num2,4233243 num3,'ca' loc from dual
union all
select 'a-522355',null ,1234567,null,'tn' from dual union all
select 'a-522357',null ,9876543,null,'wy' from dual union all
select 'a-522371',null ,1112223,null,'wa' from dual union all
select 'a-522423',1234567,2345678,1234567,'nj' from dual union all
select 'a3-522423',null,null,null,'nj' from dual union all
select 'a-a-522427',9876543,6249853,6249853,'nj' from dual;
--
select
d.*,
case when t.cc_ndv=0 and t.cc_null=3 then 'Not matching'
when t.cc_ndv=(3-t.cc_null) then 'Once'
else 'Multiplay match'
end flag
--t.cc_ndv,
--t.cc_null
from test_dt d ,lateral(
select
count(distinct case level when 1 then num1
when 2 then num2
when 3 then num3
end ) cc_ndv,
count(distinct case level when 1 then nvl2(num1,null,1)
when 2 then nvl2(num2,null,2)
when 3 then nvl2(num3,null,3)
end ) cc_null
from dual connect by level<=3 and sys_guid()is not null
) t;
Or
create a procedure(see to dbms_sql.return_result) that returns a some data sets.
Process these data of cursors / datasets separately.

How to do the sum for each row of the values of a column?

I have a SQL table containing a column of list of int, like this
Index Column to sum
1 1:5:13:3:6:7:11:2:4:1:2:5
2 1:7:2:1:1
3 19:05:05
4 2:1:1:5:1:4:64:177:86:75:2:83:2:57:1
5 43
6 1:1:1:3:10:6:1
7 2:11:4:3:1
8 1:5:2:3:34:2:2
9 4:3:1:2
10 4:1:1:4
I would like to calcul in SQL the sum for each row of the Column to sum value.
It would give something like this:
1 60
2 12
...
10 10
Any idea?
Thanks
Your first effort should go into fixing your data model, as commented by Gordon Linoff. The numbers should be stored in a separate table, with each value on a separate row.
In Postgres, you can split the values to rows using regexp_split_to_table(), then aggregate:
select t.id, sum(x.val::int) result
from mytable t
cross join lateral regexp_split_to_table(t.column_to_sum, ':') as x(val)
group by t.id

Searching for a number in a database column where column contains series of numbers seperated by a delimeter '"&" in SQLite

My table structure is as follows :
id category
1 1&2&3
2 18&2&1
3 11
4 1&11
5 3&1
6 1
My Question: I need a sql query which generates the result set as follows when the user searched category is 1
id category
1 1&2&3
2 18&2&1
4 1&11
5 3&1
6 1
but i am getting all the results not the expected one
I have tried regexp and like operators but no success.
select * from mytable where category like '%1%'
select * from mytable where category regexp '([.]*)(1)(.*)'
I really dont know about regexp I just found it.
so please help me out.
For matching a list item separated by &, use:
SELECT * FROM mytable WHERE '&'||category||'&' LIKE '%&1&%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.