Combine multiple rows into single row in an CSV file in SSIS - ssis

I load data from SQL to CSV file. And my csv records are in the following way.
Eid ename desig sourceD location
1 Ralf PM 1 UK
1 Ralf PM 1 USA
2 Marty PL 3 GBR
3 Levis BA 5 UK
4 Ricky BI 7 RSA
4 Ricky BI 7 FRA
now i want to combine EID 1 and 4 into single rows. My output has to be in the following way
Eid ename desig sourceD location
1 Ralf PM 1 UK, USA
2 Marty PL 3 GBR
3 Levis BA 5 UK
4 Ricky BI 7 RSA, FRA
I have only CSV File i dont have any sql Query or anything.

SQL Fiddle
SQL Query
select c1.Eid, c1.ename,c1.desig,c1.sourceD,
( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
) as Locations
from Contacts c1
group by c1.Eid, c1.ename,c1.desig,c1.sourceD
Working ?
If you want without comma in the end:
SELECT c1.Eid, c1.ename,c1.desig,c1.sourceD,
LEFT( ( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
),LEN(( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
))-1) AS Locations
FROM Contacts c1
GROUP BY c1.Eid, c1.ename,c1.desig,c1.sourceD
This Query will let you avoid lenght 0:
SELECT c1.Eid, c1.ename,c1.desig,c1.sourceD,
LEFT( ( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
),CASE WHEN LEN(( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
))=0 THEN 1 ELSE LEN(( SELECT x.location + ', '
FROM Contacts x
WHERE c1.Eid = x.Eid
ORDER BY x.location
FOR XML PATH ('')
)) END -1) AS Locations
FROM Contacts c1
GROUP BY c1.Eid, c1.ename,c1.desig,c1.sourceD

Related

SQL Server : combine multiple records into one field

I have created the following SQL script:
SELECT
E.ID,
abc = STUFF((SELECT ' ' + E2.ExclusionID
FROM Exclusion E2
WHERE E.ID = E2.ID
FOR XML PATH ('')), 1, 0, '')
FROM
Exclusion E
GROUP BY
E.ContractMovementID
It combines all exclusion records for the ID into one record and displays as below:
ID ExclusionID
-------------------
1 123
2 2345
3 4567
However, I would like it to display as:
ID ExclusionID
-------------------
1 1 2 3
2 23 45
3 45 67
So that the columns are separated with space, however, I cannot seem to get it to put a space between them. Can anyone help me with this? (I hope this makes sense)
Thanks
Jessica
How about this:
SELECT
E.ID,
abc = STUFF((SELECT ' ' + E2.ExclusionID
FROM Exclusion E2
WHERE E.ID = E2.ID
FOR XML PATH ('')), 1, 1, '')
FROM
Exclusion E
GROUP BY
E.ContractMovementID
I have found a solution, because the exclusion.id was an INT data type i had to convert it into a varchar to be able to add a seperator.
SELECT
E.ID,
abc = STUFF((SELECT CAST(E2.ExclusionID AS VARCHAR) + ','
FROM Exclusion E2
WHERE E.ID = E2.ID
FOR XML PATH ('')), 1, 0, '') FROM
Exclusion E GROUP BY
E.ContractMovementID

Retrieve Distinct concat values from MySQL table

I have an SQL table advert
id name cat
11 abc ab
12 acb ab, bc
13 abb bcd
14 abcd ad
15 acbd de
16 abbd ad
On using DISTINCT function I am getting an output like this
Query:
SELECT DISTINCT cat FROM advert;
Output:
ab
ab, bc
bcd
ad
de
WHAT changes do I need to make in my query for output like this
ab
bc
bcd
ad
de
select distinct trim(substring_index(substring_index(cat,',',n),',',-1)) as cat
from t join (select 1 as n union all select 2 union all select 3) r
on cat like concat('%',repeat(',%',n-1))
I think you should change your table structure and make it like this.
tblName
id | name
11 abc
12 acb
13 abb
14 abcd
15 acbd
16 abbd
tblCat
id | name_id | cat
some ids* 11 ab
12 ab
12 bc
13 bcd
14 ad
15 de
16 ad
In this way you can easily query and manage your data in your tables.
You should fix your data structure so you are not storing comma-delimited lists in columns. That is the wrong way to store data in a relational database . . . as you can see by the problems for answering this simple question. What you want is a junction table.
Sometimes, we are stuck with other peoples bad designs. You say that there are only two or values, then you can do:
select cat
from ((select substring_index(cat, ', ', 1) as cat
from advert
) union all
(select substring_index(substring_index(cat, ', ', 2), ', ', -1) as cat
from advert
where cat like '%, %'
) union all
(select substring_index(substring_index(cat, ', ', 3), ', ', -1) as cat
from advert
where cat like '%, %, %'
)
) c
group by cat;
First... I would create a statement that would turn all the rows into one big massive comma delimited list.
DECLARE #tmp VarChar(max)
SET #tmp = ''
SELECT #tmp = #tmp + ColumnA + ',' FROM TableA
Then use the table valued udf split described by this SO article to turn that massive string back into a table with a distinct clause to ensure that it's unique.
https://stackoverflow.com/a/2837662/261997
SELECT DISTINCT * FROM dbo.Split(',', #tmp)
Full code example:
if object_id('dbo.Split') is not null
drop function dbo.Split
go
CREATE FUNCTION dbo.Split (#sep char(1), #s varchar(512))
RETURNS table
AS
RETURN (
WITH Pieces(pn, start, stop) AS (
SELECT 1, 1, CHARINDEX(#sep, #s)
UNION ALL
SELECT pn + 1, stop + 1, CHARINDEX(#sep, #s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT pn,
SUBSTRING(#s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces
)
go
declare #t table (colA varchar(max))
insert #t select '111, 223'
union all select '333'
union all select '444'
union all select '777,999';
select ltrim(rtrim(s.s)) as colC
from #t t
cross apply
dbo.split(',', t.colA) s

How to Remove the duplicates in SQL Table and concat other part [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 7 years ago.
Let's Assume I have DataTable /SQL Table(MicroSoft SQL) that represents employee information for example
which contains firstname,lastname,age,company,yearsofexperience,Degree
I want to combine information based on firstname,lastname,age
company,yearsofexperience,Degree must be concat into corresponding cell
firstname lastname age company yearsofexperience Degree
john muller 21 IBM 4years MBA
jan tonny 22, MSoft 1years MS
martin tata 21 apple 2years PHD
john Muller 21 sony 3years MBA
james muller 21 IBM 4years PHD
jan tonny 22 Telsa 1years BS
martin tata 21 sun 2years MBA
james Muller 21 TCS 3years BS
Note:MS SQL solution not Mysql
Please find me way to remove the duplicate rows and make other data concat in particular column
For example in from above example I want combine the information present in the similar other 3 entries
firstname lastname age company yearsofexperience Degree
john muller 21 IBM,sony, 4years,3years, MBA,MBA
jan tonny 22, MSoft,Telsa 1years,1years MS,BS
martin tata 21 apple,sun 2years,2years PHD,MBA
james muller 21 IBM,TCS 4years,3years PHD,BS
Right I am looking for what is best ways to implement this
Its good approach if I Split the Tables in to 2 different tables? may be based on Primary key match. we can con cat other entries ?
Please help me out thanks(+1) in advance
If you are using SQL Server then try this:
SELECT T1.firstname
,t1.lastname
,t1.age
,Company = SubString (( SELECT ', ' + T2.company
FROM table_name as T2
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age
FOR XML PATH ( '' ) ), 3, 1000)
,yearsofexperience = SubString (( SELECT ', ' + T2.yearsofexperience
FROM table_name as T2
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age
FOR XML PATH ( '' ) ), 3, 1000)
,yearsofexperience = SubString (( SELECT ', ' + T2.Degree
FROM table_name as T2
WHERE T1.firstname = T2.firstname
and t1.lastname = t2.lastname
and t1.age = t2.age
FOR XML PATH ( '' ) ), 3, 1000)
FROM table_name as T1
GROUP BY T1.firstname, t1.lastname ,t1.age
Reference: Create A Comma Delimited List From a Column in SQL Server
You can try group by and group_concat of SQL
SELECT *,group_concat(company) as company,
group_concat(yearsofexperience) as yearsofexperience,
group_concat(Degree) as Degree
from table group by firstname
group by lastname
group by age
for more follow the link
Merge data in two row into one

SQL PIVOT QUERY ISSUE

I have a table [dbo].[RISK_DISCPLN]
RISK_DISCPLN_ID RISK_DISCPLN_NM
1 LegalRisk
2 institutional Risk
4 Market Risk
Tabbe Name [dbo].[PROJ_RISK_DISCPLN]
PROJ_ID RISK_DISCPLN_ID
1 1
1 2
1 4
2 1
2 2
2 4
3 1
3 2
3 4
Table Name [dbo].[USER]
USER_ID USER_FIRST_NM USER_LST_NM
2 saravanakumar rajkumar
3 Soosai Antony
4 Adam Allen
5 Babita Tripathy
9 stacey Davis
11 NULL NULL
I tried below query to display names into the corresponding risk, i am getting all the names as null value... Help needed please
;with cte as
(
SELECT [PROJ_RISK_DISCPLN].PROJ_ID,
[USER].USER_LST_NM + ' '+ [USER].USER_FIRST_NM as Name,
[RISK_DISCPLN].RISK_DISCPLN_NM,
[RISK_DISCPLN].RISK_DISCPLN_ID,
[USER].[USER_ID]
FROM dbo.[PROJ_RISK_DISCPLN]
left join [dbo].[USER]
on [PROJ_RISK_DISCPLN].RISK_OWN_USER_ID = [dbo].[USER].[USER_ID]
left join dbo.[RISK_DISCPLN] on
[PROJ_RISK_DISCPLN].RISK_DISCPLN_ID = [RISK_DISCPLN].RISK_DISCPLN_ID
)
select *
from
(
select c1.PROJ_ID,
c1.RISK_DISCPLN_NM,
STUFF(
(SELECT ', ' + c2.Name
FROM cte c2
where c1.PROJ_ID = c2.PROJ_ID
and c1.RISK_DISCPLN_ID = c2.RISK_DISCPLN_ID
FOR XML PATH (''))
, 1, 1, '') AS Name
from cte c1
) d
pivot
(
max(Name)
for RISK_DISCPLN_NM in ([LegalRisk Owner],[institutional Risk Owner],[Market Risk Owner])
) piv
I got answer, Instead [LegalRisk Owner],[institutional Risk Owner],[Market Risk Owner]
i have to use [LegalRisk],[institutional Risk],[Market Risk] ....

MySQL - Complex Query w/ GROUP_CONCAT... Single Table

I have the following table:
id | billingno | location
-------------------------
1 | 9999999 | Toronto
2 | 9999999 | Toronto
3 | 7777777 | Toronto
4 | 7777777 | Quebec
I need a query that would generate me something that looked like this:
location | total | display
--------------------------
Toronto | 3 | 9999999 - 2, 7777777 - 1
Quebec | 1 | 7777777 - 1
So, it groups by location, displays the total number of billingno's for that location, and then the display column lists each billingno and how many times they were in that location. I have been trying to write this for some time, my closest attempt is this:
SELECT location, COUNT(*) AS total, GROUP_CONCAT(DISTINCT CAST(CONCAT(CONVERT(billingno,CHAR(16)), ' - ', THIS_COUNT_PART_FOR_EACH_LOCATION_IN_DISPLAY_DOESNT_WORK)AS CHAR)
SEPARATOR ' - ') AS display
FROM table GROUP BY location
ORDER BY COUNT(*) DESC
It gives me everything I need except I cannot for the life of me figure out how to count the number of each billingno's under display. If I use COUNT() it gives me an error about grouping. Please help!
Oh, I also had to use the convert to char so it would show up as text and not a BLOB in phpMyAdmin. Thanks again!
Sample data:
create table location (
id int,
billingno varchar(10),
location varchar(10)
);
insert into location
select 1, '9999999', 'Toronto' union
select 2, '9999999', 'Toronto' union
select 3, '7777777', 'Toronto' union
select 4, '7777777', 'Quebec' ;
Query:
select
location,
sum(qty) as total,
group_concat(concat(billingno, ' - ', cast(qty as char(7)))
order by qty desc separator ', '
) as display
from (
select billingno, location, count(*) as qty
from location
group by billingno, location
) t
group by location
order by total desc
Result:
location total display
Toronto 3 9999999 - 2, 7777777 - 1
Quebec 1 7777777 - 1
How about this,
SELECT table.location,
SUM(LocationCount) AS Total,
GROUP_CONCAT(CAST(CONCAT(CONVERT(billingno,CHAR(16)), ' - ', THIS_COUNT_PART_FOR_EACH_LOCATION_IN_DISPLAY_DOESNT_WORK)AS CHAR)
SEPARATOR ' - ') AS display
FROM table
LEFT JOIN
(SELECT location , COUNT(id) AS LocationCount
FROM table
GROUP BY location) t on t.location = table.location
GROUP BY location
ORDER BY SUM(LocationCount) DESC
SELECT location, SUM( total ) AS total, GROUP_CONCAT( CONCAT( billingno, ' - ', billing_count ) ) AS display
FROM (
SELECT location, COUNT( billingno ) AS total, billingno, COUNT( billingno ) AS billing_count
FROM billing
GROUP BY location, billingno
ORDER BY COUNT( * ) DESC
) AS t
GROUP BY location