SQl结果重复

时间:2015-03-16 11:41:58

标签: sql-server

我有3张桌子

CREATE TABLE [Distributors]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](max) NULL,
)

INSERT INTO [Distributors]([Name]) VALUES ('A')
INSERT INTO [Distributors]([Name]) VALUES ('B')

CREATE TABLE [Points]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Distributor_ID] [int] NULL,
    [Point_Count] [int] NULL,
    [Point_Type] [nvarchar](max) NULL,
    [Point_Level] [int] NULL,
    [From_Distributor] [int] NULL,
    [Is_Calculated] [bit] NULL,
    [Point_Date] [datetime]
)

INSERT INTO [Points] ([Distributor_ID], [Point_Count], [Point_Type],[Point_Level], [From_Distributor], [Is_Calculated], [Point_Date])     
VALUES(1, 1, 'AN', '', 2, 'True', '2015-02-16')
INSERT INTO [Points] ([Distributor_ID], [Point_Count], [Point_Type],[Point_Level], [From_Distributor], [Is_Calculated], [Point_Date])     
VALUES(1, 1, 'Offer', '', 2, 'True', '2015-02-16')`

CREATE TABLE [dbo].[Profit]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Distributor_ID] [int] NULL,
    [Profit] [decimal](18, 2) NULL,
    [Percentage] [int] NULL,
    [Profit_Date] [datetime] NULL,
    [From_Distributor_ID] [int] NULL,
    [Is_Paid] [bit] NULL,
    [Paid_By] [int] NULL
)

INSERT INTO [Profit]([Distributor_ID], [Profit], [Percentage],[Profit_Date], [From_Distributor_ID], [Is_Paid], [Paid_By]) 
VALUES (1, '80', 10, '2015-02-16', 2, 'False', 0)

INSERT INTO [Profit]([Distributor_ID], [Profit], [Percentage],[Profit_Date], [From_Distributor_ID], [Is_Paid], [Paid_By]) 
VALUES (1, '40', 5, '2015-02-16', 2, 'False', 0)

当我使用join语句执行select查询时,我面临重复

SELECT distinct Points.[ID]
  ,Points.[Distributor_ID]
  ,[Point_Count]
  ,[Point_Type]
  ,[Point_Level]
  ,[From_Distributor]
  ,[Is_Calculated]
  ,[Point_Date]
  --,Profit.Profit
FROM 
   [Points]
INNER JOIN 
   Profit ON Points.From_Distributor = Profit.From_Distributor_ID 
          AND Points.Distributor_ID = Profit.Distributor_ID
WHERE 
    (Points.[Distributor_ID] = 1) 
    AND ([Point_Date] >= '2/16/2015') 
    AND ([Point_Date] <= '2/28/2015') 

结果是两行

使用&#34; Profit.Profit&#34;在select语句中它有4行
我认为加入声明中的问题 我需要你的建议

0 个答案:

没有答案