I'm attempting to write a Just how much did I type? query on Stack* Data Explorer.

Modifying a current 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'm expecting three posts and something row, something similar to this:

Posts    Comments    Total
1234     5678        6912

But there's some syntax problem, because of that we get:

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

What's the correct syntax with this?

This 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

I'd get it done by doing 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'

Hi your condition is you have 3 Claims concatenated to at least one Statement - simply make one Statement from 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 ...


-- Just how much did I type?

/* If this sounds like to become a parameter out of your application, you don't have to declare it here*/

DECLARE @UserId int

set @UserID = 4

Choose *, (Posts+Comments) as Total

FROM

  (choose sum(len(Body)) AS Posts    FROM posts    where owneruserid = @UserId ) p,

  (choose sum(len(Text)) AS Comments FROM comments where userid      = @UserId ) c