我需要内部连接来自其他数据库的数据。确切地说是三个。
我试图从不同的数据库中执行Select语句,但是由于某种原因,我无法使内部联接起作用。
我内部应该一次加入一个,而不是一次全部加入三个吗?棘手的部分是将第一个查询的结果表分配给变量,以便我可以将其与后续表进行内部联接。
我拥有的当前代码
(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_]
获取以下错误消息
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 ')'.
期待结果为我提供SKU,销售额,售出单位,零售价格,起始数据,尺寸和尺寸类型。
尝试了前面的代码,但给出了与
相同的错误消息Msg 207, Level 16, State 1, Line 25
Invalid column name 'Item.No_'.
我曾经用来代替所有内容的代码是:
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_
答案 0 :(得分:1)
如果所有数据库都在同一实例上,请使用三部分名称在join语句中引用它们。
Select *
From (
SELECT TABLE1.ACCNT_CODE
,TABLE1.PERIOD
,TABLE1.REPORT_AMT
, 'B' MEASURE
FROM TABLE1
UNION ALL
SELECT TABLE2.ACCNT_CODE
,TABLE2.PERIOD
,TABLE2.REPORT_AMT
,'A' MEASURE
FROM TABLE2
) src
PIVOT ( SUM(REPORT_AMT) FOR MEASURE IN ([A], [B]) ) AS PivotTable
如果数据库位于不同的实例上,请在它们之间设置一个链接服务器,然后使用四部分名称FROM [DB1].[dbo].[TableA] a
JOIN [DB2].[dbo].[TableB] b on a.Key = b.Key
LEFT JOIN [DB3].[dbo].[TableC] on c.Key = a.Key
这假定执行查询的用户在所有数据库中都具有适当的权限。
编辑-根据您对原始问题的编辑,建议您一次完成所有操作。试试这个(我无法测试,因为我没有您的所有桌子)。
...JOIN [LINKEDSERVER].[DB2].[dbo].[TableB]...
答案 1 :(得分:0)
我能够使它正常工作。这是一个简单的Group By解决方案。感谢那些为我指明了正确方向的人!
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]