0
select

count([Item Nbr]) as [Item Nbr],

count([Item Flags]) as [Item Flags],

count(UPC) as UPC,

count([Store Nbr]) as [Store Nbr] ,

count (RetrievalWeek) as RetrievalWeek,

* from tblStoreItemForecast

GROUP BY

[Item Nbr],[Item Flags],UPC,

[Store Nbr], [Week 01 Forecast], [Week 02 Forecast],

[Week 03 Forecast],[Week 04 Forecast],[Week 05 Forecast],

[Week 06 Forecast],[Week 07 Forecast],[Week 08 Forecast],[Week 09 Forecast],

[Week 10 Forecast],[Week 11 Forecast],[Week 12 Forecast],[Week 13 Forecast],

[Week 14 Forecast],[Week 15 Forecast],RetrievalWeek

having (count(*) > 1)

order by RetrievalWeek desc

I got the message error Msg 8120,

Level 16, State 1, Line 76

Column 'tblStoreItemForecast.Week 16 Forecast' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Msg 209, Level 16, State 1, Line 91

Ambiguous column name 'RetrievalWeek'.

Can anyone help me why this happen.

I tried another method

if i run the below code it gives me no result as i am using record no (unique key) here

select

[Item Nbr],[Item Flags],UPC,[Store Nbr],RetrievalWeek,recordno,COUNT(*)

from tblStoreItemForecast

GROUP BY

[Item Nbr],[Item Flags],UPC,[Store Nbr],

RetrievalWeek,recordno

having

count(*) > 1

order by RetrievalWeek desc

but if I run the below code without using record no as it is unique here it gives result

select

[Item Nbr],[Item Flags],UPC,[Store Nbr],RetrievalWeek,COUNT(*)

from tblStoreItemForecast

GROUP BY

[Item Nbr],[Item Flags],UPC,[Store Nbr],

RetrievalWeek

having

count(*) > 1

order by RetrievalWeek desc

i want to count the duplicate by using record no how to write the can not figure out i also use inner join but getting error space in the log can not be reused. Is there any solution?

11
  • 1
    This looks like sql server, not mysql which you tagged. The error is also pretty clear, what don't you understand about it? Commented May 31, 2022 at 12:10
  • I've removed the conflicting tags; edit your question to retag appropriately. Commented May 31, 2022 at 12:11
  • 1
    Remove the * in your SELECT Commented May 31, 2022 at 12:14
  • 1
    Why not use a windowed count then? Aggregating and grouping on the same columns is always the sign of a flaw. Commented May 31, 2022 at 12:31
  • 2
    To be honest it looks like you have a bit of a design flaw here. You have violated 1NF by using repeating groups. [Week 01 Forecast] etc. You have also made it more difficult on yourself by putting spaces in your column names. The grouping by every column sort of suggest your table is also lacking a primary key. Commented May 31, 2022 at 13:09

1 Answer 1

0

You have an output column alias with the same name as a table column. Try adding an underscore to differentiate as below.

select

count([Item Nbr]) as [Item Nbr],

count([Item Flags]) as [Item Flags],

count(UPC) as UPC,

count([Store Nbr]) as [Store Nbr] ,

count (RetrievalWeek) as Retrieval_Week,

* from tblStoreItemForecast

GROUP BY

[Item Nbr],[Item Flags],UPC,

[Store Nbr], [Week 01 Forecast], [Week 02 Forecast],

[Week 03 Forecast],[Week 04 Forecast],[Week 05 Forecast],

[Week 06 Forecast],[Week 07 Forecast],[Week 08 Forecast],[Week 09 Forecast],

[Week 10 Forecast],[Week 11 Forecast],[Week 12 Forecast],[Week 13 Forecast],

[Week 14 Forecast],[Week 15 Forecast],RetrievalWeek

having (count(*) > 1)

order by RetrievalWeek desc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.