我有一个问题:
function foo() {
this.a = 1;
this.bar = function () {
return this.a;
}
}
var instance = new foo;
instance.bar(); // returns 1
instance.bar.bind(otherContext)(); // returns 2
我需要的是输出每一行,但我无法弄清楚如何做到这一点。现在,我得到了这个:
SELECT name, APN, BPN, count(APN), min(aCost), min(bCost), ceil(avg(aQty)),
max(aQty), sum(bShipped),
CONCAT(truncate((avg(aResale)-avg(aCost))/avg(aResale),2) * 100,'%'), code FROM
(SELECT name, APN, BPN, aCost, aQty, code
FROM table_1
WHERE customer = '12345' AND
aDate >= '2013-01-01' and
aDate <= '2015-12-12') as qh
INNER JOIN (SELECT CPN, bCost, bResale, bShipped from table_2
WHERE customer = '12345') as ih
ON qh.APN = ih.CPN
WHERE bShipped > 0
GROUP BY qh.APN;
由于NAME | APN | BPN | APN Count
asdf 001 555 3 /*summary of APN 001*/
qere 002 865 1 /*summary of APN 002*/
rtrt 003 123 2 /*summary of APN 003*/
,我获得group by
的摘要,但是如果我不使用任何qh.APN
语句,我的结果只有1行。
我希望有这个group by
列,而同时显示所有行 - 没有汇总任何值。像这样:
APN Count
我需要查看构成NAME | APN | BPN | APN Count
asdf 001 555 3 /*Individual record, with count too*/
asdf 001 862 3 /*Individual record, with count too*/
asdf 001 999 3 /*Individual record, with count too*/
qere 002 865 1 /*Individual record, with count too*/
rtrt 003 123 2 /*Individual record, with count too*/
rtrt 003 456 2 /*Individual record, with count too*/
列的每条记录,因为我需要查看每个APN Count
,而不只是一个摘要行。我现在用BPN
写这个,因为看到汇总数据比 nothing 更好,因为我不知道在这里使用正确的语法来得到结果我想。
答案 0 :(得分:2)
将表1与您的聚合查询作为子查询连接。
SELECT t1.name, t1.apn, t1.bpn, t1.code, t2.*
FROM table_1 AS t1
JOIN (
SELECT APN, count(APN) AS APN_count, min(aCost) AS min_aCost, min(bCost) AS min_bCost, ceil(avg(aQty)) AS avgQty,
max(aQty) AS maxQty, sum(bShipped) AS sum_bShipped,
CONCAT(truncate((avg(aResale)-avg(aCost))/avg(aResale),2) * 100,'%') AS avg_Margin FROM
(SELECT name, APN, BPN, aCost, aQty, code
FROM table_1
WHERE customer = '12345' AND
aDate >= '2013-01-01' and
aDate <= '2015-12-12') as qh
INNER JOIN (SELECT CPN, bCost, bResale, bShipped from table_2
WHERE customer = '12345') as ih
ON qh.APN = ih.CPN
WHERE bShipped > 0
GROUP BY qh.APN) AS t2
ON t1.APN = t2.APN
答案 1 :(得分:2)
显而易见的方法是将结果集加入某些原始表。在你的情况下,我不确定什么是原始的,但它可能是这样的:
SELECT t.*,
t_agregated.*
FROM table_1 t
LEFT JOIN (
SELECT t1.APN,
count(t1.APN),
min(t1.aCost),
min(bCost),
ceil(avg(t1.aQty)),
max(aQty),
sum(bShipped),
CONCAT(truncate((avg(t1.aResale)-avg(t1.aCost))/avg(t1.aResale),2) * 100,'%')
FROM table_1 t1
INNER JOIN table_2 t2
ON t1.APN = t2.CPN
AND t2.bShipped > 0
WHERE t1.customer = '12345' AND
aDate >= '2013-01-01' and
aDate <= '2015-12-12'
GROUP BY t1.APN) t_agregated
ON t.APN = t_agregated.APN
WHERE t.customer = '12345' AND
t.aDate >= '2013-01-01' and
t.aDate <= '2015-12-12'
但我很确定,你的目标远非如此。您应该更好地发布原始数据源和预期结果集。在这里提问的好方法是为sqlfiddle提供表格方案和数据样本。