0

我在 SQL Server 中遇到了一个未返回正确数据的查询问题。我很高兴提前承认,我应该能够看到一个简单而简洁的解决方案,但我认为我一直在想事情。

业务场景是我们正在比较我们为给定项目收取的价格与我们报价的价格。这是查询:

SELECT  ipl1.Id AS Id,
        CASE s1.FirstName WHEN '' THEN s1.LastName ELSE s1.LastName + ', ' + s1.FirstName END AS SupplierName,
        p1.Date,
        p1.OrderNo,
        p1.InvoiceNo,
        i1.Number AS ItemNumber,
        i1.Name AS ItemName,
        ipl1.Quantity,
        ROUND(ipl1.TaxExclusiveUnitPrice, 2) AS PriceCharged,
        ROUND(ISNULL(p2.Amount, p4.Amount), 2) AS PriceQuoted,
        ROUND(ROUND(ipl1.TaxExclusiveUnitPrice, 2) - ROUND(ISNULL(p2.Amount, p4.Amount), 2), 2) AS Difference,
        ipl1.Quantity*ROUND(ROUND(ipl1.TaxExclusiveUnitPrice, 2) - ROUND(ISNULL(p2.Amount, p4.Amount), 2), 2) AS Overcharge,
        ISNULL(p2.Starts, p4.Starts) AS QuoteDate,
        ISNULL(p2.QuoteNo, p4.QuoteNo) AS QuoteNumber
FROM    pl_ItemPurchaseLines ipl1
        INNER JOIN
        pl_Purchases p1 ON p1.Id=ipl1.Purchase_Id
        INNER JOIN
        pl_Suppliers s1 ON s1.Id=p1.Supplier_Id 
        INNER JOIN
        pl_Items i1 ON i1.Id=ipl1.Item_Id 
        LEFT JOIN
        --- First Priority is the lowest Amount Current Quote or List Price
        (
        SELECT  p1.Id,
                p1.Amount,
                p1.Starts,
                p1.Expires,
                p1.QuoteNo,
                ipl.Id AS ItemPurchaseLine_Id,
                row_number() over (partition by ipl.Id order by p1.Amount ASC, p1.QuoteNo DESC, p1.Starts DESC) as Row
        FROM    pl_ItemPurchaseLines ipl
                INNER JOIN
                pl_Purchases p ON p.Id=ipl.Purchase_Id
                INNER JOIN 
                pl_Prices p1 ON p1.Starts<=p.Date AND 
                                ((p1.Expires>=p.Date AND p1.QuoteNo<>'') OR
                                 (p1.Expires IS NULL AND p1.QuoteNo='')) AND
                                 p1.Item_Id=ipl.Item_Id
        ) AS p2 ON  p2.Row = 1 AND
                    ipl1.Id=p2.ItemPurchaseLine_Id
        LEFT JOIN           
        (
        SELECT  p3.Id,
                p3.Amount,
                p3.Starts,
                p3.Expires,
                p3.QuoteNo,
                ipl2.Id AS ItemPurchaseLine_Id,
                row_number() over (partition by ipl2.Id order by p3.Expires DESC) as Row
        FROM    pl_ItemPurchaseLines ipl2
                INNER JOIN
                pl_Purchases p2 ON p2.Id=ipl2.Purchase_Id
                INNER JOIN 
                pl_Prices p3 ON p3.Starts<=p2.Date AND 
                                p3.QuoteNo<>'' AND
                                p3.Item_Id=ipl2.Item_Id
        ) AS p4 ON  p4.Row = 1 AND
                    ipl1.Id=p4.ItemPurchaseLine_Id

问题的核心在于子查询。有 2 种价格,一种是有开始日期、没有到期日的标价和 '' 的报价,并被后一个标价取代;或具有开始日期、到期日期和报价编号的报价。

问题出在第一个子查询中。在这里,我正在寻找价格最低的仍然有效的报价或标价,但是,我无法弄清楚如何排除被取代的标价,即带有 quoteno='' 且到期为空的价格(这些很容易)以及其他具有相同 ID 且开始日期较晚的价目表。

我曾考虑根据触发器在表格中设置到期日期,但我对此有一些困难,因为需要处理同时出现 2 个后面的价目表的可能性(甚至更早的价目表 - 我们正在向后工作以及前锋)。

我可以在给定价格 id 时编写一个函数来确定它是否在给定日期被取代,但这似乎不是最理想的。

我觉得我只需要对 where 子句做点什么来让这些被取代的价格消失,但我已经做了一些尝试,并建立了一些不起作用的冗长的巴洛克式子句!

以下是一些示例数据:

-- No there isn't I will put some up as soon as I format it --

以下是表定义:

USE [tempdb]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[pl_Suppliers](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [LastName] [nvarchar](100) NOT NULL,
    [FirstName] [nvarchar](100) NOT NULL,
    [ShortName] [nvarchar](100) NOT NULL,
    [Prefix] [nvarchar](100) NOT NULL,
    [UserID] [nvarchar](100) NOT NULL,
    [Password] [nvarchar](1000) NOT NULL,
    [SentToMyob] [bit] NOT NULL,
 CONSTRAINT [PK_Suppliers] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[pl_ItemPurchaseLines](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Quantity] [float] NOT NULL,
    [TaxExclusiveUnitPrice] [float] NOT NULL,
    [TaxExclusiveTotal] [float] NOT NULL,
    [TaxInclusiveTotal] [float] NOT NULL,
    [TaxBasisAmount] [float] NOT NULL,
    [TaxCode] [nvarchar](max) NOT NULL,
    [Received] [float] NOT NULL,
    [TaxInclusiveUnitPrice] [float] NOT NULL,
    [Purchase_Id] [int] NOT NULL,
    [Item_Id] [int] NOT NULL,
 CONSTRAINT [PK_pl_ItemPurchaseLines] PRIMARY KEY NONCLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[pl_Purchases](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [OrderNo] [nvarchar](max) NOT NULL,
    [InvoiceNo] [nvarchar](max) NOT NULL,
    [Date] [datetime] NOT NULL,
    [ShipToAddressLine1] [nvarchar](max) NOT NULL,
    [ShipToAddressLine2] [nvarchar](max) NOT NULL,
    [ShipToAddressLine3] [nvarchar](max) NOT NULL,
    [ShipToAddressLine4] [nvarchar](max) NOT NULL,
    [Comment] [nvarchar](max) NOT NULL,
    [TotalLines] [float] NOT NULL,
    [TotalTax] [float] NOT NULL,
    [Supplier_Id] [int] NOT NULL,
    [SentToMyob] [bit] NOT NULL,
    [Job_ID] [int] NULL,
 CONSTRAINT [PK_Purchases] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[pl_Items](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Number] [nvarchar](100) NOT NULL,
    [Name] [nvarchar](255) NOT NULL,
    [Supplier_Id] [int] NOT NULL,
    [SentToMyob] [bit] NOT NULL,
 CONSTRAINT [PK_Items] PRIMARY KEY NONCLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[pl_Prices](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Amount] [float] NOT NULL,
    [Starts] [datetime] NOT NULL,
    [Expires] [datetime] NULL,
    [QuoteNo] [nvarchar](100) NOT NULL,
    [Item_Id] [int] NOT NULL,
 CONSTRAINT [PK_Prices] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[pl_Prices]  WITH CHECK ADD  CONSTRAINT [FK_ItemPrice] FOREIGN KEY([Item_Id])
REFERENCES [dbo].[pl_Items] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[pl_Prices] CHECK CONSTRAINT [FK_ItemPrice]
GO
ALTER TABLE [dbo].[pl_Items]  WITH CHECK ADD  CONSTRAINT [FK_SupplierItem] FOREIGN KEY([Supplier_Id])
REFERENCES [dbo].[pl_Suppliers] ([Id])
GO

ALTER TABLE [dbo].[pl_Items] CHECK CONSTRAINT [FK_SupplierItem]
GO

ALTER TABLE [dbo].[pl_Items] ADD  CONSTRAINT [DF_pl_Items_SentToMyob]  DEFAULT ((0)) FOR [SentToMyob]
GO

ALTER TABLE [dbo].[pl_Purchases]  WITH CHECK ADD  CONSTRAINT [FK_PurchasesSuppliers] FOREIGN KEY([Supplier_Id])
REFERENCES [dbo].[pl_Suppliers] ([Id])
GO

ALTER TABLE [dbo].[pl_Purchases] CHECK CONSTRAINT [FK_PurchasesSuppliers]
GO

ALTER TABLE [dbo].[pl_Purchases] ADD  CONSTRAINT [DF_pl_Purchases_SentToMyob]  DEFAULT ((0)) FOR [SentToMyob]
GO

ALTER TABLE [dbo].[pl_ItemPurchaseLines]  WITH CHECK ADD  CONSTRAINT [FK_ItemPurchaseLineItem] FOREIGN KEY([Item_Id])
REFERENCES [dbo].[pl_Items] ([Id])
GO

ALTER TABLE [dbo].[pl_ItemPurchaseLines] CHECK CONSTRAINT [FK_ItemPurchaseLineItem]
GO

ALTER TABLE [dbo].[pl_ItemPurchaseLines]  WITH CHECK ADD  CONSTRAINT [FK_Purchase_ItemPurchaseLines] FOREIGN KEY([Purchase_Id])
REFERENCES [dbo].[pl_Purchases] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[pl_ItemPurchaseLines] CHECK CONSTRAINT [FK_Purchase_ItemPurchaseLines]
GO

ALTER TABLE [dbo].[pl_Suppliers] ADD  CONSTRAINT [DF_pl_Suppliers_SentToMyob]  DEFAULT ((0)) FOR [SentToMyob]
GO
4

2 回答 2

1

您的解决方案在子选择中有一个错误:p1.Item_Id=p1.Item_Id 这仅引用“子选择”表“p1”而不是“外部选择”表“p”

任何带有子选择的解决方案都不适用于大型数据集。

假设适当的索引,这样的事情将是可扩展的:

SELECT p.[Id]
  ,p.[Amount]
  ,p.[Starts] 
  ,ISNULL(p.Expires, DATEADD(DAY,-1,MIN(p1.Starts))
  ,p.[QuoteNo]
  ,p.[Item_Id]
FROM    pl_Prices p

LEFT JOIN pl_Prices p1 ON
     p1.Item_Id=p.Item_Id AND    
     p1.Starts>p.Starts
GROUP BY 
      p.[Id]
      ,p.[Amount]
      ,p.[Starts] 
      ,p.[QuoteNo]
      ,p.[Item_Id]
于 2012-12-08T14:58:13.147 回答
0

我想通了——正如我所料,这很简单。

我创建了这个视图,并在第一个子查询中引用了它。

ALTER VIEW [dbo].[pl_PricesWithExpiry]
AS
SELECT [Id]
      ,[Amount]
      ,[Starts] 
      ,ISNULL(p.Expires, (SELECT DATEADD(DAY,-1,MIN(p1.Starts))
                          FROM pl_Prices p1
                          WHERE p1.Item_Id=p.Item_Id AND
                          p1.Starts>p.Starts)) AS Expires
      ,[QuoteNo]
      ,[Item_Id]
FROM    pl_Prices p
于 2012-12-07T05:14:43.123 回答