显示带有连接字段的其他字段

时间:2014-01-15 17:46:23

标签: sql sql-server sql-server-2008

previous question我得到了这个结果:

    WITH    ItemCount
      AS ( SELECT BrandId
               ,COUNT(Items.ITEMNO) AS item_Count
            FROM Items
               ,Brand_Products
               ,Brands
            WHERE Items.ITEMNO = Brand_Products.ItemNo
                AND Brands.BrandId = Brand_Products.BrandId
                AND Items.SubcategoryID = 'SCat-020'
            GROUP BY Brands.BrandId)
SELECT b.BrandName + ' ' + CONVERT(VARCHAR(5), Item_Count)
    FROM Brands AS b
    JOIN ItemCount AS I
        ON b.BrandId = i.BrandId

我想要添加的是选择连接字符串和BrandId。

1 个答案:

答案 0 :(得分:1)

除非这是某种技巧问题,否则您只需将其添加到SELECT列表中:

SELECT b.BrandId, b.BrandName + ' ' + CONVERT(VARCHAR(5), Item_Count)
    FROM Brands AS b
    JOIN ItemCount AS I
        ON b.BrandId = i.BrandId