[英]mysql query issue with calculated fields
我想通過人員生成選擇客戶交易。
它應該顯示我所有回電,約會,已發送電子郵件或特定座席的其他任何狀態的計數(由ID生成)。
架構:
CREATE TABLE IF NOT EXISTS `client_transaction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client_id` int(11) NOT NULL,
`generated_by_id` int(11) NOT NULL,
`schedule_for_id` int(11) NOT NULL,
`transaction_type` varchar(50) NOT NULL,
`target_date_time` datetime NOT NULL,
`date_added` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`)
)
查詢:
SELECT generated_by_id as agent_id, count(*) as call_backs
FROM client_transaction
WHERE transaction_type='CALL_BACK'
GROUP BY generated_by_id
我已經創建了一種狀態的查詢。 但是,每個狀態我都需要它。 我也想從其他座席表中獲取座席名稱。 獲取所有適當的數據后,如何離開代理表?
謝謝。
只需將transaction_type添加為查詢中的字段,然后對其進行另一級別的分組:
SELECT
generated_by_id AS agent_id,
transaction_type,
count(*) AS transaction_count
FROM client_transaction
GROUP BY generated_by_id, transaction_type
/* sort in some manner */
ORDER BY generated_by_id ASC, transaction_type ASC
通過連接到代理表進行類似查詢的示例可能類似於:
SELECT
/* I am assuming field names on agent table
* and am guessing you want to join to be able
* to get something like agent name in this query
*/
a.id AS `agent_id`,
a.name AS `agent_name`,
ct.transaction_type AS `transaction_type`,
count(*) AS `transaction_count`
FROM agent AS a
LEFT JOIN client_transaction AS ct
ON a.id = ct.generated_by_id
GROUP BY `agent_id`, `transaction_type`
/* sort in some manner */
ORDER BY `agent_id` ASC, `transaction_type` ASC
每個狀態我都需要
按交易類型分組。
SELECT generated_by_id as agent_id, transaction_type, count(*) as call_backs
FROM client_transaction
GROUP BY generated_by_id, transaction_type
我怎樣才能離開與代理表?
與LEFT JOIN
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.