안녕하세요? 쿼리 수행 시 집계 또는 하위 쿼리가 포함된 식에서는 집계 함수를 수행할 수 없습니다. 라는 오류로 진행이 안되어 방법을 문의드립니다. SELECT CASE WHEN (DIM_IND.[Dimension Value Code] LIKE 'A%') THEN 'Abc' WHEN (DIM_IND.[Dimension Value Code] LIKE 'T%') THEN 'Ttt' END AS MARKET_CODE, SUM(ABS((VE.[Invoiced Quantity]) * SIL.[UNIT PRICE])) AS ORIGINAL_AMT, SUM(CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)]))) AS AMT_LOCAL, SUM(CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)] * PEG.[EXCHANGE RATE]))) AS AMT_USD, SUM(CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)]))) AS MARGIN_LOCAL, SUM(CONVERT(float,SUM(ROUND((VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)])* PEG.[EXCHANGE RATE],4)))) AS MARGIN_USD, (CASE WHEN SUM(VE.[SALES AMOUNT (ACTUAL)]) <> 0 THEN SUM((ROUND(100*(SUM((VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)]))/SUM(VE.[SALES AMOUNT (ACTUAL)])),1))) ELSE 0 END) AS MARGIN_PER FROM ( SELECT [Document No_], [Document Line No_],[POSTING DATE],[EXTERNAL DOCUMENT NO_], SUM("Invoiced Quantity") AS "Invoiced Quantity", SUM("Cost Amount (Actual)") AS "Cost Amount (ACTUAL)", SUM("Sales Amount (Actual)") AS "Sales Amount (ACTUAL)", SUM("Cost Amount (Non-Invtbl_)") AS "Cost Amount (Non-Invtbl_)" FROM VALUE ENTRY GROUP BY [Document No_], [Document Line No_],[POSTING DATE],[EXTERNAL DOCUMENT NO_] ) AS VE LEFT JOIN SALES INVOICE LINE AS SIL ON VE.[DOCUMENT NO_] = SIL.[DOCUMENT NO_] AND VE.[DOCUMENT LINE NO_] = SIL.[LINE NO_] INNER JOIN SALES INVOICE HEADER AS SIH ON SIH.NO_ = SIL.[DOCUMENT NO_] GROUP BY DIM_IND.[Dimension Value Code]
쿼리가 너무 쓸데없이 길어서 축약해서 글 복사하다 보니 생략되었네요. 다시 올립니다. sum(sum())형태를 제거해 보았으나 값이 제대로 grouping이 안되네요. 도움 부탁드려요. select MARKET_CODE , AMT_LOCAL ,AMT_USD from ( SELECT CASE WHEN (DIM_IND.[Dimension Value Code] LIKE 'A%') THEN 'A' WHEN (DIM_IND.[Dimension Value Code] LIKE 'E%') THEN 'E' WHEN (DIM_IND.[Dimension Value Code] LIKE 'F%') THEN CASE WHEN (DIM_IND.[Dimension Value Code] LIKE 'FM%') THEN 'F' WHEN (DIM_IND.[Dimension Value Code] LIKE 'F%') THEN 'F1' END WHEN (DIM_IND.[Dimension Value Code] LIKE 'H%') THEN 'H' WHEN (DIM_IND.[Dimension Value Code] LIKE 'I%') THEN 'I' WHEN (DIM_IND.[Dimension Value Code] LIKE 'M%') THEN 'M' WHEN (DIM_IND.[Dimension Value Code] LIKE 'O%') THEN 'O' WHEN (DIM_IND.[Dimension Value Code] LIKE 'P%') THEN 'P' WHEN (DIM_IND.[Dimension Value Code] LIKE 'T%') THEN 'T' END AS MARKET_CODE, SUM(ABS((VE.[Invoiced Quantity]) * SIL.[UNIT PRICE])) AS ORIGINAL_AMT, CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)])) AS AMT_LOCAL, CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)] * PEG.[EXCHANGE RATE])) AS AMT_USD, CONVERT(float,SUM(VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)])) AS MARGIN_LOCAL, CONVERT(float,SUM(ROUND((VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)])* PEG.[EXCHANGE RATE],4))) AS MARGIN_USD, (CASE WHEN SUM(VE.[SALES AMOUNT (ACTUAL)]) <> 0 THEN (ROUND(100*(SUM((VE.[SALES AMOUNT (ACTUAL)]+VE.[COST AMOUNT (ACTUAL)]+VE.[COST AMOUNT (NON-INVTBL_)]))/SUM(VE.[SALES AMOUNT (ACTUAL)])),1)) ELSE 0 END) AS MARGIN_PER FROM ( SELECT [Document No_], [Document Line No_],[POSTING DATE],[EXTERNAL DOCUMENT NO_], SUM("Invoiced Quantity") AS "Invoiced Quantity", SUM("Cost Amount (Actual)") AS "Cost Amount (ACTUAL)", SUM("Sales Amount (Actual)") AS "Sales Amount (ACTUAL)", SUM("Cost Amount (Non-Invtbl_)") AS "Cost Amount (Non-Invtbl_)" FROM [VALUE ENTRY] GROUP BY [Document No_], [Document Line No_],[POSTING DATE],[EXTERNAL DOCUMENT NO_] ) AS VE LEFT JOIN [SALES INVOICE LINE] AS SIL ON VE.[DOCUMENT NO_] = SIL.[DOCUMENT NO_] AND VE.[DOCUMENT LINE NO_] = SIL.[LINE NO_] INNER JOIN [SALES INVOICE HEADER] AS SIH ON SIH.NO_ = SIL.[DOCUMENT NO_] LEFT JOIN [Posted Document Dimension] AS DIM_IND ON DIM_IND.[Table ID] = '113' and DIM_IND.[Dimension Code] = 'INDUSTRY' and DIM_IND.[Document No_] = SIL.[Document No_] AND DIM_IND.[Line No_] = SIL.[Line No_] INNER JOIN [PEGGED RATE] AS PEG ON PEG.YEAR = SUBSTRING(CONVERT(VARCHAR, VE.[POSTING DATE], 112), 1, 4) GROUP BY DIM_IND.[Dimension Value Code] ) as test
글쎄요? 올려주신 쿼리도 작성하다 만건지? 중간에 잘린건지? 알 수 없는 쿼리네요?
"제대로 그루핑이 안된다"는 한줄짜리 표현만으로는 뭘 원하는지 알 수 없습니다.
무엇을 원하는지? 어느 부분에서 막히는지? 구체적으로 표현해 주세요.
그런데 결국 똑같은 질문을 하셧던것 같은데요? http://gurubee.net/article/80302
market_code 로 그룹핑을 하고 싶으신거 아닌가요?
그런데 GROUP BY 는 market_code 가 아닌 DIM_IND.[Dimension Value Code] 로 하고 있네요?