2010-06-25 81 views
5

我想写一个我输入了多少?查询Stack* Data Explorer此数据资源管理器SQL查询有什么问题?

修改现有的查询让我这么远:

-- How much did I type? 

DECLARE @UserId int = ##UserId## 

    select sum(len(Body)) AS 'Posts' from posts where owneruserid = @UserId, 
    select sum(len(Text)) AS 'Comments' from comments where userid = @UserId, 
    (select sum(len(Body)) from posts where owneruserid = @UserId + 
    select sum(len(Text)) from comments where userid = @UserId) AS 'Total' 

我期待着三列一列,这样的事情:

Posts Comments Total 
1234  5678  6912 

但是有一些语法问题,由于其我得到:

Error: Incorrect syntax near ','. Incorrect syntax near ','. Incorrect syntax near the keyword 'select'. Incorrect syntax near ')'.

这是什么正确的语法?

+0

问题是关于http://odata.stackexchange.com/stackoverflow/query/new具体。随机SQL查询没有帮助。 – 2010-06-25 06:13:29

+0

@Aaron Harun:大家做错了什么?它是否像Data Explorer仅支持有效的SQL查询的子集? – Lazer 2010-06-25 06:25:55

+0

基本上,是的。他们需要使用TSQL,但有些则不是。 (http://www.devguru.com/technologies/t-sql/home.asp)在大多数“错误”的例子中,有语法错误和其他使用不同字段名称的例子。 *耸耸肩*它发生。 – 2010-06-25 06:31:59

回答

3

这里是一个工作查询:

​​
1

我会做这样......

declare @ownerId int 
set @ownerId = 1 

declare @Posts bigint 
declare @Comments bigint 

select 
@Posts = sum(len(Body)) 
from Posts where owneruserid = @ownerId 

select 
@Comments = sum(len(Text)) 
from Comments where userid = @ownerId 

select @Posts as 'Posts', @Comments as 'Comments', @Posts + @Comments as 'Total' 
+0

最初,我忘记清除“sum”语句前的select语句。现在应该是好的。 – dhillis 2010-06-25 05:53:51

+0

对不起,我累了,没有注意到subselects ...新版本来... – dhillis 2010-06-25 06:00:24

+0

测试查询在这里:http://arrow.attachs.com/stackoverflow/query/new – 2010-06-25 06:21:33

0
 
-- How much did I type? 

/* If this is to be a parameter from your app, you don't need to declare it here*/ 
DECLARE @UserId int; 
set @UserID = 4; 

Select *, (Posts+Comments) as Total 
FROM 
    (select sum(len(Body)) AS Posts FROM posts where owneruserid = @UserId) p, 
    (select sum(len(Text)) AS Comments FROM comments where userid  = @UserId) c 
+0

你有一个额外的等于,除此之外,它的作品。 – 2010-06-25 06:12:28

1

你好你的问题是,你有3条语句连接起来以1点声明 - 只要发出一条陈述如果: like

select sum(len(Body)) AS 'Posts', sum(len(Text)) AS 'Comments' , sum(len(Body)) + sum(len(Text)) AS Total 
from posts t1 inner join comments t2 on t1.owneruserid = t2.userid 
where t1.owneruserid = @UserId 

希望我输入正确...