SQL语句 别名为啥无效,怎么解决啊

selecta.gsName,a.quName,sum(a.num)民用水量,(selectsum(price)fromawheretypeId=1)水费,(select... select a.gsName, a.quName,
sum(a.num) 民用水量,
(select sum(price) from a where typeId=1) 水费, (select sum(price) from a where typeId=2) 违约金,
(select sum(price) from a where typeId=3) 维修费, (select sum(price) from a where typeId=4) 维护费,
sum(b.num) 商用水量,
(select sum(price) from b where typeId=1) 水费, (select sum(price) from b where typeId=2) 违约金,
(select sum(price) from b where typeId=3) 维修费, (select sum(price) from b where typeId=4) 维护费
from (select gsName, quName, num, price, typeId from mingxi, users, fenqu where fenqu.gsId=users.gsId and mingxi.userId=users.userId and xzId=1) as a,
(select gsName, quName, num, price, typeId from mingxi, users, fenqu where fenqu.gsId=users.gsId and mingxi.userId=users.userId and xzId=2) as b
group by a.gsName, a.quName
go错误提示:消息 208,级别 16,状态 1,第 1 行
对象名 'a' 无效。
展开
 我来答
匿名用户
2014-01-19
展开全部
老大,你这个好复杂哟,呵呵。。。(select sum(price) from a where typeId=1) 水费, (select sum(price) from a where typeId=2) 像这个语句里面,作用域不一样了,所以写别名他找不到了。建议你把(select gsName, quName, num, price, typeId from mingxi, users, fenqu where fenqu.gsId=users.gsId and mingxi.userId=users.userId and xzId=1)和(select gsName, quName, num, price, typeId from mingxi, users, fenqu where fenqu.gsId=users.gsId and mingxi.userId=users.userId and xzId=2)写成视图,然后这样用: select a.gsName, a.quName,
sum(a.num) 民用水量,
(select sum(price) from 你的视图a where typeId=1) 水费, (select sum(price) from 你的视图a where typeId=2) 违约金,
(select sum(price) from 你的视图a where typeId=3) 维修费, (select sum(price) from 你的视图a where typeId=4) 维护费,
sum(b.num) 商用水量,
(select sum(price) from 你的视图b where typeId=1) 水费, (select sum(price) from 你的视图b where typeId=2) 违约金,
(select sum(price) from 你的视图b where typeId=3) 维修费, (select sum(price) from 你的视图b where typeId=4) 维护费
from 你的视图a as a,
你的视图b as b
group by a.gsName, a.quName
go
匿名用户
2014-01-19
展开全部
采用LINQ的写法吧,把from那部分写到试试
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式