【问题标题】:Count(*) as inside a JOIN on SQL QueryCount(*) 作为 SQL 查询的 JOIN 内
【发布时间】:2017-05-03 02:06:11
【问题描述】:

我对报告进行了以下查询,一切正常,但我需要在报告中添加一个变量,根据“manheim_auction_listings”记录中的记录数汇总返回的每条记录的记录数。我觉得它需要在一个连接中,但是在任何地方我都会使用“COUNT(*) AS num_of_runs”,这似乎使整个查询只返回一行,其中计算查询中的记录总数而不是所有带有变量 num_of_runs 的行以及每个 CAR 记录的“manheim_auction_listings”记录数。

SELECT products.client_id,
    clients.name AS client_name,
    manheim_auction_lanes.lane_number,
    manheim_auction_listings.sequence,
    manheim_auction_listings.gross_sale_price,
    products.asking_price,  products.asking_price_condition, 
    manheim_auctions.auction_date,
    manheim_auctions.auction_number,
    product_purchases.total_spent,
    product_purchases.purchase_price 
FROM manheim_auction_listings
    JOIN cars ON 
    cars.id = manheim_auction_listings.car_id 
    JOIN products ON 
     cars.product_id = products.id 
    JOIN product_purchases ON
     current_product_purchase_id = product_purchases.id
    JOIN manheim_auctions ON 
     manheim_auctions.id = manheim_auction_listings.manheim_auction_id 
    JOIN manheim_auction_lanes ON 
      manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id 
    JOIN clients ON 
     clients.id = products.client_id 
     AND clients.id LIKE $P{LoggedInUserAttribute_ClientID} 
WHERE 
     manheim_auctions.auction_number = $P{SaleNumber} 
     AND manheim_auctions.`year` = $P{SaleYear} 
ORDER BY manheim_auction_lanes.lane_number DESC,
    manheim_auction_listings.sequence DESC

【问题讨论】:

  • 样本数据和期望的结果确实有助于传达您想要做的事情。
  • 这里发生了很多事情。您能否将查询简化为仍能证明问题的更简单的查询?即更少的桌子?
  • 通过您使用的变量,我认为您正在使用 iReport,那么,您为什么不在其上创建此计数?

标签: mysql sql join count


【解决方案1】:

请尝试以下...

SELECT products.client_id,
       clients.name AS client_name,
       manheim_auction_lanes.lane_number,
       manheim_auction_listings.sequence,
       manheim_auction_listings.gross_sale_price,
       num_of_runs,
       products.asking_price,  products.asking_price_condition,
       manheim_auctions.auction_date,
       manheim_auctions.auction_number,
       product_purchases.total_spent,
       product_purchases.purchase_price 
FROM ( SELECT manheim_auction_id AS manheim_auction_id,
              COUNT( manheim_auction_id ) AS num_of_runs
       FROM manheim_auction_listings
       GROUP BY manheim_auction_id
     ) AS num_of_runs_finder
JOIN manheim_auction_listings ON manheim_auction_listings.manheim_auction_id = num_of_runs.manheim_auction_id
JOIN cars ON cars.id = manheim_auction_listings.car_id
JOIN products ON cars.product_id = products.id
JOIN product_purchases ON current_product_purchase_id = product_purchases.id
JOIN manheim_auctions ON manheim_auctions.id = manheim_auction_listings.manheim_auction_id
JOIN manheim_auction_lanes ON manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id
JOIN clients ON clients.id = products.client_id 
            AND clients.id LIKE $P{LoggedInUserAttribute_ClientID}
WHERE manheim_auctions.auction_number = $P{SaleNumber}
  AND manheim_auctions.`year` = $P{SaleYear} 
ORDER BY manheim_auction_lanes.lane_number DESC,
         manheim_auction_listings.sequence DESC

通过将您的其他表连接到一个计算与每个manheim_auction_id 关联的列表数量的表,有效地将manheim_auction_id 的计数附加到出现manheim_auction_id 的每一行。

如果num_of_runs 是根据其他标准计算的,请相应地告诉我。

如果您有任何问题或cmets,请随时发表相应的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多