A Column Stuff() Alternative SubQuery

  • I used Stuff() to combine the rows for ModifyName after unpivoting Charity & Super and placing them in Curr's column, but the result of the currNames didn't comply with Cognos's rules regarding blobs. I did all I could  to configure Report Studio with no FM access to get the query to return data. I did, but I couldn't filter it. So I'm trying to replace the xml path() portion of the query to produce: currType and ModifyName is concatenated with a hyphen if ModifyName is populated

    ComicsCodeComicsNameissuenumslipnum    Date          currTypecurrkTypecurrrNameS
    2055           marvelDC9999 DC99801   1/24/1901Modify-55HelpModify - Modify-Sky
    2055           marvelDC9999 DC99801   1/24/1901Modify-3CharityModify; Modify-Sky
    2055           marvelDC9999 DC99801   1/24/1901Modify-2CharityModify; Modify-Sky
    2055           marvelDC9999 DC99801   1/24/1901Modify0SuperModify; Modify-Sky

    from this:
    d:

    ComicsCodeComicsNameissuenumslipnum    Date          currType       curr charityModifyNameSuper
    2055           marvelDC9999 DC99801   1/24/1901Modify-55-2   Modify-Sky0
    2055           marvelDC9999 DC99801   1/24/1901Modify0-3  

    select d.[currType]
          ,STUFF((select '; ' + cast(d.currName as nvarchar(2000))
                  from [foo].[dbo].[zool] d
                  where f.[issuenum] = d.[issuenum]
                  and    f.[slipnum] = d.[slipnum]
                  and f.[comicsName] = d.[comicsName]
                  FOR XML PATH(''), TYPE).value(';','NVARCHAR(2000)'),1,1,' ') [currNames]
                  --for XML Path('')), 1,1, '') [currNames]

    ---^^^^^^^^^^replace

      FROM [foo].[dbo].tbl] f
      LEFT outer join [foo].[dbo].[Zool] d
       on f.[billNo] = d.[billNo]

    Msg 2209, Level 16, State 1, Line 50
    ----XQuery [value()]: Syntax error near ';' tried casting

    ---  Using a blob column in this query requires that the query subject [Q1].[SQL2] must have either a key or a unique index.

  • Simple syntax error in the value function
    😎

    This should work

    SELECT
      d.[currType]
     ,STUFF(
        (SELECT
          '; ' + d.currName
         from [foo].[dbo].[zool] d
         where f.[issuenum] = d.[issuenum]
         and f.[slipnum] = d.[slipnum]
         and f.[comicsName] = d.[comicsName]
         FOR XML PATH(''), TYPE).value('(./text())[1]','NVARCHAR(2000)'),1,1,'') AS [currNames]
    FROM [foo].[dbo].tbl] f
    LEFT outer join [foo].[dbo].[Zool] d
    on f.[billNo] = d.[billNo];

  • Eirikur Eiriksson - Wednesday, May 23, 2018 12:42 AM

    Simple syntax error in the value function
    😎

    This should work

    SELECT
      d.[currType]
     ,STUFF(
        (SELECT
          '; ' + d.currName
         from [foo].[dbo].[zool] d
         where f.[issuenum] = d.[issuenum]
         and f.[slipnum] = d.[slipnum]
         and f.[comicsName] = d.[comicsName]
         FOR XML PATH(''), TYPE).value('(./text())[1]','NVARCHAR(2000)'),1,1,'') AS [currNames]
    FROM [foo].[dbo].tbl] f
    LEFT outer join [foo].[dbo].[Zool] d
    on f.[billNo] = d.[billNo];

    Thank you, Is there a correlated query or self join approach that could be used to keep the data type of the colmn from becoming a blob?

  • ackrite55 - Wednesday, May 23, 2018 6:53 AM

    Eirikur Eiriksson - Wednesday, May 23, 2018 12:42 AM

    Simple syntax error in the value function
    😎

    This should work

    SELECT
      d.[currType]
     ,STUFF(
        (SELECT
          '; ' + d.currName
         from [foo].[dbo].[zool] d
         where f.[issuenum] = d.[issuenum]
         and f.[slipnum] = d.[slipnum]
         and f.[comicsName] = d.[comicsName]
         FOR XML PATH(''), TYPE).value('(./text())[1]','NVARCHAR(2000)'),1,1,'') AS [currNames]
    FROM [foo].[dbo].tbl] f
    LEFT outer join [foo].[dbo].[Zool] d
    on f.[billNo] = d.[billNo];

    Thank you, Is there a correlated query or self join approach that could be used to keep the data type of the colmn from becoming a blob?

    No need for that, the value function does the conversion to nvarchar(2000) in this case.
    😎
    The text() function also avoids the construct of an XML for the output, cuts the effort almost in half. The role of the stuff function is simply to remove the leading semicolon, does not contribute to the concatenation.

  • Eirikur Eiriksson - Wednesday, May 23, 2018 7:13 AM

    ackrite55 - Wednesday, May 23, 2018 6:53 AM

    Eirikur Eiriksson - Wednesday, May 23, 2018 12:42 AM

    Simple syntax error in the value function
    😎

    This should work

    SELECT
      d.[currType]
     ,STUFF(
        (SELECT
          '; ' + d.currName
         from [foo].[dbo].[zool] d
         where f.[issuenum] = d.[issuenum]
         and f.[slipnum] = d.[slipnum]
         and f.[comicsName] = d.[comicsName]
         FOR XML PATH(''), TYPE).value('(./text())[1]','NVARCHAR(2000)'),1,1,'') AS [currNames]
    FROM [foo].[dbo].tbl] f
    LEFT outer join [foo].[dbo].[Zool] d
    on f.[billNo] = d.[billNo];

    Thank you, Is there a correlated query or self join approach that could be used to keep the data type of the colmn from becoming a blob?

    No need for that, the value function does the conversion to nvarchar(2000) in this case.
    😎
    The text() function also avoids the construct of an XML for the output, cuts the effort almost in half. The role of the stuff function is simply to remove the leading semicolon, does not contribute to the concatenation.

    Thanks!!!! Tested & compatibility is confirmed. Greatly appreciated!

  • ackrite55 - Wednesday, May 23, 2018 8:34 AM

    Eirikur Eiriksson - Wednesday, May 23, 2018 7:13 AM

    ackrite55 - Wednesday, May 23, 2018 6:53 AM

    Eirikur Eiriksson - Wednesday, May 23, 2018 12:42 AM

    Simple syntax error in the value function
    😎

    This should work

    SELECT
      d.[currType]
     ,STUFF(
        (SELECT
          '; ' + d.currName
         from [foo].[dbo].[zool] d
         where f.[issuenum] = d.[issuenum]
         and f.[slipnum] = d.[slipnum]
         and f.[comicsName] = d.[comicsName]
         FOR XML PATH(''), TYPE).value('(./text())[1]','NVARCHAR(2000)'),1,1,'') AS [currNames]
    FROM [foo].[dbo].tbl] f
    LEFT outer join [foo].[dbo].[Zool] d
    on f.[billNo] = d.[billNo];

    Thank you, Is there a correlated query or self join approach that could be used to keep the data type of the colmn from becoming a blob?

    No need for that, the value function does the conversion to nvarchar(2000) in this case.
    😎
    The text() function also avoids the construct of an XML for the output, cuts the effort almost in half. The role of the stuff function is simply to remove the leading semicolon, does not contribute to the concatenation.

    Thanks!!!! Tested & compatibility is confirmed. Greatly appreciated!

    You are very welcome!
    😎

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply