简体   繁体   English

在没有子查询的 mysql 中获取比率

[英]Getting ratios in mysql without subqueries

I would like to run a query from the tables below that gives me the ratio of the amount paid from the executive offices to the total amount due:我想从下表中运行一个查询,得到从执行办公室支付的金额与应付总金额的比率:

 CREATE TABLE  offices (
   name VARCHAR(255),
   ID VARCHAR(255),
  level VARCHAR(255)
 );



 INSERT INTO offices (name,ID,level)
   VALUES 
  ('chairman', 'ad1', 'admin'),
  ('MD', 'ad2', 'admin'),
  ('CEO', 'ad3', 'admin'),
  ('president', 'ex1', 'exec' ),
  ('VP', 'ex2', 'exec'),
  ('GM', 'ex3', 'exec'),
  ('HOD', 'ex4', 'exec');

CREATE TABLE  cheques (
  payee VARCHAR(255) ,
 officeID VARCHAR(255),
 amount INT(),
 status VARCHAR(255) 
 );


INSERT INTO cheques
 VALUES ('john', 'ad2', '100',  'paid'),
 ('john',   'ad3',  '50',   'owed'),
 ('john',   'ex3',  '80',   'paid'),
 ('john',   'ex4',  '150',  'owed'),
 ('john',   'ex1',  '35',   'paid'),
('mary',    'ad1',  '200',  'paid'),
('mary',    'ad3',  '90',   'owed'),
('mary',    'ex2',  '110',  'paid'),
('mary',    'ex4',  '40',   'owed'),
('mary',    'ex1',  '60',   'paid'),
('jane',    'ad1',  '75',   'paid'),
('jane',    'ad3',  '120',  'paid');

The expected result is as below:预期结果如下:

data.frame(
 payee=c("john","mary"),
 totalpaid=c(115,170),
 status=c("paid", "paid"),
 totalsdue = c(415, 500),
 ratio=(0.28, 0.34)
   )

I have been able to get totalpaid from exec offices as below我已经能够从以下执行办公室获得总薪水

  SELECT c.payee, SUM(c.amount) as totalpaid, c.status
   FROM cheques c
   JOIN offices o
   ON c.officeID = o.ID
  WHERE 
   o.level LIKE '%ex%'
 AND 
   c.status LIKE '%paid%'
 GROUP BY payee 

and the totalsdue as below和总欠款如下

   SELECT  c.payee, SUM(c.amount) as totalsdue
     FROM cheques c
      GROUP BY payee

What I have tried without success:我没有成功的尝试:

  SELECT  
   c.payee, SUM(c.amount) as totalsdue
      totalsdue/totalpaid as ratio
      FROM cheques c
      WHERE c.payee IN
        (SELECT c.payee, SUM(c.amount) as totalpaid, c.status
           FROM cheques c
           JOIN offices o
           ON c.officeID = o.ID
           WHERE 
             o.level LIKE '%ex%'
           AND 
             c.status LIKE '%paid%'
          GROUP BY payee )
      GROUP BY payee

How to get the expected results with a single query.如何通过单个查询获得预期结果。

I would like to run a query from the tables below that gives me the ratio of the amount paid from the executive offices to the total amount due.我想从下表中运行一个查询,得到从执行办公室支付的金额与应付总金额的比率。

I think you just want window functions:我想你只想要 window 功能:

SELECT c.payee, SUM(c.amount) as totalsdue,
       SUM(c.amount) / SUM(SUM(c.amount)) OVER () as ratio
FROM cheques c
GROUP BY payee;

You have other conditions in your queries, but they are not explained in the question.您的查询中有其他条件,但问题中没有解释。

For the result you could use the subquery in JOIN对于结果,您可以在 JOIN 中使用子查询

  SELECT t1.payee, t1.totalsdue, t1.totalsdue/t2.totalpaid 
  from  (
      SELECT  c.payee, SUM(c.amount) as totalsdue
      FROM cheques c
      GROUP BY payee
  ) t1
  INNER JOIN  (
    SELECT c.payee, SUM(c.amount) as totalpaid, c.status
    FROM cheques c
    JOIN offices o  ON c.officeID = o.ID
    WHERE o.level LIKE '%ex%'
    AND   c.status LIKE '%paid%'
    GROUP BY payee 
 ) t2 on t1.payee = t2.payee 

for better performance..为了更好的表现..
check if you really need like and wildchar matching or you can result ve the query with exact matching检查你是否真的需要 like 和 wildchar 匹配,或者你可以得到完全匹配的查询

and be sure you have proper index on columns potenzially involved in join and where并确保您对可能参与连接的列有正确的索引以及在哪里

table cheques column ( officeID, payee)表检查列(officeID、payee)

or if the column status allow exact match column ( officeID, status, payee)或者如果列状态允许完全匹配列(officeID、status、payee)

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

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