此 Data Explorer SQL 查询有什么问题?

发布于 2024-09-07 03:48:10 字数 808 浏览 4 评论 0原文

我正在尝试在 Stack* Data Explorer我输入了多少?查询一个>。

修改现有查询让我到目前为止:

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

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

Posts    Comments    Total
1234     5678        6912

但是存在一些语法问题,因此我得到:

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

正确的语法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉城凉梦凉人心 2024-09-14 03:48:10

这是一个有效的查询:

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

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
野味少女 2024-09-14 03:48:10

我会这样做...

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'

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'
浅忆流年 2024-09-14 03:48:10

您好,您的问题是您有 3 个语句连接到 1 个语句 - 只需从 if 中生成一个语句:
就像

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

希望我输入正确一样...

Hi your problem is that you have 3 Statements concatenated to 1 Statement - just make one Statement out of 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 ...

梦里南柯 2024-09-14 03:48:10
-- 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
-- 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文