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)
Related
I am reading some raw input which looks something like this:
20 abc def
21 ghi jkl
mno pqr
23 stu
Note the first two rows are "good" rows and the last two rows are "bad" rows since they are missing some data.
Here is the snippet of my hive query which is reading this raw data into a readonly external table:
DROP TABLE IF EXISTS readonly_s3;
CREATE EXTERNAL TABLE readonly_s3 (id string, name string, data string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
I need to get the count of ALL the rows, both "good" and "bad." The problem is some of the data is missing, and if I do SELECT count(id) as total_rows for example, that doesn't work since not all the rows have an id.
Any suggestions on how I can count ALL the rows in this raw data file?
Hmmm . . . You can use:
select sum(case when col1 is not null and col2 is not null and col3 is not null then 1 else 0 end) as num_good,
sum(case when col1 is null or col2 is null or col3 is null then 1 else 0 end) as num_bad
from readonly_s3;
I have a table that looks like this
I want to create a query so that same values of column 1 and column 2 (tcbname and status) are grouped and column 3 (scope_name) lists all the scopes related to that status and tcb_name.
Below is my expected outcome
| TUVAmerica, inc | | E | |<all the scope_name values that match first two column>|
It seems to me you need group_concat()
select tcb_name ,status,
group_concat(scope_name separator ',') as list_of_scope
from your_table
group by tcb_name,status
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
I am using oder by name in my sql query.
The following is the name in my table.
rama1
rama2
rama10
rama3
rama11
I am using the query to display the name order by name
the output is coming like,
rama1
rama10
rama11
rama2
rama3
But I need the output should be,
rama1
rama2
rama3
rama10
rama11
Please help me with the query to get the above output.
Thanks In advance
I suppose you have a wrong structure of your table. You should have a separate column, like ID of the numeric datatype, where you could keep your rama numeric part. In this case you would be able to make such queries without developing a bycicle.
In your case you can get numeric part from your string (see How to get the numeric part from a string using T-SQL? for ms sql) and order by it. But this is wrong way to go.
Try this
SELECT col FROM Table1
ORDER BY
CASE WHEN PatIndex('%[0-9]%',col) > 0
THEN RIGHT(col,LEN(col)- (PatIndex('%[0-9]%',col)-1)) * 1
ELSE col END
DEMO
Query:
SELECT t1.*
FROM Table1 t1
ORDER BY CAST(REPLACE(t1.col, 'rama', '') AS UNSIGNED) ASC
Result:
| COL |
----------
| rama1 |
| rama2 |
| rama3 |
| rama10 |
| rama11 |
Please try:
select *
From tbl
order by CAST(SUBSTRING(col, PATINDEX('%[0-9]%', col+'0'), 10) as int)
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