larrydavid
Programmer
Hello,
I have a query in a stored proc using a CTE that I want to join with two other queries (Test1 and Test2 in comments below):
If someone could show me how to use temp tables to join in these tables I would really appreciate the help.
Thanks,
Larry
I have a query in a stored proc using a CTE that I want to join with two other queries (Test1 and Test2 in comments below):
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_Test]
(
@BeginDate DATETIME,
@EndDate DATETIME,
@VendorFileName VARCHAR(32)
)
AS
BEGIN
SET NOCOUNT ON;
SELECT [Source Name],
[Vendor File Name],
[RecordCount]
FROM
(
SELECT count(*) AS [RecordCount],
st.[Source Name],
a.[Vendor File Name]
FROM tbl_AllComp a INNER JOIN tbl_Source_Type st ON a.ID = st.ID
GROUP BY st.[Source Name], a.[Vendor File Name]
) x
WHERE [Vendor File Load Date] BETWEEN @BeginDate And @EndDate
GROUP BY [Source Name],[Vendor File Name],[RecordCount]
HAVING [Source Name] Like '%' + @VendorFileName + '%'
ORDER BY [Vendor File Load Date]
/*
-- *****Test1
SELECT r.VENDOR_ID,
[Vendor File Name] = v.Vendor_Name,
[Vendor File Load Date] = CONVERT(VARCHAR(10), LOAD_DATE, 101),
Count(r.SUB_ID) AS [No of Test1 Rejects]
FROM dbo.TEST1_SUBMISSION_REJECTS r INNER JOIN VENDOR v ON
r.VENDOR_ID = v.VENDOR_ID INNER JOIN
BATCH_TYPE b ON r.BATCH_TYPE_ID = b.BATCH_TYPE_ID
GROUP BY r.VENDOR_ID,
v.Vendor_Name,
b.BATCH_TYPE_NAME,
r.LOAD_DATE,
CONVERT(VARCHAR(10), LOAD_DATE, 101)
HAVING CONVERT(VARCHAR(10), LOAD_DATE, 101) Between @BeginDate And @EndDate
AND Vendor_Name LIKE @VendorFileName
-- *****Test2
SELECT r.VENDOR_ID,
[Vendor File Name] = v.Vendor_Name,
[Vendor File Load Date] = CONVERT(VARCHAR(10), [Test_Load_Date], 101),
Count(r.VENDOR_ID) AS [No of Test2 Rejects]
FROM dbo.Test_Load_Results r INNER JOIN VENDOR v ON
r.VENDOR_ID = v.VENDOR_ID INNER JOIN
BATCH_TYPE b ON r.BATCH_TYPE_ID = b.BATCH_TYPE_ID
WHERE r.Load_Status <> 1
GROUP BY r.VENDOR_ID,
v.Vendor_Name,
b.BATCH_TYPE_NAME,
r.[Test_Load_Date],
CONVERT(VARCHAR(10), [Test_Load_Date], 101)
HAVING CONVERT(VARCHAR(10), [Test_Load_Date], 101) Between @BeginDate And @EndDate
AND Vendor_Name LIKE @VendorFileName
*/
END
If someone could show me how to use temp tables to join in these tables I would really appreciate the help.
Thanks,
Larry