2010-06-25 22 views
5

Sto provando a scrivere un Quanto ho digitato? query su Stack* Data Explorer.Cosa c'è di sbagliato in questa query SQL Data Explorer?

Modifica di una query esistente me questo ha ottenuto di gran lunga:

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

mi aspetto tre colonne e una riga, qualcosa di simile:

Posts Comments Total 
1234  5678  6912 

ma c'è qualche problema di sintassi, a causa della quale ottengo:

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

Qual è la sintassi corretta per questo?

+0

La domanda riguarda http://odata.stackexchange.com/stackoverflow/query/new in particolare. Le query SQL a caso non aiutano. –

+0

@ Aaron Harun: cosa stanno facendo tutti sbagliati? È come Data Explorer supporta solo un sottoinsieme di query SQL valide? – Lazer

+0

Fondamentalmente, sì. Devono usare TSQL, ma alcuni non lo sono. (http://www.devguru.com/technologies/t-sql/home.asp) Nella maggior parte degli esempi "sbagliati", ci sono errori di sintassi e altri hanno usato nomi di campo diversi. * scrollando le spalle * Succede. –

risposta

3

Ecco una query di lavoro:

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

vorrei farlo in questo modo ...

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

Inizialmente, ho dimenticato di cancellare le istruzioni select prima delle istruzioni "sum". Dovrebbe essere buono ora. – dhillis

+0

scusate, sono stanco e non ho notato i subselect ... nuova versione in arrivo ... – dhillis

+0

Testare la query qui: 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

Hai avuto un altro equals, a parte quello, funziona. –

1

Hi Il tuo problema è che avete 3 Dichiarazioni concatenato al 1 Dichiarazione - Basta fare una dichiarazione fuori di se: come

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

Problemi correlati