I have a table, sample records are shown below -
Name ID C.NO Text
---- ---- ---- ----
ABC A 1 first
ABC A 2 xyz
ABC A 3 AMD
ZSD B 1 hoho
ZSD B 2 hihi
now my output would be like -------
Name ID Text
---- --- ----
ABC A firstxyzAMD
ZSD B hohohihi
kindly help me providing sql statement
In SAP Hana, you would use string_agg():
select name, id, string_agg(text, '')
from t
group by name, id;
The equivalent function in MySQL is group_concat(); in Oracle, listagg().
MySQL:
SELECT
GROUP_CONCAT(`text`, '' SEPARATOR '') AS `newtext`
FROM [table]
GROUP BY `name`;
Well, following query worked in my table (MySQL) and I got the exact result as per your specification
select
Name,
ID,
group_concat(Text SEPARATOR '')
from table_name
group by ID
Related
I need some help with writing SQL query.
I've data in the following format coming from a query returned by joining multiple tables.
CustID Name AccNo Bank
------ ---- ----- -----
1 Varun 9848032919 CB
1 Varun 9998887771 COB
1 Varun 9988776655 CB
2 Lokesh 9876543210 COB
2 Lokesh 9282726252 CB
3 aaaa 9181716151 COB
I would like the data to be formatted as below so that it would be easy to load into Crystal reports.
CustID Name AccNo Bank
------ ---- ----- -----
1 Varun 9848032919,9998887771,9988776655 CB,COB,CB
2 Lokesh 9876543210,9282726252 COB,CB
3 aaaa 9181716151 COB
I've looked at other answers which suggest using STUFF, XML PATH() but they are applicable only if data comes from one table. In my scenario data is retrieved by joining multiple tables.
May I know how to group by CustID and concatenate data in other columns as I desire?
Thanks a lot for your time!!!
EDIT: I'm looking for a SQL Server 2005 version answer
You can place the query inside a CTE, then apply FOR XML PATH on this CTE like this:
;WITH CTE AS (
... your query here
)
SELECT C.CustID, MAX(Name),
STUFF((
SELECT ', ' + + CAST(AccNo AS VARCHAR(MAX))
FROM CTE
WHERE (CustID = C.CustID)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS AccNo,
STUFF((
SELECT ', ' + + CAST(Bank AS VARCHAR(MAX))
FROM CTE
WHERE (CustID = C.CustID)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS Bank
FROM CTE C
GROUP BY C.CustID
Wrapping the select makes it look like a simple table. You can apply the logic on the result.
Like
Select a.*
from (select t1.col1, t2.col2
from table1 t1
join table2 t2 on t1.f1=t2.c1) a
I have a table with only one column about 100 rows of only names. But I need to display the 3 names in a row. So that I will get 34 rows each row with 3 names.
Example:
Name
_____
Raj
Sam
Guru
Tej
Avin
Sami
Fanst
I need to display above data as
Name Name1 Name2
____ _____ ______
Raj Sam Guru
Tej Avin Sami
Fanst
No condition just need to covert single column value into 3 columns data.
Oracle DB
You can do this using conditional aggregation and rownum. Something like this:
select max(case when mod(rn, 3) = 1 then name end) as name1,
max(case when mod(rn, 3) = 2 then name end) as name2,
max(case when mod(rn, 3) = 0 then name end) as name3
from (select name, rownum as rn
from table t
) t
group by trunc((rn - 1) / 3);
You can do it using CASE, make a try on it using PIVOT
Try to PIVOT Your Name column like this
SELECT Name,Name1,Name2
FROM
(SELECT Name FROM table_name) AS SourceTable
PIVOT
(
FOR Name IN (Name,Name1,Name2)
) AS PivotTable;
I have the following query to get a specific Area:
Select
Id, Name
from
Area
Where
(Id = 1)
Each Area can have 2 files in AreaFiles table, so I changed the query to:
SELECT
Area.Id, Area.Name, AreaFiles.FileName
FROM
AreaFiles
INNER JOIN
Area ON AreaFiles.AreaId = Area.Id
WHERE
(Area.Id = 1)
The result is:
Id | Name | FileName
--- ------ ----------
1 abc file1.jpg
1 abc file2.jpg
I want to merge these rows into one row to get this:
Id | Name | FileName1 | FileName2
--- ------ ------------------------
1 abc file1.jpg file1.jpg
Is it possible?
To achieve this, you will need to use PIVOT
See here for a good example - http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query
So for your eample here, it would be
SELECT ID,NAME, [1] AS FILENAME1, [2] AS FILENAME2 FROM
(SELECT ID, FILENAME, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS ROWNUM) FROM AREA A
PIVOT ((MAX(ID) FOR ROWNUM IN [1],[2])) AS PVT
for that you need to use with clause
with first as (select id,name,filename from area),
second as (select id,name,filename from area)
SELECT first.id,first.name,first.filename,second.filename
FROM first,second
where first.id=second.id;
I am trying to concatenate the select statment results in one row.
For eg :
For this select statement output :
Name
ABC
DEF
GHI
I needed following Output :
Name
1 ABC, 2 DEF, 3 GHI
(Means row number should be appended in front for each row)
I am using wm_concat() function but it is giving me the following o/p : ABC,DEF,GHI
Can anyone help ?
It sounds like you want something similar to this. The listagg() function is available in Oracle 11g+:
select listagg(cast(rownum as varchar2(20))||' '|| name, ', ')
within group (order by name) name
from yourtable
See SQL Fiddle with Demo
Results:
| NAME |
-----------------------
| 1 ABC, 2 DEF, 3 GHI |
If you do not have Oracle 11g, then you can use wm_concat():
select wm_concat(cast(rownum as varchar2(20))||' '|| name) name
from yourtable
Additionaly you could use WITH clause:
WITH tmp AS
(
:your_select_statement
)
SELECT
LISTAGG(rownum || ' ' || :your_column, ', ')
WITHIN GROUP (ORDER BY :your_column) tmp
FROM tmp;
Then rownum is from tmp temporary table which have results of :your_select_statement not from primary table.
i have a table where termid,termversion are two primary key.
column present in table - termid,termversion,name
I want to select all term whose name ilike '%AB%' and the result should contain each matching term with maximum version number.
Example:
id name ver
1 ABBBB 1
1 ABBBB 2
1 ABBBB 3
2 ABC 1
2 ABC 2
output should come
1 ABBBB 3
2 ABC 2
I want to write this query in hibernate using
Criteria...if any one can suggest me in hibernate then its really good else at least help me in writing the sql query.
PS I am using postgresql
I think that is what you are looking for:
SELECT id, name, MAX(ver)
FROM table
WHERE name ILIKE '%AB%'
GROUP BY name, id -- I assume: id == id' <==> name == name'
is it?
select id, name, max(ver) from mytable where name like '%AB%' group by id, name
can you try this sql command.
select id,name,max(ver) from table_name group by id, name having name like '%AB%'