子查询给出null结果

时间:2016-08-18 21:15:08

标签: sql-server subquery

我有两个表,我想制作一个子查询,如下所示:

SELECT VPL.[Location Code], sum(VPL.Amount)  as totaal, V.[Freight-free Limit]
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
JOIN [Verploegen POC$Vendor] V
ON VPL.No_ = V.No_
--WHERE [Buy-from Vendor No_] =  '3929' -- is empty??' --'3932' --'3929' --'3923' --'3904' --'3885' --'3820' --'3894' 
WHERE VPL.[Buy-from Vendor No_] = '3929' AND VPL.Type = 2 AND [Posting Date] BETWEEN '2016-01-01' AND '2016-12-31' 
GROUP BY VPL.[Location Code], V.[Freight-free Limit]  --, VPL.[Direct Unit Cost]

但是这给了零重复。但如果我这样做:

SELECT V.[Freight-free Limit], PL.[Location Code],  sum(Amount)  as totaal   --SUM([Unit Cost])
FROM [Verploegen POC$Vendor] V
FULL JOIN [Verploegen POC$Purch_ Inv_ Line] PL
ON V.No_= PL.No_
WHERE [Buy-from Vendor No_] = '3929' AND Type = 2 AND [Posting Date] BETWEEN '2016-01-01' AND '2016-12-31'
GROUP BY V.[Freight-free Limit], PL.[Location Code]

它给出了这个结果:

NULL    AM  1585.30000000000000000000
NULL    DB  1865.91000000000000000000
NULL    DL  3652.43000000000000000000
NULL    RD  1599.32000000000000000000
NULL    ZM  571.54000000000000000000

你明白了。如果客户订购了一些金额,则客户无需支付运费。这是属性:[无货运限额]

例如:

SELECT [Freight-free Limit]
FROM [Verploegen POC$Vendor]
WHERE No_ = '3929'

结果如下: 350.00000000000000000000

但是我没有得到Subquery的作品。

谢谢。

这是表格:Purch_inv_line:

USE [VERPLOEGEN-NAV2009-LIVE]
GO

/****** Object:  Table [dbo].[Verploegen POC$Purch_ Inv_ Line]    Script Date: 08/18/2016 23:42:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Verploegen POC$Purch_ Inv_ Line](
    [timestamp] [timestamp] NOT NULL,
    [Document No_] [varchar](20) NOT NULL,
    [Line No_] [int] NOT NULL,
    [Buy-from Vendor No_] [varchar](20) NOT NULL,
    [Type] [int] NOT NULL,
    [No_] [varchar](20) NOT NULL,
    [Location Code] [varchar](10) NOT NULL,
    [Posting Group] [varchar](10) NOT NULL,
    [Expected Receipt Date] [datetime] NOT NULL,
    [Description] [varchar](50) NOT NULL,
    [Description 2] [varchar](50) NOT NULL,
    [Unit of Measure] [varchar](10) NOT NULL,
    [Quantity] [decimal](38, 20) NOT NULL,
    [Direct Unit Cost] [decimal](38, 20) NOT NULL,
    [Unit Cost (LCY)] [decimal](38, 20) NOT NULL,
    [VAT %] [decimal](38, 20) NOT NULL,
    [Line Discount %] [decimal](38, 20) NOT NULL,
    [Line Discount Amount] [decimal](38, 20) NOT NULL,
    [Amount] [decimal](38, 20) NOT NULL,
    [Amount Including VAT] [decimal](38, 20) NOT NULL,
    [Unit Price (LCY)] [decimal](38, 20) NOT NULL,
    [Allow Invoice Disc_] [tinyint] NOT NULL,
    [Gross Weight] [decimal](38, 20) NOT NULL,
    [Net Weight] [decimal](38, 20) NOT NULL,
    [Units per Parcel] [decimal](38, 20) NOT NULL,
    [Unit Volume] [decimal](38, 20) NOT NULL,
    [Appl_-to Item Entry] [int] NOT NULL,
    [Shortcut Dimension 1 Code] [varchar](20) NOT NULL,
    [Shortcut Dimension 2 Code] [varchar](20) NOT NULL,
    [Job No_] [varchar](20) NOT NULL,
    [Indirect Cost %] [decimal](38, 20) NOT NULL,
    [Pay-to Vendor No_] [varchar](20) NOT NULL,
    [Inv_ Discount Amount] [decimal](38, 20) NOT NULL,
    [Vendor Item No_] [varchar](20) NOT NULL,
    [Gen_ Bus_ Posting Group] [varchar](10) NOT NULL,
    [Gen_ Prod_ Posting Group] [varchar](10) NOT NULL,
    [VAT Calculation Type] [int] NOT NULL,
    [Transaction Type] [varchar](10) NOT NULL,
    [Transport Method] [varchar](10) NOT NULL,
    [Attached to Line No_] [int] NOT NULL,
    [Entry Point] [varchar](10) NOT NULL,
    [Area] [varchar](10) NOT NULL,
    [Transaction Specification] [varchar](10) NOT NULL,
    [Tax Area Code] [varchar](20) NOT NULL,
    [Tax Liable] [tinyint] NOT NULL,
    [Tax Group Code] [varchar](10) NOT NULL,
    [Use Tax] [tinyint] NOT NULL,
    [VAT Bus_ Posting Group] [varchar](10) NOT NULL,
    [VAT Prod_ Posting Group] [varchar](10) NOT NULL,
    [Blanket Order No_] [varchar](20) NOT NULL,
    [Blanket Order Line No_] [int] NOT NULL,
    [VAT Base Amount] [decimal](38, 20) NOT NULL,
    [Unit Cost] [decimal](38, 20) NOT NULL,
    [System-Created Entry] [tinyint] NOT NULL,
    [Line Amount] [decimal](38, 20) NOT NULL,
    [VAT Difference] [decimal](38, 20) NOT NULL,
    [VAT Identifier] [varchar](10) NOT NULL,
    [IC Partner Ref_ Type] [int] NOT NULL,
    [IC Partner Reference] [varchar](20) NOT NULL,
    [Prepayment Line] [tinyint] NOT NULL,
    [IC Partner Code] [varchar](20) NOT NULL,
    [Posting Date] [datetime] NOT NULL,
    [Job Task No_] [varchar](20) NOT NULL,
    [Job Line Type] [int] NOT NULL,
    [Job Unit Price] [decimal](38, 20) NOT NULL,
    [Job Total Price] [decimal](38, 20) NOT NULL,
    [Job Line Amount] [decimal](38, 20) NOT NULL,
    [Job Line Discount Amount] [decimal](38, 20) NOT NULL,
    [Job Line Discount %] [decimal](38, 20) NOT NULL,
    [Job Unit Price (LCY)] [decimal](38, 20) NOT NULL,
    [Job Total Price (LCY)] [decimal](38, 20) NOT NULL,
    [Job Line Amount (LCY)] [decimal](38, 20) NOT NULL,
    [Job Line Disc_ Amount (LCY)] [decimal](38, 20) NOT NULL,
    [Job Currency Factor] [decimal](38, 20) NOT NULL,
    [Job Currency Code] [varchar](20) NOT NULL,
    [Prod_ Order No_] [varchar](20) NOT NULL,
    [Variant Code] [varchar](10) NOT NULL,
    [Bin Code] [varchar](20) NOT NULL,
    [Qty_ per Unit of Measure] [decimal](38, 20) NOT NULL,
    [Unit of Measure Code] [varchar](10) NOT NULL,
    [Quantity (Base)] [decimal](38, 20) NOT NULL,
    [FA Posting Date] [datetime] NOT NULL,
    [FA Posting Type] [int] NOT NULL,
    [Depreciation Book Code] [varchar](10) NOT NULL,
    [Salvage Value] [decimal](38, 20) NOT NULL,
    [Depr_ until FA Posting Date] [tinyint] NOT NULL,
    [Depr_ Acquisition Cost] [tinyint] NOT NULL,
    [Maintenance Code] [varchar](10) NOT NULL,
    [Insurance No_] [varchar](20) NOT NULL,
    [Budgeted FA No_] [varchar](20) NOT NULL,
    [Duplicate in Depreciation Book] [varchar](10) NOT NULL,
    [Use Duplication List] [tinyint] NOT NULL,
    [Responsibility Center] [varchar](10) NOT NULL,
    [Cross-Reference No_] [varchar](20) NOT NULL,
    [Unit of Measure (Cross Ref_)] [varchar](10) NOT NULL,
    [Cross-Reference Type] [int] NOT NULL,
    [Cross-Reference Type No_] [varchar](30) NOT NULL,
    [Item Category Code] [varchar](10) NOT NULL,
    [Nonstock] [tinyint] NOT NULL,
    [Purchasing Code] [varchar](10) NOT NULL,
    [Product Group Code] [varchar](10) NOT NULL,
    [Return Reason Code] [varchar](10) NOT NULL,
    [Allow Condition Disc_] [tinyint] NOT NULL,
    [Allow Prod_ Gr_ Cond_ Disc_] [tinyint] NOT NULL,
    [Condition Discount %] [decimal](38, 20) NOT NULL,
    [Condition Discount Amount] [decimal](38, 20) NOT NULL,
    [Prod_ Gr_ Cond_ Disc_ %] [decimal](38, 20) NOT NULL,
    [Prod_ Gr_ Cond_ Disc_ Amount] [decimal](38, 20) NOT NULL,
    [Vend__Item Discount %] [decimal](38, 20) NOT NULL,
    [Vend__Item Disc_ Amount] [decimal](38, 20) NOT NULL,
    [Order Type Code] [varchar](10) NOT NULL,
    [Price Factor] [decimal](38, 20) NOT NULL,
    [Unit Cost (Unit of Measure)] [decimal](38, 20) NOT NULL,
    [Unit of Meas_ Code (Unit Cost)] [varchar](10) NOT NULL,
    [Bonus Code] [varchar](10) NOT NULL,
    [Item Bonus Group] [varchar](10) NOT NULL,
    [Allow Bonus (Direct Unit Cost)] [tinyint] NOT NULL,
    [Allow Bonus] [tinyint] NOT NULL,
    [Shipping No_] [varchar](20) NOT NULL,
    [Cost Component] [varchar](10) NOT NULL,
    [Cost Allocation] [tinyint] NOT NULL,
    [Min_ Order Amount (LC)] [decimal](38, 20) NOT NULL,
    [Min_ Order Quantity (Base)] [decimal](38, 20) NOT NULL,
    [Unit Volume (Total)] [decimal](38, 20) NOT NULL,
    [Line Amount (LC)] [decimal](38, 20) NOT NULL,
    [Unit of Measure (UOM)] [varchar](10) NOT NULL,
    [Quantity (UOM)] [decimal](38, 20) NOT NULL,
    [Original Quantity] [decimal](38, 20) NOT NULL,
    [Cancelled] [tinyint] NOT NULL,
    [Order No_] [varchar](20) NOT NULL,
    [Order Line No_] [int] NOT NULL,
    [Source Line No_] [int] NOT NULL,
    [Waste Disposal Fee] [tinyint] NOT NULL,
    [Price Unit of Measure Code] [varchar](10) NOT NULL,
    [Price Unit of Measure] [varchar](10) NOT NULL,
    [Qty_ per Price UOM] [decimal](38, 20) NOT NULL,
    [Direct Unit Cost (Price)] [decimal](38, 20) NOT NULL,
    [Routing No_] [varchar](20) NOT NULL,
    [Operation No_] [varchar](10) NOT NULL,
    [Work Center No_] [varchar](20) NOT NULL,
    [Prod_ Order Line No_] [int] NOT NULL,
    [Overhead Rate] [decimal](38, 20) NOT NULL,
    [Routing Reference No_] [int] NOT NULL,
 CONSTRAINT [Verploegen POC$Purch_ Inv_ Line$0] PRIMARY KEY CLUSTERED 
(
    [Document No_] ASC,
    [Line No_] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [Data Filegroup 1]
) ON [Data Filegroup 1]

GO

SET ANSI_PADDING OFF
GO

这是供应商表:

USE [VERPLOEGEN-NAV2009-LIVE]
GO

/****** Object:  Table [dbo].[Verploegen POC$Vendor]    Script Date: 08/18/2016 23:43:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Verploegen POC$Vendor](
    [timestamp] [timestamp] NOT NULL,
    [No_] [varchar](20) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Search Name] [varchar](50) NOT NULL,
    [Name 2] [varchar](50) NOT NULL,
    [Address] [varchar](50) NOT NULL,
    [Address 2] [varchar](50) NOT NULL,
    [City] [varchar](30) NOT NULL,
    [Contact] [varchar](50) NOT NULL,
    [Phone No_] [varchar](30) NOT NULL,
    [Telex No_] [varchar](20) NOT NULL,
    [Our Account No_] [varchar](20) NOT NULL,
    [Territory Code] [varchar](10) NOT NULL,
    [Global Dimension 1 Code] [varchar](20) NOT NULL,
    [Global Dimension 2 Code] [varchar](20) NOT NULL,
    [Budgeted Amount] [decimal](38, 20) NOT NULL,
    [Vendor Posting Group] [varchar](10) NOT NULL,
    [Currency Code] [varchar](10) NOT NULL,
    [Language Code] [varchar](10) NOT NULL,
    [Statistics Group] [int] NOT NULL,
    [Payment Terms Code] [varchar](10) NOT NULL,
    [Fin_ Charge Terms Code] [varchar](10) NOT NULL,
    [Purchaser Code] [varchar](10) NOT NULL,
    [Shipment Method Code] [varchar](10) NOT NULL,
    [Shipping Agent Code] [varchar](10) NOT NULL,
    [Invoice Disc_ Code] [varchar](20) NOT NULL,
    [Country_Region Code] [varchar](10) NOT NULL,
    [Blocked] [int] NOT NULL,
    [Pay-to Vendor No_] [varchar](20) NOT NULL,
    [Priority] [int] NOT NULL,
    [Payment Method Code] [varchar](10) NOT NULL,
    [Last Date Modified] [datetime] NOT NULL,
    [Application Method] [int] NOT NULL,
    [Prices Including VAT] [tinyint] NOT NULL,
    [Fax No_] [varchar](30) NOT NULL,
    [Telex Answer Back] [varchar](20) NOT NULL,
    [VAT Registration No_] [varchar](20) NOT NULL,
    [Gen_ Bus_ Posting Group] [varchar](10) NOT NULL,
    [Picture] [image] NULL,
    [Post Code] [varchar](20) NOT NULL,
    [County] [varchar](30) NOT NULL,
    [E-Mail] [varchar](80) NOT NULL,
    [Home Page] [varchar](80) NOT NULL,
    [No_ Series] [varchar](10) NOT NULL,
    [Tax Area Code] [varchar](20) NOT NULL,
    [Tax Liable] [tinyint] NOT NULL,
    [VAT Bus_ Posting Group] [varchar](10) NOT NULL,
    [Block Payment Tolerance] [tinyint] NOT NULL,
    [IC Partner Code] [varchar](20) NOT NULL,
    [Prepayment %] [decimal](38, 20) NOT NULL,
    [Primary Contact No_] [varchar](20) NOT NULL,
    [Responsibility Center] [varchar](10) NOT NULL,
    [Location Code] [varchar](10) NOT NULL,
    [Lead Time Calculation] [varchar](32) NOT NULL,
    [Base Calendar Code] [varchar](10) NOT NULL,
    [Chamber of Commerce] [varchar](11) NOT NULL,
    [Prices on order] [tinyint] NOT NULL,
    [Tradium Account] [int] NOT NULL,
    [EAN adrescode] [varchar](13) NOT NULL,
    [Manufacturer] [tinyint] NOT NULL,
    [Transaction Mode Code] [varchar](20) NOT NULL,
    [Bank Account Code] [varchar](10) NOT NULL,
    [Freight-free Limit] [decimal](38, 20) NOT NULL,
    [Minimum Order Amount] [decimal](38, 20) NOT NULL,
    [Find Best Price] [tinyint] NOT NULL,
    [Discount Calculation] [int] NOT NULL,
    [Print Conditions] [tinyint] NOT NULL,
    [No Prices on Order] [tinyint] NOT NULL,
    [Vendor Bonus Group] [varchar](10) NOT NULL,
    [Cost Allocation] [tinyint] NOT NULL,
    [Minimum Order Volume] [decimal](38, 20) NOT NULL,
    [Apply Waste Disposal Fee] [tinyint] NOT NULL,
    [Purchase budget] [decimal](38, 20) NOT NULL,
    [Credit Limit] [decimal](38, 20) NOT NULL,
    [Performance Score Mandatory] [tinyint] NOT NULL,
    [Melding] [varchar](30) NOT NULL,
    [Partner Type] [int] NOT NULL,
    [2BA] [tinyint] NOT NULL,
    [Traffic Light Image Solvency] [image] NULL,
 CONSTRAINT [Verploegen POC$Vendor$0] PRIMARY KEY CLUSTERED 
(
    [No_] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [Data Filegroup 1]
) ON [Data Filegroup 1] TEXTIMAGE_ON [Data Filegroup 1]

GO

SET ANSI_PADDING OFF
GO

我有这个:

SELECT VPL.[Location Code], sum(VPL.Amount)  as totaal, VPL.[Buy-from Vendor No_]   --, V.[Freight-free Limit]
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
WHERE      VPL.[Buy-from Vendor No_] = '3929' AND VPL.Type = 2 AND [Posting Date] BETWEEN '2016-01-01' AND '2016-12-31' 
GROUP by VPL.[Location Code], VPL.[Buy-from Vendor No_]--, VPL.[Vendor Item No_]
ORDER BY VPL.[Location Code]

结果如下:

AM  1585.30000000000000000000   3929
DB  1865.91000000000000000000   3929
DL  3652.43000000000000000000   3929
RD  1599.32000000000000000000   3929
ZM  571.54000000000000000000    3929

但是如何实现这个特性: 无运费限额

1 个答案:

答案 0 :(得分:0)

这就是诀窍:

use [VERPLOEGEN-NAV2009-LIVE]
SELECT VPL.[Location Code], 
       SUM(VPL.Amount)  AS Total, 
       V.[Freight-free Limit]
FROM [Verploegen POC$Purch_ Inv_ Line] VPL
 JOIN [Verploegen POC$Vendor] V
     ON VPL.[Buy-from Vendor No_] = V.No_
--WHERE [Buy-from Vendor No_] =  '3929' -- is empty??' --'3932' --'3929' --'3923' --'3904' --'3885' --'3820' --'3894' 
WHERE VPL.[Buy-from Vendor No_] = '3929' 
      AND VPL.Type = 2 AND ([Posting Date] BETWEEN '2016-01-01' AND '2016-12-31') 
GROUP BY VPL.[Location Code], V.[Freight-free Limit]  --, VPL.[Direct Unit Cost]

我只是使用了错误的属性