如何从3个表中获取数据?

时间:2015-03-02 11:51:05

标签: sql inner-join

我有3张桌子

collecton, paymentdata, payment

Payment表是计算表,它只有一个产品数据

因此,rcvamtrestamt来自付款表

我有以下数据

收集:

id(PK) clientid company     Client  product     total   note
1      2001     Company1    Client1 Product1    50000   note1
2      2002     Company2    Client2 Product2    60000   note2
3      2003     Company3    Client3 Product3    70000   note3

PaymentData:

wid(PK)wcid(FK) clientid product    rcvamt  restamt rcvdate     nxtdate   Note
    1      1        2001 Product1   500     49500   10-1-2015   11-2-2015 abc1
    2      1        2001 Product1   800     48700   11-2-2015   12-3-2015 xyz1
    3      2        2002 Product2   1500    58500   5-3-2015    6-4-2015  qwe1

付款

id(PK)  wid(FK) clientid    product     rcvamt  restamt
1       2       2001        Product1    1300    48700
2       3       2002        Product2    1500    58500

我想要显示像

这样的报告
clientid company    procudt   total rcvamt  restamt rcvdate     nxtdate   note
2001     Company1   Product1  50000 1300    48700   11-2-2015   12-3-2015 xyz1
2002     Company2   Product2  60000 1500    58500   5-3-2015    6-4-2015  qwe1
2003     Company3   Product3  70000 -       -       -            -        -

3 个答案:

答案 0 :(得分:1)

我试图简化:

SELECT DISTINCT
    C.clientid
, C.company
, C.product
, C.total
, P.rcvamt
, P.restamt
, ( SELECT TOP 1 rcvdate FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
, ( SELECT TOP 1 nxtdate FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
, ( SELECT TOP 1 Note FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
FROM
    Collection C 
        LEFT OUTER JOIN Payment P 
            ON C.clientid = P.clientid
        LEFT OUTER JOIN PaymentData PD
            ON P.clientid = PD.clientid

但我不知道桌子之间的所有关系。

答案 1 :(得分:0)

这样的事情应该有效。您似乎想要restamt上的汇总,其他两个字段都是收到的最后一笔付款。我还假设这是SQL Server,因为你的名字。如果它是不同的数据库,请提供

更新:更改为LEFT JOIN以处理没有产品的客户端3,修复产品中的拼写错误。 SQL小提琴:http://sqlfiddle.com/#!3/8ad566/19/0

SELECT
    c.clientid,
    c.company,
    c.product,
    c.total,
    SUM(pd.rcvamt) AS rcvamt,
    LastPayment.restamt,
    LastPayment.rcvdate,
    LastPayment.nxtDate,
    LastPayment.note
FROM Collection c
    LEFT OUTER JOIN PaymentData pd
        ON pd.wcid = c.id
    LEFT OUTER JOIN (
        SELECT
            wcid,
            restamt,
            rcvdate,
            nxtdate,
            Note,
            ROW_NUMBER() OVER (PARTITION BY wcid ORDER BY rcvdate DESC) AS RowNum
        FROM PaymentData
    ) LastPayment
        ON LastPayment.wcid = c.id
            AND LastPayment.RowNum = 1 -- Get last payment info
GROUP BY
    c.clientid,
    c.company,
    c.product,
    c.total,
    LastPayment.restamt,
    LastPayment.rcvdate,
    LastPayment.nxtDate,
    LastPayment.note
ORDER BY
    c.clientid

答案 2 :(得分:0)

您的问题的答案可能如下所示

Select Collection.clientid
      ,Collection.company
      ,Collection.product
      ,Collection.total
      ,Payment.rcvamt
      ,Payment.restamt
      ,PaymentData.rcvdate
      ,PaymentData.nxtdate
      ,PaymentData.Note

From PaymentData
Inner Join (Select  wcid
                        ,Max(PaymentData.rcvdate) as rcvdate
                        ,Max(PaymentData.nxtdate) as nxtdate
                  FROM PaymentData 

                  GROUP BY wcid) AS SubSelect ON PaymentData.wcid = SubSelect.wcid
                                             AND PaymentData.rcvdate = SubSelect.rcvdate
                                             AND PaymentData.nxtdate = SubSelect.nxtdate

Inner Join Payment on PaymentData.wcid = Payment.id
RIGHT OUTER JOIN Collection ON PaymentData.clientid = Collection.clientid 

这里sqlfiddle来证明我的答案。