所以问题来了。我正在尝试编写一份新的填充率报告,因为内置的报告不够好...我正在尝试运行单个 select 语句来返回两者,即特定月份订购某商品的次数的计数,然后还统计已开具发票/全额发货的次数。
这段代码显然是错误的,我目前也将其限制为只能查看 2015 年 8 月的数据,但这只是为了简化测试过程中的结果。
我不知道如何进行第二次计数...这就是我正在尝试的(大脑卡在旧的每个循环逻辑上):
select inv_mast.item_id,
inv_mast.item_desc,
"YEAR" = year(oe_line.required_date),
"MONTH" = month(oe_line.required_date),
"ORDERS" = count(1),
"HITS" = (
select count(1)
from invoice_line
where invoice_line.order_no = oe_line.order_no
and invoice_line.oe_line_number = oe_line.line_no
and invoice_line.qty_shipped = oe_line.qty_ordered
)
from oe_line,
inv_mast,
inv_loc
where inv_mast.inv_mast_uid = oe_line.inv_mast_uid
and inv_mast.delete_flag = 'N'
and inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
and inv_loc.location_id = '101'
and year(oe_line.required_date) = '2015'
and month(oe_line.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(oe_line.required_date),
month(oe_line.required_date)
order by inv_mast.item_id
最佳答案
对我来说,您似乎可以重写查询以使用发票行表上的左联接。没有任何适当的测试数据我不能保证它是正确的,但我认为应该是正确的。
除了左连接之外,我还更改为显式连接并移动了别名,因为我认为 MySQL 不支持 alias = column
语法。
select inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date) as "YEAR",
month(o.required_date) as "MONTH",
count(1) as "ORDERS",
count(invoice_line.order_no) as "HITS"
from oe_line o
join inv_mast on inv_mast.inv_mast_uid = o.inv_mast_uid
join inv_loc on inv_mast.inv_mast_uid = inv_loc.inv_mast_uid
left join invoice_line on invoice_line.order_no = o.order_no
and invoice_line.oe_line_number = o.line_no
and invoice_line.qty_shipped = o.qty_ordered
where inv_mast.delete_flag = 'N'
and inv_loc.location_id = '101'
and year(o.required_date) = '2015'
and month(o.required_date) = '8'
group by inv_mast.item_id,
inv_mast.item_desc,
year(o.required_date),
month(o.required_date)
order by inv_mast.item_id;
关于mysql - 如何在 select 语句中使用嵌套 select 执行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260111/