What happened to this SQL Data Explorer query?

I'm trying to write how much did I type? request for Stack * Data Explorer .

Modification of an existing request led me like this:

-- 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 expect three columns and one row, something like this:

Posts    Comments    Total
1234     5678        6912

But there is a syntax issue due to which I get:

Error: Incorrect syntax next to ','. Invalid syntax next to ','. Incorrect syntax next to the select keyword. Invalid syntax next to ')'.

What is the correct syntax for this?

+5
source share
4 answers

Here is a working request:

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
+3
source

I would do it like this ...

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

, , 3 , 1 - :

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

I hope I typed the correct one ...

+1
source
- 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
source

All Articles