|
|
|
Forum 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?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 2:53 AM
Points: 111,
Visits: 517
|
|
| have some sample data here
|
|
|
|
|
SSCrazy
      
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
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 2:11 AM
Points: 9,378,
Visits: 6,473
|
|
|
|
|
|
Forum 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 :)
|
|
|
|
|
Forum 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;
|
|
|
|
|
SSCrazy 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
|
|
|
|
|
Forum 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.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556,
Visits: 4,398
|
|
|
|
|
|
Forum 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
|
|
|
|