Get the Middle Value of the Column - teradata

I have Certain records in my Column as below
Col1
G XXXXX AB
G XXX XXX AB
G XXX XXXXX AB
G XXX AB
Scenario is to get everything in the middle if it starts with G and ends with AB
so Resultwould be some thing like this
XXXXX
XXX XXX
XXX XXXXX
XXX

We can try removing the leading G and trailing AB from matching records using a regex replacement:
SELECT
Col1,
REGEXP_REPLACE(Col1, '^G\s*|\s*AB$', '') AS Col1_Middle
FROM yourTable
WHERE
Col1 LIKE 'G%AB';
Demo
If you only want records starting with G followed by a space, and ending with AB preceded by a space, then use the following WHERE clause instead:
WHERE Col1 LIKE 'G % AB'
Demo given using Oracle (there are no web based Teradata demo tools), but the syntax should also work with Teradata.

Related

SQLite select order by alphabet first, then all other characters

I have a SQLite db with a table, containing rows with different names. For example:
id
name
1
antony
2
%
3
10
4
stackoverflow
5
john
I get the data from this table with
SELECT * FROM table WHERE 1 ORDER BY name Asc LIMIT ?, ?
And it returns
id
name
2
%
3
10
1
antony
5
john
4
stackoverflow
But i want it to return names in alphabetical order first, then all other names which starts with non letters in the right order too. So i want to get:
id
name
1
antony
5
john
4
stackoverflow
2
%
3
10
How can i achieve that?
Use the operator GLOB to check if the name starts with a letter in the ORDER BY clause:
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, name
See the demo.
Thanks to #forpas, just wish to add
If you wish to make Case insensitive sorting, you may try as below
SELECT *
FROM tablename
ORDER BY name GLOB '[A-Za-z]*' DESC, Upper(name)

sqlite3 unable to select column from table in attached database

I'm merging two similar databases (A and B) where I take entries in A that aren't in B and add them to B. I was trying to do it with an attached database (I used .open A.db and attach 'B.db' as B). Both have table C which contains column D. I need to do some inner joins on other tables, but first I need to do something like:
select D from C where not exists (select D from B.C where C.D = B.C.D);
And I get the error "no such column: B.C.D
Are you not allowed two dots in one expression? Do I need to somehow use "as" to fix that, or would that even work?
If the 2 tables contain a column D then you should not get that error.
But, there is another problem in the subquery:
select D from B.C where C.D = B.C.D
C.D is interpreted as the column D of the table B.C and not the table A.C as you might expect.
Set an alias for the table A.C and use it inside the subquery:
select D from C AS x where not exists (select D from B.C where x.D = B.C.D);
Or, use EXCEPT:
select D from C
EXCEPT
select D from B.C
Use quotes around those items that have dots in them
select D from C where not exists (select D from "B.C" where C.D = "B.C.D");
There is one question - where is the table "B" in your clause?

Updating Fields sequentially based on conditions

I have this current example data set
NEW_ID Name OLD_ID New_Name
123 Hello XYZ
124 How XYZ
125 Are XYZ
126 My ABC
127 Name ABC
128 Is ABC
129 Alex ABC
My objective is to amend the Name field to a new naming convention to be stored in New_Name- ie Hello_Part_1, How_Part_2, Are_Part_3 where all these records share an OLD_ID - in this case XYZ. Similarly, with My_Part_1, Name_Part_2, Is_Part_3, Alex_Part_4 etc with IDs that equal ABC.
I'm using SQL Lite with an Import of .CSV File.
The naming convention is as follows - NAME_PART_X where X increments on the number of records within that 'Group' of OLD_IDs.
SQL does not work sequentially; you have to express the operation independently for each row.
The number you want is the count of rows with the same old ID that also have a new ID that is the same or smaller as the new ID of the current row.
This can be computed with a correlated subquery:
UPDATE MyTable
SET New_Name = Name || '_Part_' ||
(SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.OLD_ID = MyTable.OLD_ID
AND T2.NEW_ID <= MyTable.NEW_ID);

how to remove duplicates in a row using sqlite having numbers which contains special characters

I have a question which is similar to Find the number of duplicates in a row using sqlite except that the numbers mentioned in the column may contain brackets too . For eg:
Name Num0 Num1 Num2 Num3 Num4 Num5 Num6 Num7
1)John (12)34 1234 (123)4
2)Hebbar 234
3)Jim (9)876 9876 (9876)
4)Kim 111 111 111
5)Kate 666
Now when i run sqlite the query i should be getting the results as John, Jim and Kim respectively.
I am using C language for my project.
Thanks a lot in advance.
To remove the parentheses, use replace() to replace them with an emptry string:
SELECT replace(replace(Num0, '(', ''), ')', '') AS Num0_without_parens,
...
FROM MyTable;
This can then be processed further, as shown in the linked question.

LINQ grouping with count issue

I'm having trouble trying to group a LINQ query with a count correctly. As an example I'm trying to query a datatable with the following data:
PC EC CC
ABC XXX US
ABC XXX CA
ABC XXX UK
DEF YYY US
DEF YYY CA
DEF YYY UK
DEF ZZZ US
DEF ZZZ CA
DEF ZZZ UK
HIJ AAA US
HIJ AAA CA
HIJ AAA UK
I want to use the count function to show the number of different values of EC for each value of PC, removing the values in CC. In other words the result for the above dataset should be:
PC Count EC
ABC 1
DEF 2
HIJ 1
I've tried several different ways of getting to this result but I keep hitting a wall. My last attempt looked like this:
Dim test = From r In ( _
From s In Base _
Group By s.Base_PC, _
s.Base_EC _
Into g = Group) _
Group r By key = New With {r.Base_PC} _
Into Group _
Select key.Base_PC, Group.Count
Any ideas where I'm going wrong? Thanks!
Here the method syntax since it's too much for query syntax:
Dim counts = base.AsEnumerable().
GroupBy(Function(r) r.Field(Of String)("PC")).
Select(Function(g) New With {
.PC = g.Key,
.CountEC = g.Select(Function(r) r.Field(Of String)("EC")).
Distinct().
Count()
})
For Each x In counts
Console.WriteLine("PC={0} Count of distinct EC's={1}", x.PC, x.CountEC)
Next
First you need to group by the "PC" column, then you select an anonymous type with the group's key and the count of distinct values of the "EC" column.

Resources