all groups > sql server programming > september 2003 >
You're in the

sql server programming

group:

returning data



returning data john smith
9/18/2003 9:46:35 PM
sql server programming: I am trying to get a query to work using grouping by days

SELECT CAST(datepart(dd,date_created) AS int) AS DOW, SUM(prod_unique) as
unique_count, COUNT(prod_id) as total_hits,
(COUNT(prod_id) - SUM(prod_unique)) AS total_returns
FROM crm_ecom_tracker
WHERE datepart(yyyy,date_created) = @spYear AND datepart(m,date_created) =
@spMonth AND org_id = @spOrg_id and prod_id = @spProd_id
GROUP BY CAST(datepart(dd,date_created) AS int)
ORDER BY CAST(datepart(dd,date_created) AS int) DESC


RETURN @@ROWCOUNT

The problem is when i try to add generic fields i get the field is invalid
because it is not an aggregate function. Since it is a tracking query the
results need to be grouped by the day of the week.

Thanks for any help,
Donnie

Re: returning data Louis Davidson
9/18/2003 10:07:40 PM
I assume you mean that you try to add fields that are not part of the GROUP
By statement. This is not allowed, since there may be multiple different
values. Consider the following table of data

Col1 Col2
1 1
1 2
1 3

SELECT Col1, Count(*)
FROM table
GROUP BY col

If you group by 1, you only get one row. However, if you tried to put Col2
in the output, there are three different values. You could use Min() or
Max() to output one of the rows, but that is probably not what you want.

--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266


[quoted text, click to view]

Re: returning data Andrew J. Kelly
9/18/2003 11:21:54 PM
I seem to be missing the part where your adding fields, can you be more
specific. What exactly is the error? By the way there is no need to cast
the Datepart as an integer since it is an integer.

--

Andrew J. Kelly
SQL Server MVP


[quoted text, click to view]

Re: returning data john smith
9/19/2003 3:43:38 PM
Like if I added ...

date_created,ip_address,browser

.... to the query. I can't use any non aggregate returns

error ...
"dbo.table.column_name" is invalid in the select list because it is not
contained in either an aggregate function or group by clause.

Thanks,
Donnie

[quoted text, click to view]

Re: returning data Andrew J. Kelly
9/19/2003 8:06:08 PM
That is the nature of aggregates and group by. If you post the tables DDL
and a description of exactly what you want someone here should be able to
offer a query for you.

--

Andrew J. Kelly
SQL Server MVP


[quoted text, click to view]

AddThis Social Bookmark Button