• msh083 (2/3/2016)


    GilaMonster (2/3/2016)


    You've given them names. First and Second in your code example. You can reference them by those names in the last query.

    What are you trying to do?

    i want use SUM(dbo.card_in.in1) AS sum_in in first select for last select,

    in last select i do not know ??

    SELECT dbo.card_in.id_reg, dbo.region.name_reg, dbo.card_in.id_card, dbo.card.name_card, SUM(dbo.card_in.in1) AS sum_in

    FROM dbo.card_in INNER JOIN

    dbo.card ON dbo.card_in.id_card = dbo.card.id_card INNER JOIN

    dbo.region ON dbo.card_in.id_reg = dbo.region.id_reg

    WHERE (dbo.card_in.datefa1 BETWEEN @d1 AND @d2)

    GROUP BY dbo.card_in.id_reg, dbo.region.name_reg, dbo.card.name_card, dbo.card_in.id_card

    Without going into details, you can JOIN to first and second like any other tables (after all, the T in CTE stands for Table), then in the select list you refer to the fields using the CTE name or alias: first.sum_in