从SQL Server中的3个表中检索数据

时间:2012-01-26 11:00:03

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

查询

SELECT     
    dbo.SI_Customer.cst_Name, dbo.SI_InvoiceMaster.invcm_DocNo,    
    dbo.SI_InvoiceMaster.invcm_Date, dbo.SI_InvoiceMaster.invcm_TotalAmount, 
    dbo.SI_ReceiptMaster.recm_DocNo, dbo.SI_ReceiptMaster.recm_Date,    
    dbo.SI_ReceiptMaster.recm_TotalAmount
FROM         
    dbo.SI_Customer 
FULL OUTER JOIN
    dbo.SI_ReceiptMaster ON dbo.SI_Customer.cst_Code = dbo.SI_ReceiptMaster.cst_Code 
FULL OUTER JOIN
    dbo.SI_InvoiceMaster ON dbo.SI_Customer.cst_Code = dbo.SI_InvoiceMaster.cst_Code
WHERE     
    (dbo.SI_Customer.cst_Code = '001813')

以下是此查询的结果 - 我不想重复这些值

enter image description here

2 个答案:

答案 0 :(得分:0)

由于这些行包含不同的数据,即日期和日期两侧的数字,每一行都是唯一的,因此将显示所有选定的列。

您可以删除部分列并执行分组和SUM或失败,您可以在BIDS和按日期生成报告,并将您不希望重复的其他数据重复为CHILD组。

答案 1 :(得分:0)

“傻瓜涌入” - 试试:

SELECT     
    c.cst_Name, 
    m.invcm_DocNo,    
    m.invcm_Date, 
    m.invcm_TotalAmount, 
    m.recm_DocNo, 
    m.recm_Date,    
    m.recm_TotalAmount
FROM dbo.SI_Customer c
LEFT JOIN (SELECT cst_Code,
                  invcm_DocNo,
                  invcm_Date, 
                  invcm_TotalAmount, 
                  CONVERT(varchar(10), null) recm_DocNo, 
                  CONVERT(datetime, null) recm_Date,    
                  CONVERT(decimal(25,8), null) recm_TotalAmount
           from dbo.SI_InvoiceMaster
           where cst_Code = '001813'
           union all
           SELECT cst_Code,
                  CONVERT(varchar(10), null) invcm_DocNo,
                  CONVERT(datetime, null) invcm_Date, 
                  CONVERT(decimal(25,8), null) invcm_TotalAmount, 
                  recm_DocNo, 
                  recm_Date,    
                  recm_TotalAmount
           from dbo.SI_ReceiptMaster
           where cst_Code = '001813') m
       ON c.cst_Code = m.cst_Code
WHERE c.cst_Code = '001813'