Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

not contained in the aggregate or GROUP BY..problem with D..

 
   Database Help (Home) -> Programming RSS
Next:  Computer column with case statement  
Author Message
SQL Programmer

External


Since: Jun 17, 2008
Posts: 1



(Msg. 1) Posted: Tue Jun 17, 2008 10:55 am
Post subject: not contained in the aggregate or GROUP BY..problem with DISTINCT?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

If someone could help, I'd appreciate it.

I do not know my SQL programming all that well, so I'll try to explain.

Below is a query (a view actually) that is to pull up data on inventory
items. Now, I thought that this would work but, when I run it, I get a
message saying that the SC020130.ITEMNMBR field is invalid because it is not
contained in an "aggregate" or in a GROUP BY statement.

I do have this field in the GROUP BY statement by way of the ITEMNMBR field.

Is the problem with the "DISTINCT" clause? I need this clause in there to
eliminate duplicates.

When I double-click on the error message, it takes me to the very first line
of the view. I have tried lots of things to eliminate this issue, and I get
the same message (I have tried placing the table name in front of the field
in all spots, I ahve tried removing the DISTINCT phrase, I have tried taking
ITEMNMBR out of the GROUP BY statement)--but no success. Thanks, for any
input! Below is the view.



select ITEMNMBR as [ITEM], DATE1 As [FORECAST DATE], QTYTOFORECOST_I As
[FORECAST QTY],
[TOTAL QTY ON ORDER], DUEDATE As [DUE DATE], GETDATE() As [CURRENT DATE]
FROM(
select DISTINCT SC020130.ITEMNMBR, SC020230.SFLINEITEM_I, SC020230.DATE1,
SC020230.QTYTOFORECOST_I,
MRPW4060.QUANTITY, MRPW4060.DUEDATE, GETDATE() As [CURRENT DATE],
CASE WHEN GETDATE()<MRPW4060.DUEDATE--and CASE WHEN GETDATE()>SC020230.DATE1
THEN SUM(MRPW4060.QUANTITY)
ELSE 0 END As [TOTAL QTY ON ORDER]
from SC020230 As SC020230
JOIN SC020130 As SC020130
on SC020230.SFLINEITEM_I = SC020130.SFLINEITEM_I
JOIN MRPW4060 As MRPW4060
on SC020130.ITEMNMBR = MRPW4060.ITEMNMBR
where SC020230.PLANNAME_I = 'MRP FG1' and SC020130.ITEMNMBR = '89417' and
SOPTYPE = '2') As I
GROUP BY ITEMNMBR, SFLINEITEM_I, DATE1, QTYTOFORECOST_I, DUEDATE, [TOTAL QTY
ON ORDER]
ORDER BY ITEMNMBR ASC

 >> Stay informed about: not contained in the aggregate or GROUP BY..problem with D.. 
Back to top
Login to vote
SQL Programmer

External


Since: Jun 17, 2008
Posts: 48



(Msg. 2) Posted: Tue Jun 17, 2008 11:21 am
Post subject: RE: not contained in the aggregate or GROUP BY..problem with DISTINCT? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Nevermind...I figured it out...I should not have had the SUM statement in the
select DISTINCT clause. Here is the solution:

select ITEMNMBR as [ITEM], DATE1 As [FORECAST DATE], QTYTOFORECOST_I As
[FORECAST QTY],
SUM([QTY ON ORDER]) As [TOTAL QTY ON ORDER], DUEDATE As [DUE DATE],
GETDATE() As [CURRENT DATE]
FROM(
select DISTINCT SC020130.ITEMNMBR, SC020230.SFLINEITEM_I, SC020230.DATE1,
SC020230.QTYTOFORECOST_I,
MRPW4060.QUANTITY, MRPW4060.DUEDATE, GETDATE() As [CURRENT DATE],
CASE WHEN GETDATE()<MRPW4060.DUEDATE--and CASE WHEN GETDATE()>SC020230.DATE1
THEN MRPW4060.QUANTITY
ELSE 0 END As [QTY ON ORDER]
from SC020230 As SC020230
JOIN SC020130 As SC020130
on SC020230.SFLINEITEM_I = SC020130.SFLINEITEM_I
JOIN MRPW4060 As MRPW4060
on SC020130.ITEMNMBR = MRPW4060.ITEMNMBR
where SC020230.PLANNAME_I = 'MRP FG1' and SC020130.ITEMNMBR = '89417' and
SOPTYPE = '2') As I
GROUP BY ITEMNMBR, SFLINEITEM_I, DATE1, QTYTOFORECOST_I, DUEDATE, QUANTITY
ORDER BY ITEMNMBR ASC

"SQL Programmer" wrote:

> If someone could help, I'd appreciate it.
>
> I do not know my SQL programming all that well, so I'll try to explain.
>
> Below is a query (a view actually) that is to pull up data on inventory
> items. Now, I thought that this would work but, when I run it, I get a
> message saying that the SC020130.ITEMNMBR field is invalid because it is not
> contained in an "aggregate" or in a GROUP BY statement.
>
> I do have this field in the GROUP BY statement by way of the ITEMNMBR field.
>
> Is the problem with the "DISTINCT" clause? I need this clause in there to
> eliminate duplicates.
>
> When I double-click on the error message, it takes me to the very first line
> of the view. I have tried lots of things to eliminate this issue, and I get
> the same message (I have tried placing the table name in front of the field
> in all spots, I ahve tried removing the DISTINCT phrase, I have tried taking
> ITEMNMBR out of the GROUP BY statement)--but no success. Thanks, for any
> input! Below is the view.
>
>
>
> select ITEMNMBR as [ITEM], DATE1 As [FORECAST DATE], QTYTOFORECOST_I As
> [FORECAST QTY],
> [TOTAL QTY ON ORDER], DUEDATE As [DUE DATE], GETDATE() As [CURRENT DATE]
> FROM(
> select DISTINCT SC020130.ITEMNMBR, SC020230.SFLINEITEM_I, SC020230.DATE1,
> SC020230.QTYTOFORECOST_I,
> MRPW4060.QUANTITY, MRPW4060.DUEDATE, GETDATE() As [CURRENT DATE],
> CASE WHEN GETDATE()<MRPW4060.DUEDATE--and CASE WHEN GETDATE()>SC020230.DATE1
> THEN SUM(MRPW4060.QUANTITY)
> ELSE 0 END As [TOTAL QTY ON ORDER]
> from SC020230 As SC020230
> JOIN SC020130 As SC020130
> on SC020230.SFLINEITEM_I = SC020130.SFLINEITEM_I
> JOIN MRPW4060 As MRPW4060
> on SC020130.ITEMNMBR = MRPW4060.ITEMNMBR
> where SC020230.PLANNAME_I = 'MRP FG1' and SC020130.ITEMNMBR = '89417' and
> SOPTYPE = '2') As I
> GROUP BY ITEMNMBR, SFLINEITEM_I, DATE1, QTYTOFORECOST_I, DUEDATE, [TOTAL QTY
> ON ORDER]
> ORDER BY ITEMNMBR ASC

 >> Stay informed about: not contained in the aggregate or GROUP BY..problem with D.. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 3) Posted: Tue Jun 17, 2008 2:37 pm
Post subject: Re: not contained in the aggregate or GROUP BY..problem with DISTINCT? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In the derived table you have SUM with no grouping. Try this:

SELECT ITEMNMBR as [ITEM],
DATE1 As [FORECAST DATE],
QTYTOFORECOST_I As [FORECAST QTY],
SUM([TOTAL QTY ON ORDER]) AS [TOTAL QTY ON ORDER],
DUEDATE As [DUE DATE],
GETDATE() As [CURRENT DATE]
FROM(
SELECT DISTINCT SC020130.ITEMNMBR,
SC020230.SFLINEITEM_I,
SC020230.DATE1,
SC020230.QTYTOFORECOST_I,
MRPW4060.QUANTITY,
MRPW4060.DUEDATE,
GETDATE() As [CURRENT DATE],
CASE WHEN GETDATE() < MRPW4060.DUEDATE--and CASE WHEN
GETDATE()>SC020230.DATE1
THEN MRPW4060.QUANTITY
ELSE 0 END As [TOTAL QTY ON ORDER]
FROM SC020230 As SC020230
JOIN SC020130 As SC020130
ON SC020230.SFLINEITEM_I = SC020130.SFLINEITEM_I
JOIN MRPW4060 As MRPW4060
ON SC020130.ITEMNMBR = MRPW4060.ITEMNMBR
WHERE SC020230.PLANNAME_I = 'MRP FG1'
AND SC020130.ITEMNMBR = '89417'
AND SOPTYPE = '2') As I
GROUP BY ITEMNMBR,
SFLINEITEM_I,
DATE1,
QTYTOFORECOST_I,
DUEDATE
ORDER BY ITEMNMBR ASC

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: not contained in the aggregate or GROUP BY..problem with D.. 
Back to top
Login to vote
SQL Programmer

External


Since: Jun 17, 2008
Posts: 48



(Msg. 4) Posted: Tue Jun 17, 2008 2:37 pm
Post subject: Re: not contained in the aggregate or GROUP BY..problem with DISTI [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thank you, Plamen!



"Plamen Ratchev" wrote:

> In the derived table you have SUM with no grouping. Try this:
>
> SELECT ITEMNMBR as [ITEM],
> DATE1 As [FORECAST DATE],
> QTYTOFORECOST_I As [FORECAST QTY],
> SUM([TOTAL QTY ON ORDER]) AS [TOTAL QTY ON ORDER],
> DUEDATE As [DUE DATE],
> GETDATE() As [CURRENT DATE]
> FROM(
> SELECT DISTINCT SC020130.ITEMNMBR,
> SC020230.SFLINEITEM_I,
> SC020230.DATE1,
> SC020230.QTYTOFORECOST_I,
> MRPW4060.QUANTITY,
> MRPW4060.DUEDATE,
> GETDATE() As [CURRENT DATE],
> CASE WHEN GETDATE() < MRPW4060.DUEDATE--and CASE WHEN
> GETDATE()>SC020230.DATE1
> THEN MRPW4060.QUANTITY
> ELSE 0 END As [TOTAL QTY ON ORDER]
> FROM SC020230 As SC020230
> JOIN SC020130 As SC020130
> ON SC020230.SFLINEITEM_I = SC020130.SFLINEITEM_I
> JOIN MRPW4060 As MRPW4060
> ON SC020130.ITEMNMBR = MRPW4060.ITEMNMBR
> WHERE SC020230.PLANNAME_I = 'MRP FG1'
> AND SC020130.ITEMNMBR = '89417'
> AND SOPTYPE = '2') As I
> GROUP BY ITEMNMBR,
> SFLINEITEM_I,
> DATE1,
> QTYTOFORECOST_I,
> DUEDATE
> ORDER BY ITEMNMBR ASC
>
> HTH,
>
> Plamen Ratchev
> http://www.SQLStudio.com
>
 >> Stay informed about: not contained in the aggregate or GROUP BY..problem with D.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Column X is invalid in the select list ...not contained in.. -

Error: is invalid in the select list because it is not con.. - Hi, I have a stored procedure and when I execute it, I always get the following error: "Column 'DistributionPrintReceiptInstance.DistributionCommonInstanceID' is invalid in the select list because it is not contained in either an aggregate functio...

aggregate problem - I'm new to MDX and have a dilemma. On rows I need Store and State combinations (valid associations of course). On columns I need QTD and YTD sales for Stores and States. I can easily get the sales for stores, but how do I get the total State sales? The..

Strange problem: Warning: Null value is eliminated by an a.. - Hi I would lile to run the following query: ################################################################################### select B.FI, B.DI, B.KLNR, B.YEAR, SUM(B.COUNTVIS) , SUM(B1.COUNTVIS) as BC1 , SUM(B2.COUNTVIS) as BC2 , SUM(B3.COUNTVIS)....

Beginner Problem with GROUP by - Hi, I'm a beginner with ASP but I know PHP. This works: "SELECT samAccountName FROM table GROUP BY samAccountName;" But this not: "SELECT samAccountName,cn,field1,field2 FROM log GROUP BY samAccountName;" Microsoft JET Database En...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]