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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With