2016-10-24 2 views
0

모든 도움을 주신 모든 분들께 감사 드리며 불행히도 제가 기대했던 결과를 내지 못했습니다. 더 나은 도움을 얻으려는 노력에서 나는 내가 찾고있는 것을 설명하려고 노력할 것입니다.SQL 서버에서 Sum (MAX) 계산을 시도합니다.

"IN", "AA_Now", "STF_Now", "dbo.Sheet1 $ .LOB_name", "dbo.Sheet1 $ .LifeCycleName"및 "dbo.Sheet1 $ .AreaOfBusiness" . 각 "IN"에는 "AA_Now"및 "STF_Now"가 있습니다. "IN"그룹이 "dbo.Sheet1 $ .LOB_name"아래에 롤업됩니다. "dbo.Sheet1 $ .LOB_name"에서 그룹의 최대 값을 "IN"으로 올리고 싶습니다. 이제 "dbo.Sheet1 $ .LOB_name"은 "dbo.Sheet1 $ .LifeCycleName"아래에 겹쳐서 표시되며 원하는 값은 "dbo.Sheet1 $ .LOB_name"아래에 표시되는 최대 값의 합계입니다. "dbo.Sheet1 $ .LifeCycleName"의 롤업. 마지막으로 "dbo.Sheet1 $ .LifeCycleName"은 "dbo.Sheet1 $ .AreaOfBusiness"로 롤백됩니다. 이전에 내가 원하는 것은 "dbo.Sheet1 $ .LifeCycleName"의 합계입니다. 이것들은 "AA_Now"와 "STF_Now"열에만 해당합니다 피벗 테이블에서이 작업을 시도했지만 아무런 소용이 없었으며 원시 데이터로 정렬하는 것이 가장 좋을 것이라고 생각했습니다.

내가을 수행 할 수 없습니다 합 (MAX) SQL 서버에서 계산 및 메시지 (130) 명령

을 실행할 때 다음과 오류를 받고, 수준 15, 상태 1, 줄 6 을 수행하기 위해 노력하고있어 집계 또는 부속 조회가 들어있는 표현식에 대한 집계 함수.

내가 오류가 모두

에 의해 발생 확신, SUM은 (MAX는 (변환 (플로트, 대체 ([AA_Now, 'N/A', '0')))) [AA2_Now]

은 SUM 같이 (MAX는 ((플로트 변환 대체 ([STF_Now, 'N/A', '0'))))와 같은 [STF2_Now]

하지만 오류가 발생하지 않고 다시 작성하는 방법을 모릅니다.

다음은 전체 코드입니다.

SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN 
     ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG 
     ,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc  
     ,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today] 
     ,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now] 
     ,SUM(MAX(convert(float,replace([AA_Now],'N/A','0')))) As [AA2_Now] 
     ,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today] 
     ,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today] 
     ,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today] 
     ,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today] 
     ,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today] 
     ,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now] 
     ,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now] 
     ,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty] 
     ,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty] 
     ,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today] 
     ,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now] 
     ,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now] 
     ,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now] 
     ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] 
     ,SUM(MAX(convert(float,replace([STF_Now],'N/A','0')))) As [STF2_Now] 
     ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name 
FROM   dbo.Sheet1$ RIGHT OUTER JOIN 
        dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN 
Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN 
      ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId 
      ,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today 
      ,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now 
      ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name 

저는 SQL Server에 대해 비교적 새로운 기능을 제공하므로 많은 도움을 얻을 수 있습니다.사전에

덕분에

SELECT dbo.CCA_Merged.lN  
    ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] 
    ,dbo.Sheet1$.LOB_name, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.AreaOfBusiness 
FROM   dbo.Sheet1$ RIGHT OUTER JOIN 
       dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN     
Group by stf_now ,AA_Now,dbo.CCA_Merged.lN,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name 
Order by AreaOfBusiness DESC 
아래
 
+----+---------+----------+---------------+----------------+ 
| LN | STF_Now | LOB_name | LifeCycleName | AreaOfBusiness | 
+----+---------+----------+---------------+----------------+ 
| A |  46 | BSW  | BS   | Business  | 
| B |  46 | BSW  | BS   | Business  | 
| C |  0 | BOSS  | BS   | Business  | 
| D |  112 | MSD  | BS   | Business  | 
| E |  112 | MSD  | BS   | Business  | 
| F |  42 | BHV  | BR   | Business  | 
| G |  23 | BCR  | BR   | Business  | 
| H |  23 | BHV  | BR   | Business  | 
| I |  55 | BSW2  | BS   | Business  | 
| J |  1 | BSW2  | BS   | Business  | 
| K |  46 | BSW  | BS   | Business  | 
| L |  112 | MSD  | BS   | Business  | 
| M |  112 | MSD  | BS   | Business  | 
| N |  57 | BSW  | BS   | Business  | 
| O |  0 | BOSS  | BS   | Business  | 
| P |  38 | MSD  | BS   | Business  | 
| Q |  38 | MSD  | BS   | Business  | 
| R |  19 | BHV  | BR   | Business  | 
| S |  0 | BCR  | BR   | Business  | 
| T |  19 | BHV  | BR   | Business  | 
| U |  2 | BSW  | BS   | Business  | 
| V |  1 | BSW  | BS   | Business  | 
| W |  57 | BSW  | BS   | Business  | 
| X |  38 | MSD  | BS   | Business  | 
| Y |  38 | MSD  | BS   | Business  | 
+----+---------+----------+---------------+----------------+ 

3에서 예상되는 결과가 열

에게

LOB_Name2을 (이 STF_Now의 맥스의 결과입니다 LN 추가 스크립트를 아래로 벗겨진 업데이트)

 
57 BSW 
0 BOSS 
112 MSD 
42 BHV 
23 BCR 
55 BSW2 
,451,515,

LifeCycleName2이

 
224 BS 
65 BR 

AreaOfBusiness2 (이 LOB_Name2의 롤업의 최대의 합계입니다)

 
289 Business 
+0

예제 데이터와 원하는 결과를 실제로 추가해야합니다. 필요한 것을 알아낼 수는 없습니다. –

+0

안녕하세요 JamesZ 몇 가지 샘플 데이터를 추가했습니다.감사합니다. – rasmac

답변

1
(이 LifeCycleName2의 롤업의 합)

당신은 maxsum 수 없습니다 왜냐하면 당신이 같은 그룹을 가지고 있다면 그것은 어쨌든 같은 금액이 될 것이기 때문입니다. 당신은 아마에 의해 같은 다른 그룹과의 내부 및 외부 부품이 필요합니다 : 각 제품에 대한 최대 비용을 가져옵니다

select 
    product_group, 
    sum(max_cost) 
from 
(
    select 
    product, 
    product_group, 
    max(cost) as max_cost 
    from 
    orders 
    group by 
    product_group,product 
) X 
group by product_group 

이 가상 SQL을하고 제품 그룹 레벨로 요약. 그게 내가 실제로 알아야 할 유일한 방법입니다. sum a max

+0

도움을 주셔서 감사합니다.하지만 안타깝게도 슬프게도 지금 내가 찾고있는 것이 었습니다. 나는 내가 성취하려는 것을 분명하게하기 위해 게시물을 편집했습니다. – rasmac

1

MAX 값을 합산하기 위해 다른 쿼리 수준을 추가해야합니다.

생각은 MAX에서 하나를 선택한 다음 SUM을 외부 쿼리를 사용하여 수행하는 것입니다. 아래 예에서 AVG 및 MAX를 사용하지만 모든 집계 함수를 사용할 수 있습니다. 당신이 모든 행 Max 값을 합계를 경우 다음 사용하는 각 그룹에 대한 모든 행을 Max 값을 합계하려는 경우

SELECT 
    LocationID, 
    MaxAverageSalePriceByLocation=MAX(AvgerageSalePriceByUserLocation) 
FROM 
(
    SELECT 
     UserID, 
     AvgerageSalePriceByUserLocation=AVG(SalePrice) 
    FROM 
     MyTable 
    GROUP BY 
     UserID,LocationID 
)AS A 
GROUP BY 
    LocationID 
+0

도움을 주셔서 감사합니다.하지만 불행히도 슬프게도 지금은 내가 찾고있는 것이 었습니다. 나는 달성하려는 내용을 명확하게하기 위해 게시물을 편집했습니다. – rasmac

1

다음 OVER()

Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER() AS [STF2_Now] 

를 사용 OVER(Partition by)

Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER(partition by grp1,grp2,..) AS [STF2_Now] 

참고 숫자 데이터를 float로 변환하면 앱으로 이어질 수 있습니다. 근사 문제 ... 숫자와 정밀도 및 눈금 사용

+0

도움을 주셔서 감사합니다.하지만 안타깝게도 슬프게도 지금은 내가 찾고있는 것이 었습니다. 나는 달성하려는 내용을 명확하게하기 위해 게시물을 편집했습니다. 나는 OVER()의 아이디어가 마음에 든다. 앞으로 더 많이 보게 될 것이다. 감사합니다 – rasmac

1

두 개의 중첩 집계를 수행하려고하므로 문제가 발생합니다. SUM (MAX)가 실제로 수행하려는 작업이면 대략적인 결과를 제공합니다.

그러나 James가 지적했듯이이 문제를 해결하기 위해서는 논리로 그룹을 만들어야합니다.

SELECT ... 
, SUM(AA2_Now_Max) 
... 
, SUM(STF2_Now_Max) 
FROM(
SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN 
    ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG 
    ,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc  
    ,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today] 
    ,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now] 
    ,MAX(convert(float,replace([AA_Now],'N/A','0'))) As [AA2_Now_Max] 
    ,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today] 
    ,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today] 
    ,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today] 
    ,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today] 
    ,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today] 
    ,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now] 
    ,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now] 
    ,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty] 
    ,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty] 
    ,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today] 
    ,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now] 
    ,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now] 
    ,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now] 
    ,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now] 
    ,MAX(convert(float,replace([STF_Now],'N/A','0'))) As [STF2_Now_Max] 
    ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name  
FROM   dbo.Sheet1$ RIGHT OUTER JOIN 
       dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN 
Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN 
     ,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId 
     ,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today 
     ,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now 
     ,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name) x 
GROUP BY ... 

이 다른 질문은 패턴과 비슷합니다. SQL: SUM the MAX values of results returned

+0

도움을 주셔서 감사합니다하지만 불행히도 그것은 슬픈 듯이 내가 뭘 찾고 있었는지 밝혀 졌 - 내가 노력하고있는 것을 명확하게하기 위해 게시물을 편집했습니다. – rasmac