I need to inner join data from a different databases. Three to be exact.
I've tried to do a Select Statement from the different databases but I can't get the inner join to work for some reason.
Should I Inner join one at a time instead of doing all 3 at once? The tricky part has been assigning the results table from the first query to a variable so that I could inner join it with the subsequent tables.
Current Code I have
(DECLARE @SDate date = '01/01/2018'
DECLARE @EDate date = '12/31/2018'
DECLARE @Sku TABLE
(
Sku varchar(20)
);
INSERT @Sku (Sku)
VALUES
(N'SN2150'),
(N'SN2151');
SELECT
l.No_
,SUM(l.Amount) AS Amount
,SUM(l.Quantity) AS Quantity
FROM
dbo.[Threshold Enterprises$Sales Invoice Line] AS l
JOIN
@Sku AS s
ON s.Sku = l.No_
WHERE
l.[Shipment Date] BETWEEN @SDate AND @EDate
GROUP BY
l.No_)
AS a
Select [Item.No_] and [Suggested Retail Price] and [Starting Date] FROM [NAV-
WH].dbo.v_NAV_PurchasePriceCurrent b
Select [No_] and [Size] and [Size.Type] FROM [NAV-WH].[dbo].[Threshold
Enterprises$Item] c
Inner Join a b c where a.[@SKU]=b.[Item.No_]=c.[No_]
Get the following Error Message
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near ')'.
Expect the results to give me the SKU, sales, units sold, retail price, starting data, size, and size type.
Tried the previous code but it gave the same error message of
Msg 207, Level 16, State 1, Line 25
Invalid column name 'Item.No_'.
Code I had tried to use to instead spell everything out was:
DECLARE @SDate date = '01/01/2018'
DECLARE @EDate date = '12/31/2018'
DECLARE @Sku TABLE
(
Sku varchar(20)
);
INSERT @Sku (Sku)
VALUES
(N'SN2150'),
(N'SN2151');
SELECT
l.No_ as SKU
,SUM(l.Amount) AS Amount
,SUM(l.Quantity) AS Quantity
,[NAV-WH].[dbo].[v_NAV_PurchasePriceCurrent].[Suggested Retail Price]
,[NAV-WH].[dbo].[v_NAV_PurchasePriceCurrent].[Starting Date]
,[NAV-WH].[dbo].[ThresholdEnterprises$Item].Size
,[NAV-WH].[dbo].[ThresholdEnterprises$Item].[Size.Type]
FROM
dbo.[Threshold Enterprises$Sales Invoice Line] AS l
JOIN @Sku AS s ON s.Sku = l.No_
JOIN [NAV-WH].[dbo].[v_NAV_PurchasePriceCurrent] on [NAV-WH].[dbo].
[v_NAV_PurchasePriceCurrent].[Item.No_] = l.No_
JOIN [NAV-WH].[dbo].[ThresholdEnterprises$Item] on [NAV-WH].[dbo].
[ThresholdEnterprises$Item].[No_] = l.No_
WHERE
l.[Shipment Date] BETWEEN @SDate AND @EDate
GROUP BY
l.No_
I was able to get it to work. It was a simple Group By work around. Thanks to those who pointed me in the right direction!
use [NAV-WH]
DECLARE @SDate date = '01/01/2018'
DECLARE @EDate date = '12/31/2018'
DECLARE @Sku TABLE
(
Sku varchar(20)
);
INSERT @Sku (Sku)
VALUES
(N'SN2150'),
(N'SN2151');
SELECT [Threshold Enterprises$Item].No_ AS SKU, SUM([Threshold
Enterprises$Sales Invoice Line].Amount) AS Amount, SUM([Threshold
Enterprises$Sales Invoice Line].Quantity) AS Quantity,
v_NAV_PurchasePriceCurrent.[Suggested Retail Price],
v_NAV_PurchasePriceCurrent.[Starting Date], [Threshold Enterprises$Item].Size,
[Threshold Enterprises$Item].[Size Type]
FROM [Threshold Enterprises$Item] INNER JOIN
v_NAV_PurchasePriceCurrent ON [Threshold
Enterprises$Item].No_ = v_NAV_PurchasePriceCurrent.[Item No_] INNER JOIN
[Threshold Enterprises$Sales Invoice Line] ON
[Threshold Enterprises$Item].No_ = [Threshold Enterprises$Sales Invoice
Line].No_
WHERE ([Threshold Enterprises$Item].No_ = N'SN2051' OR
[Threshold Enterprises$Item].No_ = N'SN2151') AND
([Threshold Enterprises$Sales Invoice Line].[Shipment Date] BETWEEN @SDate AND
@EDate)
GROUP BY [Threshold Enterprises$Item].No_, v_NAV_PurchasePriceCurrent.
[Suggested Retail Price], v_NAV_PurchasePriceCurrent.[Starting Date], [Threshold
Enterprises$Item].Size, [Threshold Enterprises$Item].[Size Type]