2

3つのテーブルを結合するには?

select * 
from tblcustomer as cust 
    inner join (tblamountdetails as det on det.[CustomerID]=cust.[CustomerID]) 
    inner join (select cash.AccountID, sum(Amount) as [Paid Amount] 
                from tblcashdetails as cash 
                group by cash.AccountID) as cash2 
        on cash2.AccountID=det.AccountID

テーブル形式:

1) cutomertable:
    customerid | customername | Address | phone
        1            arun       palani    1212112221
        2            aaa        sssss    123123123

2)Amountdetailtable:
    AccountID | customerid | Total amount | Daily Amount
        1            1            12000        120

3)cashtable : 
    AccountID |   customerid | amount(given day by day)
        1            1                120
        1            1                120

最後に私はtisのようにしたい....

    customerid | customername |AccountID| totalamount | daily amount | amount(given)
        1            arun          1        12000            120         240(this is sum of amount in table 3 where custid=1)
4

3 に答える 3

3
select 
    cust.customerid,
    cust.customername,
    amt.AccountID,
    amt.[Total amount],
    amt.[Daily Amount],
    t.amountgiven
from cutomertable cust
inner join Amountdetailtable amt on cust.customerid=amt.customerid
inner join (select SUM(amount) amountgiven,customerid from cashtable group by customerid)t 
on t.customerid=cust.customerid

SQL フィドル

Fiddle には多くの時間がかかりました

于 2013-05-23T08:19:44.943 に答える
0

回答: SELECT DISTINCTROW tblamountdetails.CustomerID,tblcustomer.[Customer Name],tblamountdetails.AccountID,tblamountdetails.[Total Amount], tblamountdetails.[Daily Amount],Sum(tblcashdetails.Amount) AS [Amount given],(tblamountdetails.[Total Amount]-[Amount given]) AS [Balance] FROM (tblcustomer RIGHT JOIN tblamountdetails ON tblcustomer.[CustomerID] = tblamountdetails.[CustomerID]) LEFT JOIN tblcashdetails ON tblamountdetails.[AccountID] = tblcashdetails.[AccountID] GROUP BY tblamountdetails. AccountID, tblamountdetails.CustomerID, tblamountdetails.[合計金額], tblamountdetails.[日額], tblcustomer.[顧客名]"

于 2013-06-11T11:19:35.030 に答える
0

これを試してみてください -

SELECT *
FROM tblcustomer cust
INNER JOIN tblamountdetails det ON det.[CustomerID] = cust.[CustomerID]
INNER JOIN (
    SELECT 
          cash.AccountID
        , [Paid Amount] = SUM(Amount) 
    FROM tblcashdetails cash
    GROUP BY cash.AccountID
) cash2 ON cash2.AccountID = det.AccountID
于 2013-05-23T07:15:00.973 に答える