Get the sum of partial value in a column - sql

Consider there is a table tableA
col1 col2
1 some random string and number 1213 aa5 string aaasome number
2 some random string 432682 aa3 test
1 aa7
I need to get the result as below.
1 12
2 3
group by col1 and the result will be 5+7 (the partial int after the 'aa' string)
To add more clarity to the question,the col2 has some other strings as well.. like test test test aa2 again test test 23u45 ajsdk 4834... . Here i need to pick the 2 alone.
kindly suggest a solution for this.

You need to get rid of the prefix, cast to a number, and sum. One method looks like:
select col1, sum(cast(replace(col2, 'aa', '') as number)
from tablea a
group by col1;

You can use regular expression to get the required digits from the string:
Select col1, sum(regexp_replace(col2,'(^|.*\s)aa(\d+)(\s.*|$)', '\2'))
From t
Group by col1
demo

Related

BigQuery - Concatenate multiple columns into a single column for large numbers of columns

I have data that looks like:
row
col1
col2
col3
...
coln
1
A
null
B
...
null
2
null
B
C
...
D
3
null
null
null
...
A
I want to condense the columns together to get:
row
final
1
A, B
2
B, C, D
3
A
The order of the letters doesn't matter, and if the solution includes the nulls eg. A,null,B,null ect. I can work out how to remove them later. I've used up to coln as I have about 200 columns to condense.
I've tried a few things and if I were trying to condense rows I could use STRING_AGG() example
Additionally I could do this:
SELECT
CONCAT(col1,", ",col2,", ",col3,", ",coln) #ect.
FROM mytable
However, this would involve writing out each column name by hand which isn't really feasible. Is there a better way to achieve this ideally for the whole table.
Additionally CONCAT returns NULL if any value is NULL.
#standardSQL
select row,
(select string_agg(col, ', ' order by offset)
from unnest(split(trim(format('%t', (select as struct t.* except(row))), '()'), ', ')) col with offset
where not upper(col) = 'NULL'
) as final
from `project.dataset.table` t
if to apply to sample data in your question - output is
Not in exact format that you asked for, but you can try if this simplifies things for you:
SELECT TO_JSON_STRING(mytable) FROM mytable
If you want the exact format, you can write a regex to extract values from the output JSON string.

How would you explain this SQL code in regards to select 1 from dual where exists

I need to explain this to someone in laymen's terms. How would you summarize this if you were talking to a 5 year old, lets say.
I would explain it as:
"find and display data that is from this table: a column from tableA where col1 = 5000 and col2 > ? and col3 > ? and col4 <= ?
Is there a better way you can find to word this? Thanks.
select 1
from dual
where exists
(select 1 from tableA where col1 = 5000 and col2 > ? and col3 > ? and col4 <= ? )
Me: This query will return one row with a projection of 1 if there is at least one row in TABLEA where COL1 equals 5000 and COL2 is bigger than some unknown value and COL3 is bigger than some unknown value and COL4 is not bigger than some unknown value. Otherwise it will return no rows. It uses DUAL, which is a special table in Oracle guaranteed to return a single row.
Five-year-old: This code seems rather shonky. Did Spud write it? Can Bob fix it?

how to get duplicate sub string count in db2 sql

How to get duplicate sub string count in db2 sql
col 1
|abc_123|
| abc_2 |
|xyz_123|
output will be
col1 output
|abc_123| |2|
|abc_23 | |1|
|xyz_123|
How to get count using substr(), locate() command and group by clause in SQL db2 ,I want two col i.e col1 and output
First take the part of the string and then do the group by on that part to find the count of those part of string
select substr(col1, 1,locate('_',col1)-1) col1, count(col1) from table t1
group by substr(col1, 1,locate('_',col1)-1)

SQL - how to check in WHERE clause if some sign (#) exists at least two times in string (i.e. AB#CDEF#)?

I have column with string values.
I would like to have Select statement which will return all rows where sign # is present two or more times?
For example:
COL1 COL2
1 AB#CDE#
2 AB#
3 AB#CDE#FG#IJ#
If I do
SELECT * FROM TABLE WHERE COL2 LIKE "%#%"
it will return all three rows but I need 1st and 3rd.
Thank you,
SELECT * FROM TABLE WHERE COL2 LIKE "%#%#%"
As long as there's at least 2 instances of "#" then this will catch it.

Postgres math expression calculcated for each row in table

Using PostgreSQL, supposing a table like the following:
12184 | 4 | 83
12183 | 3 | 171
12176 | 6 | 95
How can I compute a math expression for each row in the table?
For example, to divide column 2 by column 3, such that the output would be:
12184 | 0.04819277108
12183 | 0.01754385965
12176 | 0.06315789474
My instinct was to try:
SELECT col1, col2 / col3 FROM table_name;
But that return the ceiling (ie. rounded-down) integer part, I need the floating point value.
Typical cast trick needed because col2 and col3 are integers (so result is by default an integer)
select col1, col2/col3*1.0 from table
or
select col1, col2/col3::float from table
or (SQL Standard way)
select col1, col2/cast(col3 as float) from table
You can use arithmetic expressions in SELECT clause, like this:
SELECT col1 / col2 AS new_name
FROM t
select col1, col2/col3 from table;
Should work. Aren't col2 and col3 numeric?
Try query like this:
SELECT col1, col2 / col3::float FROM table_name;
In PgSql the columns are typed. So if you want to operator on them; you need to cast the column.
suppose you have a column 'minutes' and you wanna add '+5' in every values of column 'mintues'
Because you are adding and integer value the minutes column must be a integer only then the addition can be performed.
hence incorrect way:
select *, minutes+5 from my table >> syntax error
select *, minutes::int + 5 from mytable >> give the output