# Error - Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression. • ### Question

• User-797751191 posted

Hi

` SELECT T0.DocEntry,T3.[ChapterID], Sum(T1.[LineTotal]), ISNULL((Select (case when sum(T5.RvsChrgTax)<=0 then sum(T5.TaxSum/T0.DocRate) else 0 end) From INV4 T5 Where T5.DocEntry = T0.DocEntry and T5.staType = -100 and T5.RelateType = 1),0) [CGSTAmt], ISNULL((Select (case when sum(T5.RvsChrgTax)<=0 then sum(T5.TaxSum/T0.DocRate) else 0 end) From INV4 T5 Where T5.DocEntry = T0.DocEntry and T5.staType = -110 and T5.RelateType = 1),0) [SGSTAmt],ISNULL((Select (case when sum(T5.RvsChrgTax)<=0 then sum(T5.TaxSum/T0.DocRate) else 0 end) From INV4 T5 Where T5.DocEntry = T0.DocEntry and T5.staType = -120 and T5.RelateType = 1),0) [IGSTAmt]FROM OINV T0 INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode] INNER JOIN OCHP T3 ON T1.[HsnEntry] = T3.[AbsEntry] group by T0.DocEntry,T3.[ChapterID]`

Thanks

Sunday, January 12, 2020 3:10 PM

### All replies

• User-719153870 posted

Hi jsshivalik,

`ISNULL((Select (case when sum(T5.RvsChrgTax)<=0 then sum(T5.TaxSum/T0.DocRate) else 0 end) From INV4 T5 Where T5.DocEntry = T0.DocEntry and T5.staType = -100 and T5.RelateType = 1),0)`

The error should be caused by these three lines, seems you are comparing multiple records to a single record.

You can try to change these three to static value, such as 0, and run the query again see if the problem occurs again, if not then i'm right that these three lines caused the problem.

In this case, we will need your stuctures and sample data of all your 6 tables to reproduce the problem, or if you can reproduce the problem with a simple demo would be better.

You can refer to this and seems you need to change your code like below:

Notice: this is not tested.

`ISNULL((Select (case when sum(T5.RvsChrgTax)<=0 then sum(T5.TaxSum/T0.DocRate) else 0 end) From INV4 T5 inner join on T5.DocEntry = T0.DocEntry where T5.staType = -100 and T5.RelateType = 1),0)`

Best Regard,

Yang Shen

Monday, January 13, 2020 9:11 AM
{"ft_lang_label":"__label__en","ft_lang_prob":0.86583817,"math_prob":0.9543076,"size":1009,"snap":"2022-27-2022-33","text_gpt3_token_len":288,"char_repetition_ratio":0.106467664,"word_repetition_ratio":0.13496932,"special_character_ratio":0.27254707,"punctuation_ratio":0.13761468,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9799979,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T03:46:01Z\",\"WARC-Record-ID\":\"<urn:uuid:e36dcfa8-709c-4096-8012-11f32c2a80c8>\",\"Content-Length\":\"56972\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:79138d51-3a66-4562-bd40-73e8069aedc3>\",\"WARC-Concurrent-To\":\"<urn:uuid:a777a4e2-136f-40f0-9e3c-6aee77dcb9d4>\",\"WARC-IP-Address\":\"13.107.246.40\",\"WARC-Target-URI\":\"https://social.msdn.microsoft.com/Forums/en-US/6aeb89ad-4a3f-49df-b30b-cadfe25cf78a/error-multiple-columns-are-specified-in-an-aggregated-expression-containing-an-outer-reference-if?forum=aspsqlserver\",\"WARC-Payload-Digest\":\"sha1:OIM565VQFBHQE3MIWOQM2OJWARPWNFR3\",\"WARC-Block-Digest\":\"sha1:GFXXMGULXUCEQQUJKZVBEL4DA4J6726T\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103620968.33_warc_CC-MAIN-20220629024217-20220629054217-00372.warc.gz\"}"}