我只需要对dbo_ordetail.typee
进行分组,但是这段代码坚持要在分组中添加orderno,dued,deld
如何只用dbo_ordetail.typee
组重写SQL Server 2005中的波纹管代码。
来自ms访问的SQL查询
SELECT dbo_ordetail.typee, Sum(dbo_ordetail.qty) AS SumOfqty,
(SELECT sum(dbo_ordetail.qty)
FROM dbo_ordetail
WHERE dbo_ordetail.ordern=o.orderno AND
datediff("d",now(), o.dued) > 30 AND
o.deld is NULL
) AS 30day
FROM dbo_ordert o INNER JOIN dbo_ordetail ON o.orderno = dbo_ordetail.ordern
GROUP BY dbo_ordetail.typee;
答案 0 :(得分:0)
编译器因在main-query和子查询中使用dbo_orderdetail
而感到困惑。尝试用不同的别名来区分它们:
SELECT
dbo_od.typee,
Sum(dbo_od.qty) AS SumOfqty,
(SELECT sum(dbo_od_1.qty)
FROM dbo_ordetail dbo_od_1
where dbo_od_1.ordern=o.orderno
and datediff("d",now(), o.dued) > 30
AND o.deld is NULL) as 30day
FROM dbo_ordert o
INNER JOIN dbo_ordetail dbo_od ON o.orderno = dbo_od.ordern
GROUP BY dbo_od.typee
现在,您可以在两种情况下明确区分两个表。
答案 1 :(得分:0)
你的查询形成错误..应该如下
SELECT dbo_ordetail.typee, Sum(dbo_ordetail.qty) AS SumOfqty, 30day
from
(SELECT o.orderno as orderno , sum(dbo_ordetail.qty) FROM dbo_ordetail
WHERE dbo_ordetail.ordern=o.orderno and datediff("d",now(), o.dued) > 30 AND o.deld is NULL) AS 30day
group by o.orderno,o.dued,o.deld)
o INNER JOIN dbo_ordetail ON o.orderno = dbo_ordetail.ordern
GROUP BY dbo_ordetail.typee;
答案 2 :(得分:0)
也许这对你有用:
SELECT typee, SUM(qty) AS SumOfqty, _30day
FROM
(SELECT dbo_ordetail.typee, dbo_ordetail.qty,
(SELECT sum(dbo_ordetail.qty)
FROM dbo_ordetail
WHERE dbo_ordetail.ordern=o.orderno AND
DATEDIFF(d, GETDATE(), o.dued) > 30 AND
o.deld is NULL
) AS _30day
FROM dbo_ordert o INNER JOIN dbo_ordetail ON o.orderno = dbo_ordetail.ordern) t
GROUP BY typee, _30day;
列名不能以SQL Server中的数字开头,所以我用下划线作为前缀。