简体   繁体   English

MySql中两个表的交叉表

[英]cross table from two tables in MySql

I have two tables, one is *parts_raised* and another is *parts_detail*. 我有两个表,一个是* parts_raised *,另一个是* parts_detail *。

parts_raised: parts_raised:

SN(int),Job_Number(int),Category(varchar),Part_code(int),technician(varchar),Time      (timestamp),

Parts_detail: Parts_detail:

Part_code(int),Value(int),Descriptions(text),

part_code is same in both table. 两个表中的part_code相同。

How can I write query for achieving total count of jobs,and their total cost per technician on daily basis. 如何编写查询以获取工作总数及其每天每位技术人员的总成本。

technician    day1                             day2            
              Total Jobs     total cost        Total Jobs     total cost   

Technician-1  4                 153              5              253
Technician-2  7                 352              2              256

How can achieve this or suggest any other method for getting same result? 如何实现这一目标或建议其他任何方法以获得相同的结果?

Does this do it? 这样做吗?

SELECT
  technician, Job_day, SUM(Value)
FROM
(
  SELECT
    pr.technician, DAY(pr.Time) AS Job_day, pd.Value 
  FROM
    parts_raised AS pr
  JOIN
    Parts_detail AS pd
  ON
    pd.Part_code = pr.Part_code
) AS tempId
GROUP BY
  technician, Job_day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM