2010-06-25 16 views
5

Estoy tratando de escribir un ¿Cuánto he escrito? consulta en Stack* Data Explorer.¿Qué pasa con esta consulta SQL de Data Explorer?

Modificación de una consulta existente mí esto tiene ahora:

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

estoy esperando tres columnas y una fila, algo como esto:

Posts Comments Total 
1234  5678  6912 

Pero hay algún problema de sintaxis, debido a lo cual me sale:

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

¿Cuál es la sintaxis correcta para esto?

+0

La pregunta es sobre http://odata.stackexchange.com/stackoverflow/query/new específicamente. Las consultas SQL aleatorias no ayudan. –

+0

@ Aaron Harun: ¿Qué está haciendo mal todo el mundo? ¿Es como Data Explorer solo admite un subconjunto de consultas SQL válidas? – Lazer

+0

Básicamente, sí. Necesitan usar TSQL, pero otros no. (http://www.devguru.com/technologies/t-sql/home.asp) En la mayoría de los ejemplos "incorrectos", hay errores de sintaxis y otros utilizan nombres de campos diferentes. * encogerse de hombros * Sucede. –

Respuesta

3

Aquí es una consulta de trabajo:

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 
1

lo haría de esta manera ...

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

Originalmente, olvidé borrar las declaraciones seleccionadas antes de las declaraciones de "suma". Debería estar bien ahora. – dhillis

+0

lo siento, estoy cansado y no noté las subselecciones ... viene nueva versión ... – dhillis

+0

Pruebe la consulta aquí: http://odata.stackexchange.com/stackoverflow/query/new –

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

Tuviste un igual extra, aparte de eso, funciona. –

1

Hola el problema es que tiene 3 Estados concatenado a 1 Declaración - Solo haga una Declaración de: como

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 

Espero haber escrito correctamente ...