Postgres - get count of a chacter in a column? [duplicate] - sql

This question already has answers here:
Counting chars in sequences via SQL
(2 answers)
Closed 7 years ago.
In Postgres/SQL, how can I get the count of a character in a column?
For example, I want to run a query which will return the number of times "X" appears in a column that has the value "XXX" - and it should return 3.

One method is the difference of lengths:
select (length(col) - length(replace(col, 'X', ''))) as NumX

Related

OTBI function to get the maximum sequence number for a person [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
SQL: getting the max value of one column and the corresponding other columns [duplicate]
(2 answers)
Closed 7 months ago.
I have a column that gives sequence number and another for phase name for a person number
PERSON_NUMBER
SEQUENCE
Phase Name
10
1
Initial
10
2
Mid
10
3
Final review
11
1
Initial
11
2
Mid
I want to get the person number and phase against max sequence phase for the employee -
PERSON_NUMBER
SEQUENCE
Phase Name
10
3
Final review
11
2
Mid
Which function can I use in OTBI for this ? I tried using
"max("Job Application - Progression"."Current Phase Sequence Number") keep(dense_rank first order by "Job Application - Progression"."Current Phase Name" desc)"
But this is not the correct syntax for OTBI

Concatenate multiple rows to one row [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
SQL Server: Combine multiple rows into one row from a join table?
(1 answer)
ListAGG in SQLSERVER
(4 answers)
how to stuff date and time in SQL
(2 answers)
Query to get multiple row into single row
(3 answers)
Closed 3 years ago.
I have the below data and I have to concatenate the long text column and make it as a single row. The challenge part is only one row has the notification number and other rows are null. You cannot group by the notification number.
I need the output as 2 rows
row number Notification Number Plant Creation Date Language Lineno Tag Long Text
1 10014354914 A057 43466 EN 1 >X aaabbbcccdddeeefffggghhhjjjkkklll
2 10014354915 A057 43466 EN 1 >X aaabbbcccdddeeefffgggpppqqqrrrsss
I have used cursor for this. But it is taking much time.
If you are using oracle:
with data("row number", "Notification Number","Plant","Creation Date","Language","Lineno","Tag","Long Text") as (
select 1,10014354914,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffggghhhjjjkkklll' from dual
union all
select 2,10014354915,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffgggpppqqqrrrsss' from dual)
select LISTAGG("Long Text",'') within group (order by "row number") from data;
if you are using ms-sql maybe try this:
SELECT u.[Long Text] AS [text()]
FROM yourtable u
ORDER BY u.[row number]
FOR XML PATH ('')

Concatenate values based on numeric value of another column [duplicate]

This question already has answers here:
get a comma delimited string from rows [duplicate]
(2 answers)
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 3 years ago.
I'm trying to concatenate multiple rows of data into one row based off of another column's ordered value
Here's the data that I'm working with:
Here's the data picture
Here's the desired output:
37960 BRAKES GENERAL REPAIRS ADDITIONAL REPAIRS
I'm working in SSMS V14.0 Thanks!

Division Operator [duplicate]

This question already has answers here:
SQL Server round after division
(2 answers)
Closed 5 years ago.
Is there any way to return 3 as a result of ((5168/2000) .
I'm using CEILING(5168/2000) to delete rows. Its returning 2. But I want 3.
So that loop execute for 3 times.
Is there any way to return 3 as a result of (5168/2000)
Yes, make sure you're diving floating point numbers not integers
CEILING(5168.0/2000)
One way to achive that, aside from hardcoding the .0 as I have above, is to cast your integer to an appropriate type, for example:
CEILING(CAST(5168 AS FLOAT)/2000)
or muultiply it by a decimal
CEILING((1.0 * 5168)/2000)

sql max(val1,val2)? [duplicate]

This question already has answers here:
How to get min/max of two integers in Postgres/SQL?
(2 answers)
Closed 6 years ago.
is there some way to determine the max value out of two values in SQL?
I use the mod function:
MOD(cnt, cnt/100)
This yields a division by 0 error when cnt is smaller than 100. I therefore would like something like this:
MOD(cnt, MAX(cnt/100, 1))
You can use greatest
SELECT greatest(a, b, c) FROM your_table;