Concatinate top 5 results in one column - sql

I have a table that gives me the following output.
I want to write a query that gives me the result of the top 5 rows fetched from the above table (just col2) and then display them in just one column as comma separated.
Basically what I want the output as:
Can you give me the sql query for the same.

If your DB is MySQL, use:
select group_concat(col2 separator ', ') as str from myTable;
If your DB is Oracle, use:
select listagg(col2, ', ') within group (order by col1) as str from myTable;
If your DB is Postgres, use:
select string_agg(col2, ',' order by col1) as str from mytable;

Related

concatenate column values from multiple rows in Oracle without duplicates

I can concatenate column values from multiple rows in Oracle using LISTAGG
But I want to avoid duplicates
Currently it return duplicates
select LISTAGG( t.id,',') WITHIN GROUP (ORDER BY t.id) from table t;
for example for data
ID
10
10
20
30
30
40
Returns 10,10,20,30,40,40
Instead 10,20,30,40
And I can't use distinct inside LISTAGG
select LISTAGG( distinct t.id,',') WITHIN GROUP (ORDER BY t.id) from table t;
Error
ORA-30482: DISTINCT option not allowed for this function
One option would be using regexp_replace():
select regexp_replace(
listagg( t.id,',') within group (order by t.id)
, '([^,]+)(,\1)+', '\1') as "Result"
from t
Demo
You can put the distinct in a subquery:
select LISTAGG( t.id,',') WITHIN GROUP (ORDER BY t.id) from (SELECT DISTINCT t.id FROM TABLE) t

SQL query to select column1 value multiple times based on column 2 value

How can I write a query for below scenario in Oracle?
Input
Column1 Column2
R 1
S 2
F 3
Output
RSSFFF
i.e. 'R' 1 time, 'S' 2 time and 'F' 3 times.
You can use rpad to repeat the letters, and listagg to concatenate them in a single line, like so.
select
listagg(rpad(column1,column2,column1)) within group (order by column2)
from table_name;
But this would work if you have another column to order the rows, in this case, I just use the column2.
One method uses lpad():
select lpad(column1, column2, column1)
from t;
If you want a single string, then use listagg():
select listagg(lpad(column1, column2, column1)) within group (order by null)
from t;

How to find duplicates in a SQL Server table which has trailing spaces values in a column

select COL1, count(COL1)
from Table1
group by COL1
having count (COL1) > 1;
I have tried the above query and got some result based on data which do not have trailing spaces however the above query does not apply to data which has trailing spaces so I tried the below query and got no results. Please advice
select COL1, count(COL1)
from Table1
where COL1 in(select Ltrim(Rtrim(COL1))from Table1)
group by COL1
having count (COL1) > 1;
If you want to tally the text contents of COL1 ignoring leading and trailing whitespace, then just do that. Use ltrim(rtrim(COL1)) when aggregating:
select
ltrim(rtrim(COL1)) AS COL1_trimmed
count(*) cnt
from Table1
group by ltrim(rtrim(COL1))
having count(*) > 1;
In general, SQL Server ignores trailing spaces with varchar(). However, it does not when using char(). I am guessing the trailing "spaces" are not really spaces.
Here is an example.
with t as (
select cast('a' as varchar(255)) as x union all
select cast('a ' as varchar(255))
)
select t.x, count(*), min(t.x + '|') , max(t.x + '|')
from t
group by t.x;
This returns:
a 2 "a |" "a|"
(I added the double quotes to clarify the results.) Note that one row is returned, not two. But the spaces really are at the end of the values.
This leads me to suspect that the trailing characters are not spaces.
One way to investigate what they are is by using the ASCII() function.
Another way is to first remove the trailing and leading spaces from that column in your table.
If COL1 is a VARCHAR type:
update Table1
set COL1 = rtrim(ltrim(COL1))
where COL1 != rtrim(ltrim(COL1));
If COL1 is a CHAR type then you only need to left trim:
update Table1
set COL1 = ltrim(COL1)
where COL1 != ltrim(COL1);
After that cleanup, you can just use a grouping query without trimming the column
select COL1, count(*) as Total
from Table1
group by COL1
having count(*) > 1;

query to fetch the number of rows fetched by another query

How do I fetch the number of rows fetched by another query in SQL server:
The required value should be:
select count(*) of select * from table
Simply try
SELECT count(*) FROM
(
select * from yourtable
) AS A
Simply Try this Query for the number of rows fetched by another query in SQL server
select temp.TblCount From
(select Count(*) As TblCount from YOURTABLE) As Temp
Why the nested query? If all you need is the count of rows fetched by a query, better replace everything in the SELECT clause of that query with 'SELECT COUNT(*)' thus saving on using nested queries
Query 1:
select COUNT(*) from
(
select col1, col2, col3
From Table1
WHERE <Conditions>
)as x
Query 2:
select COUNT(*)
From Table1
WHERE <Conditions>
Both the Queries should be giving the same Output.

oracle pl/sql results into one string

I'm trying to create a simple stored procedure that stores queried result into one string.
v_string1 varchar2(100);
Select column1
From dual;
Will return
column 1
--------
aaaa
bbbb
cccc
I want to store "aaaa, bbbb, cccc' into v_string1.
And all I can think of is a Cursor...
Is there a better way to handle this?
Using SQL Fiddle:
select LISTAGG(name, ',') WITHIN GROUP (ORDER BY 1) AS names
from temp_table
Another option using pure SQL that will work before Oracle 11G, although is still limited to 4000 characters for the string.
Select ltrim(max(names), ', ') as names
From (
Select sys_connect_by_path(name, ' ,') as names
From (
Select name, row_number() over (order by name) as rown
From temp_table
)
Start with rown = 1
Connect by rown = prior rown + 1
)