列返回空值而不是值

时间:2016-08-11 11:21:32

标签: sql sql-server sql-server-2005

我有一个查询,其中我已经为我的要求合并了一个子查询。

这里的查询是。

select a.mkey, b.mkey RefMkey, a.doc_no, a.doc_date, 
            e.type_desc DocType, c.first_name + ' ' + c.last_name CurrentUser,
        d.Type_desc Department, b.remarks, 
                (select f.type_desc from type_mst_a where f.Add_IInfo1
                 = b.NStatus_flag and f.type_code = 'S2') currentStatus
           from inward_doc_tracking_hdr a
        inner join inward_doc_tracking_trl b
            on a.mkey = b.ref_mkey
        inner join user_mst c
            on c.mkey = b.CUser_Id  
        inner join type_mst_a d
            on d.master_mkey = b.CDept_Id   
        inner join type_mst_a e
            on e.master_mkey = a.doc_type
        inner join type_mst_a f 
            on f.master_mkey = b.NStatus_flag
where a.mkey = 227394

但是这里发生的事情是,存在值,但我得到NULL

更新

CREATE TABLE [dbo].[Inward_Doc_Tracking_Hdr](
[Mkey] [numeric](18, 0) NOT NULL,
[FModule_ID] [char](1) NOT NULL,
[Comp_Mkey] [numeric](4, 0) NOT NULL,
[Branch_Mkey] [numeric](4, 0) NOT NULL,
[Tran_Type] [varchar](6) NOT NULL,
[Tran_Code] [numeric](8, 0) NOT NULL,
[Tran_No] [numeric](4, 0) NOT NULL,
[Department_ID] [varchar](4) NOT NULL,
[User_Id] [numeric](10, 0) NULL,
[Doc_Type] [numeric](10, 0) NULL,
[Doc_No] [varchar](30) NULL,
[Doc_Date] [datetime] NOT NULL,
[Dispatch_By] [char](1) NOT NULL,
[Status_Flag] [numeric](8, 0) NOT NULL,
[FA_Year] [smallint] NOT NULL,
[Party_Mkey] [numeric](10, 0) NULL,
[To_Department] [numeric](10, 0) NULL,
[To_User] [numeric](10, 0) NULL,
[Inward_Amt] [numeric](18, 3) NULL,
[Ref_No] [varchar](40) NULL,
[Ref_date] [datetime] NULL,
[U_DateTime] [datetime] NOT NULL,
[LastAction_DateTime] [datetime] NULL,
[Remarks] [varchar](255) NULL,
[Delete_Flag] [char](1) NOT NULL,
[Outward_Type] [char](1) NULL,
[Doc_Department] [numeric](10, 0) NULL,
[Party_Name] [varchar](80) NULL,
[Delivered_By] [varchar](30) NULL,
[Doc_Description] [varchar](50) NULL,
[Last_Department] [numeric](10, 0) NULL,
[Last_User] [numeric](10, 0) NULL,
[Approved_Amount] [float] NULL,
[Chq_No] [varchar](50) NULL,
[Chq_dated] [datetime] NULL,
[Chq_Bank] [varchar](40) NULL,
[Chq_Amount] [float] NULL,
[Vendor_MKey] [int] NULL,
[Vendor_Comp_Mkey] [int] NULL,
[Project_Mkey] [numeric](10, 0) NULL,
[Program_mkey] [numeric](10, 0) NULL,
[Payment_MKey] [int] NULL,
[Due_Date] [datetime] NULL,
[Updated_Remarks] [varchar](500) NULL,
[Updated_Bill_no] [varchar](27) NULL,
[Updated_Bill_Date] [datetime] NULL,
[Updated_Bill_Amt] [float] NULL,
[TotalDeductions] [float] NULL,
[Broker_Mkey] [numeric](10, 0) NULL,
[Customer_Mkey] [numeric](10, 0) NULL,
[Payable_Amt] [float] NULL,
[Balance_Amt] [float] NULL,
[Req_Bill_Flag] [char](1) NULL,
[Po_No] [varchar](50) NULL,
[Receipt_No] [varchar](50) NULL,
[Bill_No] [varchar](50) NULL,
[Org_id] [varchar](10) NULL,
[Site_Id] [varchar](50) NULL,
[Site_Name] [varchar](150) NULL,
[Cumulative_Amt] [numeric](18, 2) NULL,
[Email_Id] [varchar](50) NULL,
[Emp_Id] [varchar](6) NULL,
[Disp_through] [varchar](50) NULL,
[Disp_Through_Name] [varchar](100) NULL,
[Last_To_User] [numeric](10, 0) NULL

)ON [PRIMARY]

显示结果的查询是

select type_desc, * from type_mst_a where type_code = 'S2'

6 个答案:

答案 0 :(得分:1)

SELECT a.mkey, 
    b.mkey AS RefMkey, 
    a.doc_no, 
    a.doc_date, 
    e.type_desc AS DocType, 
    c.first_name + ' ' + c.last_name AS CurrentUser,
    d.Type_desc AS Department, 
    b.remarks, 
    docType.type_desc AS  currentStatus
FROM inward_doc_tracking_hdr a
INNER JOIN inward_doc_tracking_trl b
    on a.mkey = b.ref_mkey
INNER JOIN user_mst c
    on c.mkey = b.CUser_Id  
INNER JOIN type_mst_a d
    on d.master_mkey = b.CDept_Id   
INNER JOIN type_mst_a e
    on e.master_mkey = a.doc_type
LEFT JOIN
    (
        SELECT type_desc,
            Add_IInfo1 
        FROM type_mst_a
        WHERE type_code = 'S2'
    ) AS docType
    ON docType.Add_IInfo1 = b.NStatus_flag
WHERE a.mkey = 227394

答案 1 :(得分:0)

您没有定义" f"

select f.type_desc from type_mst_a where f.Add_IInfo1
                 = b.NStatus_flag and f.type_code = 'S2'

答案 2 :(得分:0)

您可以对join本身执行相同的操作,而不是子查询:

select  a.mkey, b.mkey RefMkey, a.doc_no, a.doc_date, 
        e.type_desc DocType, c.first_name + ' ' + c.last_name CurrentUser,
        d.Type_desc Department, b.remarks, f.type_desc AS currentStatus
from inward_doc_tracking_hdr a
inner join inward_doc_tracking_trl b
    on a.mkey = b.ref_mkey
inner join user_mst c
    on c.mkey = b.CUser_Id  
inner join type_mst_a d
    on d.master_mkey = b.CDept_Id   
inner join type_mst_a e
    on e.master_mkey = a.doc_type
inner join type_mst_a f 
    on f.master_mkey = b.NStatus_flag AND f.Add_IInfo1 = b.NStatus_flag AND f.type_code = 'S2'
where a.mkey = 227394

答案 3 :(得分:0)

您的其中一列可能为null,请尝试使用左侧或完全外部联接

select a.mkey, b.mkey RefMkey, a.doc_no, a.doc_date, 
            e.type_desc DocType, c.first_name + ' ' + c.last_name CurrentUser,
        d.Type_desc Department, b.remarks, 
                (select f.type_desc from type_mst_a where f.Add_IInfo1
                 = b.NStatus_flag and f.type_code = 'S2') currentStatus
           from inward_doc_tracking_hdr a
        left join inward_doc_tracking_trl b
            on a.mkey = b.ref_mkey
        left join user_mst c
            on c.mkey = b.CUser_Id  
        left join type_mst_a d
            on d.master_mkey = b.CDept_Id   
        left join type_mst_a e
            on e.master_mkey = a.doc_type
        left join type_mst_a f 
            on f.master_mkey = b.NStatus_flag
where a.mkey = 227394

答案 4 :(得分:0)

您的内部SELECT是所谓的“相关子查询”。

即使数据库中没有存储NULLS,相关子查询也可以在结果中产生NULLS。如果主查询中有行,子查询中的行匹配为零,则NULLS将出现在结果中。

你必须对你的数据做一些侦探工作,看看是否是这种情况。

答案 5 :(得分:0)

这个怎么样:

select a.mkey, b.mkey RefMkey, a.doc_no, a.doc_date, 
   e.type_desc DocType, c.first_name + ' ' + c.last_name CurrentUser,
   d.Type_desc Department, b.remarks, 
   (select type_desc from type_mst_a where Add_IInfo1 = b.NStatus_flag and type_code = 'S2') currentStatus
from inward_doc_tracking_hdr a
inner join inward_doc_tracking_trl b
    on a.mkey = b.ref_mkey
inner join user_mst c
    on c.mkey = b.CUser_Id  
inner join type_mst_a d
    on d.master_mkey = b.CDept_Id   
inner join type_mst_a e
    on e.master_mkey = a.doc_type
inner join type_mst_a f 
    on f.master_mkey = b.NStatus_flag
where a.mkey = 227394

可以改写为:

select a.mkey, b.mkey RefMkey, a.doc_no, a.doc_date, 
   e.type_desc DocType, c.first_name + ' ' + c.last_name CurrentUser,
   d.Type_desc Department, b.remarks, g.type_desc as currentStatus
from inward_doc_tracking_hdr a
inner join inward_doc_tracking_trl b
    on a.mkey = b.ref_mkey
inner join user_mst c
    on c.mkey = b.CUser_Id  
inner join type_mst_a d
    on d.master_mkey = b.CDept_Id   
inner join type_mst_a e
    on e.master_mkey = a.doc_type
inner join type_mst_a f 
    on f.master_mkey = b.NStatus_flag
inner join type_mst_a g 
    on g.Add_IInfo1 = b.NStatus_flag and 
       g.type_code = 'S2'
where a.mkey = 227394