将两个子表项与两个数据库中的两个相似父表进行比较

时间:2018-03-23 13:38:17

标签: sql-server relational-database

我需要帮助创建一个查询,将一个数据库中的Equipment与另一个表中的Asset进行比较。这是我的数据库设置:

CREATE DATABASE database1
GO

USE [database1]
GO

CREATE TABLE [dbo].[Application]
(
    [ApplicationID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [APP_NUMBRER] [int] NULL,
)
GO

CREATE TABLE [dbo].[Equipment]
(
    [EquipID] [int] IDENTITY(1,1) NOT NULL,
    [ApplicationID] [int] NULL,
    [Year] [varchar](4) NULL,
    [Make] [varchar](50) NULL,
    [Model] [varchar](50) NULL
)

ALTER TABLE [dbo].[Equipment] WITH CHECK 
    ADD CONSTRAINT [FK_EQUIP_1] 
        FOREIGN KEY([ApplicationID]) REFERENCES [dbo].[APPLICATION] ([ApplicationID])
GO

INSERT INTO [Application] 
VALUES (1), (2), (3)

INSERT INTO [Equipment] 
VALUES (1, '1998', 'Equip1', 'Model1'),
       (1, '1855', 'Equip2', 'Model2'),
       (2, '1222', 'Equip3', 'Model4'),
       (2, '1333', 'Equip4', 'Model4'),
       (3, '1777', 'Equip5', 'Model5')
GO

CREATE DATABASE database2
GO

USE [database2]
GO

CREATE TABLE [dbo].[Application]
(
    [APP_KEY] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [APP_DESCRIPTION] [varchar](40) NOT NULL
)
GO

CREATE TABLE [dbo].[ASSET]
(
    [AS_KEY] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [AS_APP_FKEY] [int] NOT NULL,
    [Year] [varchar](4) NULL,
    [Make] [varchar](50) NULL,
    [Model] [varchar](50) NULL
)

ALTER TABLE [dbo].[ASSET] WITH CHECK 
    ADD CONSTRAINT [FK_ASSET_1] 
        FOREIGN KEY([AS_APP_FKEY]) REFERENCES [dbo].[APPLICATION] ([APP_KEY])
GO

INSERT INTO [Application] 
VALUES ('AppDesc1'), ('AppDesc2')

INSERT INTO [ASSET] 
VALUES (1, '1998', 'Asset1', 'Db2Model1'),
       (1, '1855', 'Asset2', 'Db2Model2'),
       (2, '1222', 'Asset3', 'Db2Model3'),
       (2, '1333', 'Asset4', 'Db2Model4')
GO

我的查询:

SELECT 
    ap1.APP_NUMBRER, 
    e.Year, e.Make, e.Model, 
    db2.APP_KEY, db2.Year, db2.Make, db2.Model
FROM 
    database1.dbo.Application ap1
JOIN 
    database1.dbo.Equipment e ON E.APPLICATIONID = ap1.APPLICATIONID
LEFT JOIN
    (SELECT 
         APP_KEY, Year, Make, Model
     FROM 
         [database2].dbo.APPLICATION ap2
     JOIN 
         [database2].dbo.ASSET ON asset.AS_APP_FKEY = ap2.APP_KEY) db2 ON ap1.APP_NUMBRER = db2.APP_KEY

结果:

AppResult

预期结果:我的查询正在创建一些重复项,将所有db1设备与db2资产进行比较。我希望进行一对一的比较。我不想要2,3,6,7项。这是因为表关系的设置方式。

Expected Result

3 个答案:

答案 0 :(得分:1)

这是因为您只加入APP_NUMBER=APP_KEY。将Year=Year添加到JOIN,您将获得所需的结果。

答案 1 :(得分:0)

在您加入时添加YEAR。它会解决你的问题。

SELECT ap1.APP_NUMBRER, e.Year, e.Make, e.Model, db2.APP_KEY, db2.Year, db2.Make, db2.Model
FROM database1.dbo.Application ap1
JOIN database1.dbo.Equipment e
    ON E.APPLICATIONID = ap1.APPLICATIONID
LEFT JOIN(
SELECT APP_KEY, Year, Make, Model
       FROM [database2].dbo.APPLICATION ap2
       JOIN [database2].dbo.ASSET 
            ON asset.AS_APP_FKEY = ap2.APP_KEY
        ) db2
ON ap1.APP_NUMBRER = db2.APP_KEY AND e.Year = db2.Year

答案 2 :(得分:0)

像这样修改您的JOIN条件

    db2     ON ap1.APP_NUMBRER  = db2.APP_KEY
            AND e.Year = db2.Year