2012-11-27 8 views
0

카테고리에있는 공급 업체의 결과가있는 매트릭스 테이블을 표시하려고합니다. 마찬가지로 :행 당 Mysql 하위 쿼리

SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0) 
FROM transaction t 
JOIN office_per_transaction opt 
ON t.id = opt.transaction_id 
JOIN_vendor_per_transaction ppt 
ON t.id = ppt.transaction_id 
AND ppt.vendor_id = 2 
RIGHT JOIN unit_type ut 
ON t.unit_type_id = ut.id 
AND transaction_end_week BETWEEN 0 AND 14 
AND YEAR(transaction_end_date) = 2012 
GROUP BY ut.id 

나에게 다음과 같은 결과 제공 :

+-------------+---------+ 
| Category | Result | 
+-------------+---------+ 
| Category 1 | 56890 | 
| Category 2 | 4000 | 
|  ... | ... | 
+-------------+---------+ 

지금 내가이 쿼리의 연결을보고 싶습니다를

+-----------+-------------+-------------+------+ 
|   | Category 1 | Category 2 | ... | 
+-----------+-------------+-------------+------+ 
| Vendor 1 |  8900 |   0 | ... | 
| Vendor 2 |  56890 |  4000 | ... | 
|  ... |  ... |  ... | ... | 
+-----------+-------------+-------------+------+ 

이미 하나 개의 공급 업체에 대한 쿼리를 만들어 주어진 사무실에서 벤더 당.

먼저 AND ppt.vendor_id = 2AND ppt.vendor_id IN (SELECT id FROM vendor WHERE office_id = 1)으로 바꾸려고 시도했지만 그 결과 내 사무실에있는 모든 공급 업체의 총 결과가 나왔습니다.

두 번째로 하위 쿼리를 사용해 보았습니다. 하지만 그건 알 수없는 열이라는 오류를 내게했습니다.

+-----------+--------- -------+ 
| Vendor | Results   | 
+-----------+-----------------+ 
| Vendor 1 | 8900,0,...  | 
| Vendor 2 | 56890,4000,... | 
| ...  | ...,...,...  | 
+-----------+-----------------+ 

내가 비현실적인 무언가를 시도하고 있는데 오히려 업체 당 쿼리 이상 수행해야합니다

은 무엇 내가 달성하고자하는 것은 같은 결과 집합입니다. 아니면 내가 여기서 뭔가를 놓치고 있니?

@SashiKant 나는 마지막 날 많은 쿼리를 시도했다, 그러나이 같은했다 :

SELECT v.id, GROUP_CONCAT(r.result) FROM vendor v, 
    (SELECT ut.unit_name, IFNULL(SUM(commission_fix_out),0) as result 
    FROM transaction t JOIN office_per_transaction opt ON t.id = opt.transaction_id 
    JOIN vendor_per_transaction ppt ON t.id = ppt.transaction_id 
    AND ppt.vendor_id = p.id 
    RIGHT JOIN unit_type ut ON unit_type_id = ut.id 
    AND transaction_end_week BETWEEN 0 AND 14 
    AND YEAR(transaction_end_date) = 2012 GROUP BY ut.id) as r 
+0

두 번째 쿼리가 무엇을 선택합니까? –

답변

0

당신은 아마 오히려 단일 벤더보다 (당신이 원하는 업체를 얻기 위해 공급 업체의 테이블에 가입해야). 또한 GROUP BY에 더 많은 절이 필요하다. (실제로 GROUP BY 절의 SELECT에서 모든 비 집계 열을 사용해야한다. mysql은 반대하지 않는다. (그리 오타 실례 시험)이 같은

뭔가

SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0) 
FROM transaction t 
INNER JOIN office_per_transaction opt 
ON t.id = opt.transaction_id 
INNER JOIN_vendor_per_transaction ppt 
ON t.id = ppt.transaction_id 
INNER JOIN vendor ve 
ON ve.id = ppt.vendor_id 
RIGHT JOIN unit_type ut 
ON t.unit_type_id = ut.id 
AND transaction_end_week BETWEEN 0 AND 14 
AND YEAR(transaction_end_date) = 2012 
WHERE ve.office_id = 1 
GROUP BY vendor_id, ut.id, ut.unit_name 
ORDER BY vendor_id, ut.id, ut.unit_name 

해당 공급 업체에 대한 모든 금액이 같은 무언가에 대해 하나의 열이 업체 당 행을합니다.

SELECT vendor_id, GROUP_CONCAT(CommisionAmount) 
FROM (
SELECT vendor_id, ut.id, ut.unit_name, IFNULL(SUM(commission_fix_out),0) AS CommisionAmount 
FROM transaction t 
INNER JOIN office_per_transaction opt 
ON t.id = opt.transaction_id 
INNER JOIN_vendor_per_transaction ppt 
ON t.id = ppt.transaction_id 
INNER JOIN vendor ve 
ON ve.id = ppt.vendor_id 
RIGHT JOIN unit_type ut 
ON t.unit_type_id = ut.id 
AND transaction_end_week BETWEEN 0 AND 14 
AND YEAR(transaction_end_date) = 2012 
WHERE ve.office_id = 1 
GROUP BY vendor_id, ut.id, ut.unit_name 
ORDER BY vendor_id, ut.id, ut.unit_name) Sub1 
+0

이것은 벤더에 대한 각 카테고리의 결과를 제공하지 않으므로 도움이되지 않습니다. 그러나 어쨌든 고마워요. –

+0

하지만 공급 업체 당 카테고리 당 하나의 행이어야합니다. 이는 사용중인 레이블에서 공급 업체별 행과 카테고리 별 열로 쉽게 정렬 될 수 있습니다. 그것을 연결시켜 다시 가져 오려면 – Kickstart

0

group_concat을 사용할 수 있습니다.

의사 SQL은 :

select vendor, group_concat(result) 
from mytable 
group by vendor 

당신은 뷰를 작성하여 "단순화"수 있습니다. 나는 당신의 테이블 구조를 알 수 없기 때문에, 이것은 단지

create view commissions as 
    SELECT ppt.vendor_id as vendor, ut.unit_name as category, 
      IFNULL(SUM(commission_fix_out),0) as commission 
    FROM transaction t 
    JOIN office_per_transaction opt 
    ON t.id = opt.transaction_id 
    JOIN_vendor_per_transaction ppt 
    ON t.id = ppt.transaction_id 
    RIGHT JOIN unit_type ut 
    ON t.unit_type_id = ut.id 
    AND transaction_end_week BETWEEN 0 AND 14 
    AND YEAR(transaction_end_date) = 2012 
    GROUP BY ppt.vendor_id, ut.id; 

을 추측하고

select vendor, group_concat(commission) 
from commissions 
group by vendor; 
+0

위의 추가 비트를 완료해야합니다. 해당 뷰는 공급 업체 2의 결과 만 가져옵니다. 공급 업체 ID가 가변적 인 방법이 있습니까? –

+0

@ JGhyllebert 죄송합니다, 방금 귀하의 선택을 맹목적으로 복사했습니다. 이제'ppt.vendor_id = 2'를 제거했지만'transaction_end_week' 및'transaction_end_date'에서는 여전히 제한이 있습니다. 필요에 따라보기를 조정하십시오. –