我有2個表格,Customer和Trans。如何以更短的方式解決這個SQL查詢?
Customer
---------
c_id | cName | cSurname | cPbx | cGsm | cLastVisitDate
Trans
------
t_id | c_id | tDate | tPaymentAmount | tSaleAmount
我想選擇債務大於零且自指定日期以來未進行交易的客戶。
例如「名單誰擁有的債務,自20.01.2012沒有訪問過的客戶」
這是我試圖解決,但它不工作。
select Customer.c_id, SUM (saleAmount - paymentAmount) as totalDebt,
cLastVisitDate,
cName,
cSurName,
cPbx,
cGsm
from Customer, Trans
where customer.c_id = trans.c_id AND cLastVisitDate < ?
這給出了以下錯誤。
'CUSTOMER.C_ID'無效。當SELECT列表至少包含一個聚合表時,所有條目必須是有效的聚合表達式。
我找到了一個解決方案,以及將查詢與非聚合列cLastVisitDate cName cSurname cPbx cGsm
分組。
select Customer.c_id, SUM (saleAmount - paymentAmount) as totalDebt,
cLastVisitDate,
cName,
cSurName,
cPbx,
cGsm
from Customer, Trans
where customer.c_id = trans.c_id AND cLastVisitDate < ?
group by customer.c_id, cLastVisitDate cName cSurname cPbx cGsm
此解決方案的作品,但它似乎沒有優雅的方式做it.Is有沒有更優雅,更簡單的方式做這個任務?
這有什麼不好看的。我無法想象一個相當可讀的等價查詢。 – Pete
使用所有非聚合列進行分組看起來並不是最好的方式。 – user1906555
我想你還需要由customer.c_id字段進行分組。據我所知,查詢中出現的所有非聚合列需要按照它們分組,這是執行它的通常方式。簡化分組代碼(但在另一部分中使其複雜化)的一種方法是將聚合函數封裝在子查詢中。然後,您可以將聚合函數的結果用作新列。 – Miquel