Как объединить все строки, а затем суммировать все столбцы в sqlserver
У меня есть два стола
Fee_Assign_to_Students
и Fee_Assign_Waiver_to_Students
и обе таблицы имеют такие столбцы, какS_Adm_No | FHeadId | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar
Я хочу объединить все строки, а затем суммировать все столбцы месяца отдельно.
Мой код упоминается ниже
Fee_Assign_to_Students
имеет значение как S_Adm_No | FHeadId | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 | 1 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 1001 | 2 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 1001 | 3 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100 | 100
Fee_Assign_Waiver_to_Students
имеет значение как S_Adm_No | FHeadId | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 | 1 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 1001 | 2 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 1001 | 3 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50
после вычитания из
Fee_Assign_to_Students
через sql querry. Он приходит как S_Adm_No | FHeadId | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 | 1 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 1001 | 2 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 1001 | 3 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50 | 50
Но я хочу как
S_Adm_No | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar 1001 | 150 | 150 | 150 | 150 | 150 | 150 | 150 | 150 | 150 | 150 | 150 | 150
пожалуйста кто нибудь помогите мне
мой код приведен ниже..
Что я уже пробовал:
SELECT FA.S_Adm_No,FA.[FHeadId], FA.APR - ISNULL(FET.APR,0) AS APR, FA.MAY - ISNULL(FET.MAY,0) AS MAY, FA.JUN - ISNULL(FET.JUN,0) AS JUN, FA.JUL - ISNULL(FET.JUL,0) AS JUL, FA.AUG - ISNULL(FET.AUG,0) AS AUG, FA.Sept - ISNULL(FET.SEP,0) AS SEP, FA.OCT - ISNULL(FET.OCT,0) AS OCT, FA.NOV - ISNULL(FET.NOV,0) AS NOV, FA.DEC - ISNULL(FET.DEC,0) AS DEC, FA.JAN - ISNULL(FET.JAN,0) AS JAN, FA.FEB - ISNULL(FET.FEB,0) AS FEB, FA.MAR - ISNULL(FET.MAR,0) AS MAR FROM Fee_Assign_to_Students FA LEFT JOIN (SELECT FE.S_Adm_No,FE.[FHeadId], SUM(FE.E_Apr) AS APR, SUM(FE.E_MAY) AS MAY, SUM(FE.E_JUN) AS JUN, SUM(FE.E_JUL) AS JUL, SUM(FE.E_AUG) AS AUG, SUM(FE.E_SEP) AS SEP, SUM(FE.E_OCT) AS OCT, SUM(FE.E_NOV) AS NOV, SUM(FE.E_DEC) AS DEC, SUM(FE.E_JAN) AS JAN, SUM(FE.E_FEB) AS FEB, SUM(FE.E_MAR) AS MAR FROM Fee_Assign_Waiver_to_Students FE GROUP BY FE.S_Adm_No,FE.FHeadId) AS FET ON (FA.S_Adm_No=FET.S_Adm_No) AND (FA.FHeadId=FET.FHeadId)WHERE FA.S_Adm_No='1001'