Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Column Data Consolidation Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 3:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 9:08 AM
Points: 6, Visits: 6
Can someone help I have a peice of code that that i am trying to use to oull som information into a report. Unfortunaltey due to nature of the database when certain feild are selected within the app i produces multiple rows of data in the report. I am trying to merge the column TITLE by the GROUP BY of REF_NO (The title field could have any number of references in it but for this example it brings through two. Also there will be more than 1 REF_NO to deal with to.)

Here is the example code.

SELECT dbo.SU_EXTENSION_DATA.REF_NO, dbo.AR_PRIMARY_ASSET.TITLE
FROM dbo.SU_EXTENSION_DATA INNER JOIN
dbo.AR_PRIMARY_ASSET ON dbo.SU_EXTENSION_DATA.VALUE_REF = dbo.AR_PRIMARY_ASSET.ITEM_REF
WHERE (dbo.SU_EXTENSION_DATA.EXTENSION_FIELD_REF = 501192) AND (dbo.SU_EXTENSION_DATA.CORE_ENTITY = 2) AND
(dbo.SU_EXTENSION_DATA.REF_NO = 86503)


And here are the results

REF_NO TITLE
86503 VSM
86503 AD Access

I would like it to show

REF_NO TITLE
86503 VSM, AD ACCESS

Can anyone help?

Post #1384511
Posted Wednesday, November 14, 2012 3:50 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 2:53 AM
Points: 111, Visits: 517
have some sample data here
Post #1384515
Posted Wednesday, November 14, 2012 3:51 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556, Visits: 4,398
Google the following:

Concatenate FOR XML PATH


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1384516
Posted Wednesday, November 14, 2012 3:52 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:11 AM
Points: 9,378, Visits: 6,473
This article explains various methods to concatenate row values:
Concatenating Row Values in Transact-SQL




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1384517
Posted Wednesday, November 14, 2012 4:53 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 9:08 AM
Points: 6, Visits: 6
I have tried 2 different ways now and each data row comes out with the exact same data in. It seems to be picking up the all the 'TITLE' and placing all of them in the new 'Products' column.

Here is the most recent code i changed and used.

SELECT REF_NO,

stuff( (SELECT ','+TITLE

FROM bgc_MERGE

WHERE ref_no = REF_NO

ORDER BY REF_NO

FOR XML PATH(''), TYPE).value('.', 'varchar(max)')

,1,1,'')

AS Products

FROM bgc_MERGE

GROUP BY ref_NO ;


There are hundreds of REF_NO that this needs to work on when this query runs but every row for every distinct ref_no comes out the same.

HELP :)
Post #1384542
Posted Wednesday, November 14, 2012 4:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 9:08 AM
Points: 6, Visits: 6
also used this and it produces the same.

SELECT DISTINCT REF_NO,
(SELECT TITLE + ','
FROM bgc_MERGE
WHERE REF_NO = REF_NO
ORDER BY TITLE FOR XML PATH('')) AS HARDWARE
FROM bgc_MERGE
GROUP BY REF_NO;
Post #1384544
Posted Wednesday, November 14, 2012 5:11 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 2:11 AM
Points: 9,378, Visits: 6,473
pmercer (11/14/2012)
I have tried 2 different ways now and each data row comes out with the exact same data in. It seems to be picking up the all the 'TITLE' and placing all of them in the new 'Products' column.

Here is the most recent code i changed and used.

SELECT REF_NO,

stuff( (SELECT ','+TITLE

FROM bgc_MERGE

WHERE ref_no = REF_NO

ORDER BY REF_NO

FOR XML PATH(''), TYPE).value('.', 'varchar(max)')

,1,1,'')

AS Products

FROM bgc_MERGE

GROUP BY ref_NO ;


There are hundreds of REF_NO that this needs to work on when this query runs but every row for every distinct ref_no comes out the same.

HELP :)


Post DDL and sample data + desired output. (as outlined in the first link in my signature)
Without that it will be too difficult to guess what you actually want.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1384549
Posted Wednesday, November 14, 2012 5:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 9:08 AM
Points: 6, Visits: 6
Not sure what DDL info you need

Here is some sample Data

REF_NO TITLE
25995 bsdinfg0503.eu.ad.espeed.com
25995 ESSHQSM01.eu.ad.espeed.com
25995 ukqsmweb01.eu.ad.espeed.com
26237 ukesp024.eu.ad.espeed.com
26237 ukqsmweb01.eu.ad.espeed.com
26258 ukqsmweb01.eu.ad.espeed.com
26267 esshcddc03.eu.ad.espeed.com
26267 ukqsmweb01.eu.ad.espeed.com
26872 ukesp024.eu.ad.espeed.com
45287 ukesp024.eu.ad.espeed.com

I would like it to read.

REF_NO TITLE
25995 bsdinfg0503.eu.ad.espeed.com, ESSHQSM01.eu.ad.espeed.com, ukqsmweb01.eu.ad.espeed.com
26237 ukesp024.eu.ad.espeed.com, ukqsmweb01.eu.ad.espeed.com
26258 ukqsmweb01.eu.ad.espeed.com
26267 esshcddc03.eu.ad.espeed.com, ukqsmweb01.eu.ad.espeed.com
26872 ukesp024.eu.ad.espeed.com
45287 ukesp024.eu.ad.espeed.com


Not sure if this helps. The code in my previous replies has applied to this.

Apologies for my lack of information i am new to SQL and to this forum.
Post #1384558
Posted Wednesday, November 14, 2012 5:31 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556, Visits: 4,398

...
Apologies for my lack of information i am new to SQL and to this forum.


The following will get you started receiving relevant and fast responses on this forum:

http://www.sqlservercentral.com/articles/Best+Practices/61537/


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1384561
Posted Wednesday, November 14, 2012 8:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 14, 2012 9:08 AM
Points: 6, Visits: 6
So created this test table for people to help me on.

Here is the code.

CREATE TABLE dbo.TEST_TABLE2(
[REF_NO] [int] NULL,
[TITLE] [varchar](max) NULL
) ON [PRIMARY]

INSERT INTO dbo.TEST_TABLE2
Values (25995, 'Outlook') ;
INSERT INTO dbo.TEST_TABLE2
Values (25995, 'VSM') ;
INSERT INTO dbo.TEST_TABLE2
Values (25995, 'PC') ;
INSERT INTO dbo.TEST_TABLE2
Values (26237, 'Word') ;
INSERT INTO dbo.TEST_TABLE2
Values (26237, 'ukqsmweb01') ;
INSERT INTO dbo.TEST_TABLE2
Values (26258, 'Driver');
INSERT INTO dbo.TEST_TABLE2
Values (26267, 'Chrome');
INSERT INTO dbo.TEST_TABLE2
Values (26267, 'IE');
INSERT INTO dbo.TEST_TABLE2
Values (26872,'Firefox' );
INSERT INTO dbo.TEST_TABLE2
Values (45287, 'VSM');


I the created a view based on this code.

SELECT     REF_NO, stuff
((SELECT ',' + TITLE
FROM dbo.TEST_TABLE2
WHERE REF_NO = REF_NO
ORDER BY TITLE FOR XML PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '') AS HARDWARE
FROM dbo.TEST_TABLE2
GROUP BY REF_NO;

And it brings these results.

REF_NO HARDWARE
25995 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word
26237 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word
26258 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word
26267 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word
26872 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word
45287 Chrome,Driver,Firefox,IE,Outlook,PC,ukqsmweb01,VSM,VSM,Word

when i would like it to look like this

REF_NO TITLE
25995 Outlook, VSM, PC
26237 Word, ukqsmweb01
26258 Driver
26267 Chrome, IE
26872 Firefox
45287 VSM


please be aware the REF_NO refers to Incident numbers therefore the amount of them will increase over time.

If there is anything else anyone needs let me know. Thanks in advance

Post #1384668
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse