Table variables and joins
- From: hharry <paulquigley@xxxxxxx>
- Date: Mon, 31 Mar 2008 11:57:44 -0700 (PDT)
Hello All,
I have this table:
CREATE TABLE [dbo].[AMS](
[AMSFGGID] [int] IDENTITY(1,1) NOT NULL,
[AMSDESCRIPTION] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
with these values:
INSERT INTO AMS (AMSDESCRIPTION)
VALUES ('TEST DESC 1')
INSERT INTO AMS (AMSDESCRIPTION)
VALUES ('TEST DESC 2')
I also have a script which creates a table variable:
DECLARE @RESULTS Table
(
ROWID INT IDENTITY(1,1),
AMSFGGID INT,
QTYSOLD FLOAT
)
INSERT @RESULTS
(
AMSFGGID,
QTYSOLD
)
VALUES
(
1, 300
)
INSERT @RESULTS
(
AMSFGGID,
QTYSOLD
)
VALUES
(
1, 700
)
I'm trying to get construct a join which will sum the quantities sold
for each AMS record, something like:
SELECT
A.AMSFGGID, S.TotalSales
FROM
AMS A
INNER JOIN
(SELECT
AMSFGGID, SUM(QTYSOLD) as TotalSales
FROM
@RESULTS
GROUP BY
AMSFGGID) S
ON
A.AMSFGGID = S.AMSFGGID
Pointers appreciated!
.
- Follow-Ups:
- Re: Table variables and joins
- From: Jason Lepack
- Re: Table variables and joins
- From: Plamen Ratchev
- Re: Table variables and joins
- Prev by Date: Re: difference between explicit inner join and implicit
- Next by Date: Re: Table variables and joins
- Previous by thread: difference between explicit inner join and implicit
- Next by thread: Re: Table variables and joins
- Index(es):