I have Data as follows from a select Statement from ItemDetails Table:
INPUT [Same Ref]:
| ItemNo | ItemValue | Ref |
| ------------ | ------------ | ------------ |
| ITM001 | 81 | A1 |
| ITM002 | 45 | B1 |
| ITM001 | 81 | A1 |
Expected OUTPUT [Same Ref - Do AVG]:
ItemNo | ItemValue | Ref |
---|---|---|
ITM001 | 40.50 | A1 |
ITM002 | 45 | B1 |
ITM001 | 40.50 | A1 |
INPUT [Same Ref but Different ItemValue] :
ItemNo | ItemValue | Ref |
---|---|---|
ITM001 | 81 | A1 |
ITM002 | 45 | B1 |
ITM001 | 30 | A1 |
Expected OUTPUT [No Change to Value]:
ItemNo | ItemValue | Ref |
---|---|---|
ITM001 | 81 | A1 |
ITM002 | 45 | B1 |
ITM001 | 30 | A1 |
Expected INPUT [ Different Ref Same ItemValue] :
ItemNo | ItemValue | Ref |
---|---|---|
ITM001 | 81 | A1 |
ITM002 | 45 | B1 |
ITM001 | 81 | A2 |
Expected OUTPUT [ No Changes to ItemValue]:
ItemNo | ItemValue | Ref |
---|---|---|
ITM001 | 81 | A1 |
ITM002 | 45 | B1 |
ITM001 | 81 | A2 |
Rows are duplicating due to other column s.
SELECT ItemNo, AVG(ItemValue) OVER (PARTITION BY ItemNo) / COUNT(*) OVER(PARTITION BY ItemNo) AS ItemValue
FROM ItemDetails ;
seems not working .
Can anyone please help ?
1条答案
按热度按时间b1zrtrql1#
This gives the outputs you indicated in your post; note that if your ItemValue column is a float, this will behave unpredictably and should be cast to a decimal or numeric type first.