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' 无效。 展开
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' 无效。 展开
2个回答
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
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那部分写到试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询