2010-06-25 74 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 

希望我輸入正確...