Sql-server – Join three queries into one

sql server

I'm using SQL Server 13.0.5102.14.

I have these three tables:

CREATE TABLE [dbo].[ProductionOrder]
(
    [ProductionOrderId] INT NOT NULL IDENTITY (1, 1), 
    [ProductId] INT NOT NULL, 
    [Name] NVARCHAR(50) NOT NULL,
    CONSTRAINT [PK_ProductionOrder] PRIMARY KEY CLUSTERED 
    (
        [ProductionOrderId] ASC
    ))
)

CREATE TABLE [dbo].[Code]
(
    [CodeId] int NOT NULL IDENTITY(1, 1),
    [Serial] [varchar](38) not null,
    [ProductionOrderId] int NOT NULL,
    [AggregationLevel] [tinyint] NOT NULL,
     CONSTRAINT [PK_Code] PRIMARY KEY CLUSTERED 
    (
        [CodeId] ASC
    ),
    CONSTRAINT [UC_Code_Serial] UNIQUE ([Serial]),
    CONSTRAINT [FK_Code_ProductionOrder_ProductionOrderId] FOREIGN KEY ([ProductionOrderId]) REFERENCES [dbo].[ProductionOrder] ([ProductionOrderId]))
)

CREATE TABLE [dbo].[VariableData]
(
    [ProductionOrderId] INT NOT NULL,
    [AggregationLevelConfigurationId] TINYINT NOT NULL, 
    [VariableDataId] VARCHAR(4) NOT NULL, 
    [Value] NVARCHAR(200) NOT NULL,
    CONSTRAINT [PK_VariableData] PRIMARY KEY CLUSTERED 
    (
        [AggregationLevelConfigurationId] ASC,
        [ProductionOrderId] ASC,
        [VariableDataId] ASC
    ), 
    CONSTRAINT [FK_VariableData_AggregationLevelConfiguration_AggregationLevelConfigurationId] FOREIGN KEY ([AggregationLevelConfigurationId], [ProductionOrderId]) REFERENCES [dbo].[AggregationLevelConfiguration] ([AggregationLevelConfigurationId], [ProductionOrderId]) ON DELETE CASCADE,
    CONSTRAINT [FK_VariableData_ProductionOrder_ProductionOrderId] FOREIGN KEY ([ProductionOrderId]) REFERENCES [dbo].[ProductionOrder] ([ProductionOrderId]),
    CONSTRAINT [CK_VariableData_VariableDataId] CHECK (([VariableDataId]<>N''))
)

CREATE TABLE [dbo].[Product]
(
    [ProductId] INT NOT NULL IDENTITY (1, 1),
    [ProductCode] VARCHAR(14) not null,
    [Description] NVARCHAR(50) NULL,
    [LawId] TINYINT NOT NULL,  
    [Name] NVARCHAR(100) NOT NULL, 
    [Comment] NVARCHAR(100) NULL, 
    CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
    (
        [ProductId] ASC
    ),
    CONSTRAINT [CK_Product_ProductCode] CHECK (([ProductCode]<>N'')),
    CONSTRAINT [CK_Product_Name] CHECK (([Name]<>N''))
)

CREATE TABLE [dbo].[AggregationChildren]
(
    [AggregationChildrenId] INT NOT NULL,
    [AggregationId] INT NOT NULL,
    [Position] [int] NOT NULL,
    CONSTRAINT [PK_AggregationChildren] PRIMARY KEY CLUSTERED 
    (
        [AggregationChildrenId] ASC
    ), 
    CONSTRAINT [FK_AggregationChildren_Aggregation_AggregationId] FOREIGN KEY ([AggregationId]) REFERENCES [Aggregation]([AggregationId]) ON DELETE CASCADE, 
    CONSTRAINT [FK_AggregationChildren_Code_AggregationChildrenId] FOREIGN KEY ([AggregationChildrenId]) REFERENCES [Code]([CodeId])
)

CREATE TABLE [dbo].[Aggregation]
(
    [AggregationId] INT NOT NULL,
    [Created] varchar(34) NULL,
    CONSTRAINT [PK_Aggregation] PRIMARY KEY CLUSTERED
    (
        [AggregationId] ASC
    ),
    CONSTRAINT [FK_Aggregation_Code_AggregationId] FOREIGN KEY ([AggregationId]) REFERENCES [dbo].[Code] ([CodeId])
)

I have this three working queries:

declare @prodID int = 1;
declare @lotAI varchar(4) = '10';

select cod.Serial as ItemNO
     , varData.Value as Lot
     , pro.ProductCode as Product

  from dbo.VariableData varData 
        JOIN dbo.Code cod ON varData.ProductionOrderId = cod.ProductionOrderId
        JOIN dbo.ProductionOrder proOrd ON varData.ProductionOrderId = proOrd.ProductionOrderId
        JOIN dbo.Product pro on proOrd.ProductId = pro.ProductId   

 where varData.ProductionOrderId = @prodID and
       varData.VariableDataId = @lotAI and
       varData.AggregationLevelConfigurationId = 1 and
       cod.AggregationLevel = 1


select cod.Serial as Box

  from dbo.Code cod
       LEFT JOIN dbo.AggregationChildren agg on agg.AggregationId = cod.CodeId

 where cod.ProductionOrderId = @prodID and
       cod.AggregationLevel = 2


select cod.Serial as Pallet

  from dbo.Code cod
       JOIN dbo.Aggregation agg on agg.AggregationId = cod.CodeId

 where cod.ProductionOrderId = @prodID and
       cod.AggregationLevel = 3

I want to join all of them into one query. My problem to do it is that I using dbo.Code to get three different values:

  • select cod.Serial as ItemNO
  • select cod.Serial as Box
  • select cod.Serial as Pallet

How can I join these three queries into one?

Best Answer

You should be able to inline the selects for Box and Pallet as this example shows:

DECLARE @prodID INT = 1;
DECLARE @lotAI VARCHAR(4) = '10';

SELECT cod.Serial AS ItemNO
    ,varData.Value AS Lot
    ,pro.ProductCode AS Product
    ,(
        SELECT cod.Serial AS Box
        FROM dbo.Code cod
        LEFT JOIN dbo.AggregationChildren agg ON agg.AggregationId = cod.CodeId
        WHERE cod.ProductionOrderId = @prodID
            AND cod.AggregationLevel = 2
        ) AS Box
    ,(
        SELECT cod.Serial AS Pallet
        FROM dbo.Code cod
        JOIN dbo.Aggregation agg ON agg.AggregationId = cod.CodeId
        WHERE cod.ProductionOrderId = @prodID
            AND cod.AggregationLevel = 3
        ) AS Pallet
FROM dbo.VariableData varData
JOIN dbo.Code cod ON varData.ProductionOrderId = cod.ProductionOrderId
JOIN dbo.ProductionOrder proOrd ON varData.ProductionOrderId = proOrd.ProductionOrderId
JOIN dbo.Product pro ON proOrd.ProductId = pro.ProductId
WHERE varData.ProductionOrderId = @prodID
    AND varData.VariableDataId = @lotAI
    AND varData.AggregationLevelConfigurationId = 1
    AND cod.AggregationLevel = 1