Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats wrong with this Data Explorer SQL query?

I am trying to write a How much did I type? query on 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 ')'.

What is the correct syntax for this?

like image 579
Lazer Avatar asked Jun 25 '10 05:06

Lazer


People also ask

What is the error in SQL query?

The most common SQL error is a syntax error. What does syntax mean? Basically, it means a set arrangement of words and commands. If you use improper syntax, the database does not know what you're trying to tell it.


1 Answers

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
like image 72
Aaron Harun Avatar answered Sep 21 '22 03:09

Aaron Harun