How do I merge two rows only if there is the TagWidgetIds are same but the tagValues are different? I have this stored procedure which generates the following output. 


The query which generates the output from the stored procedure is as follows:

SELECT tat.[agent-id],
               tat.[agent-guid],
               tat.[agent-name]  AS [AgentName],
               tw.TagWidgetID,
               tw.TagWidgetName,
               t.TagID,
               core.fnInitialCapitalization(t.DisplayName) AS TagName,
               tt.TagTypeID,
               tt.TagTypeName,
               tdt.TagDataTypeID,
               tdt.TagDataType,
               tu.TagUnitID,
               tu.TagUnit,
               --CASE 
               --     WHEN itv.TagIndex IS NULL THEN 0
               --     ELSE itv.TagIndex
               --END               AS [TagIndex],
               ISNULL(itv.TagIndex, 0) AS [TagIndex],
               --CASE 
               --     WHEN itv.ValueIndex IS NULL THEN 0
               --     ELSE itv.ValueIndex
               --END               AS [ValueIndex],
               ISNULL(itv.ValueIndex, 0) AS [ValueIndex],
               --CASE 
               --     WHEN itv.[Value] IS NULL THEN 'N/A'
               --     ELSE itv.[Value]
               --END               AS [TagValue]
                ISNULL( itv.[Value], 'N/A') AS [TagValue]
        FROM   @tmpAgentTags tat
               LEFT OUTER JOIN tag.ItemTagValue itv
                    ON  itv.FkTagID = tat.[tag-id]
                    AND itv.FkItemID = tat.[agent-id]
               INNER JOIN tag.Tag t
                    ON  t.TagID = tat.[tag-id]
               INNER JOIN tag.TagWidget tw
                    ON  tw.TagWidgetID = t.FkTagWidgetID
               INNER JOIN tag.TagType tt
                    ON  tt.TagTypeID = t.FkTagTypeID
               INNER JOIN tag.TagDataType tdt
                    ON  tdt.TagDataTypeID = t.FkTagDataTypeID
               INNER JOIN tag.TagUnit tu
                    ON  tu.TagUnitID = t.FkTagUnitID
               INNER JOIN item.Item i
                    ON i.PkItemID = tat.[agent-id]
        WHERE i.FkItemTypeID NOT IN (5,8)


How can  I join the output rows only if the TagWidegetIds are same but the TagValues, TagIndex and ValueIndex columns are differnt? please advice
You can first insert the results from the stored procedure into a temporary table (see https://stackoverflow.com/questions/653714/insert-results-of-a-stored-procedure-into-a-temporary-table), and then simply query that table with your conditions.  Let's say that you put the results in a temporary table called #procData.  In that case, your final query would look something like:

SELECT &lt;columns_you_want&gt;
FROM #procData p1
JOIN #procData p2
    ON p2.TagWidgetId = p1.TagWidgetId
    -- NOTE: if your columns are nullable, adjust this logic to account for that
    AND p2.TagValues &lt;&gt; p1.TagValues 
    AND p2.TagIndex &lt;&gt; p1.TagIndex 
    AND p2.ValueIndex &lt;&gt; p1.ValueIndex