这个Data Explorer SQL查询有什么问题?

时间:2022-03-03 22:46:59

I am trying to write a How much did I type? query on Stack* Data Explorer.

我想写一个我输入了多少?查询Stack * Data Explorer。

Modifying an existing query got me this far:

修改现有的查询让我走得很远:

-- 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'

I am expecting three columns and one row, something like this:

我期待三列一行,如下所示:

Posts    Comments    Total
1234     5678        6912

But there is some syntax problem, due to which I get:

但是有一些语法问题,由此得到:

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

错误:','附近的语法不正确。 ','附近的语法不正确。关键字“select”附近的语法不正确。 ')'附近的语法不正确。

What is the correct syntax for this?

这个的正确语法是什么?

4 个解决方案

#1


3  

Here is a working query:

这是一个有效的查询:

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

#2


1  

I would do it this way...

我会这样做的......

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'

#3


1  

Hi your problem is that you have 3 Statements concatenated to 1 Statement - just make one Statement out of if: like

嗨,你的问题是你有3个语句连接到1语句 - 只需要一个语句if: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

Hope I typed correct ...

希望我输入正确的...

#4


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

#1


3  

Here is a working query:

这是一个有效的查询:

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

#2


1  

I would do it this way...

我会这样做的......

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'

#3


1  

Hi your problem is that you have 3 Statements concatenated to 1 Statement - just make one Statement out of if: like

嗨,你的问题是你有3个语句连接到1语句 - 只需要一个语句if: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

Hope I typed correct ...

希望我输入正确的...

#4


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