Group By SQL Query

Fredek

Board Regular
Joined
Mar 8, 2011
Messages
65
Hi guys,

I am getting "SQL command not properly ended" with he below query, any idea what I'm doing wrong?

Code:
SELECT 
Bal_Dte_ID,
EQ_Branch_Name,
Acct_Curr_Cde as Bal_Curr_Cde,
VALUE_BAL as SUM_BASE_BALANCE,
VALUE_BAL_USEQ as SUM_USD_BALANCE
From dwrpt.T_Risk_Merge
where  
(Bal_Dte_ID>='20180401' and bal_dte_id<='20180401')
Order by EQ_Branch_Name
Group by Acct_Curr_Cde;
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi Fredek
You would need the group by only if the fields you have labelled as Sum.... are an aggregation such as sum for example:

then you need to group by the rest of the fields in the query
Code:
SELECT dwrpt_T_Risk_Merge.Bal_Dte_ID, dwrpt_T_Risk_Merge.EQ_Branch_Name, dwrpt_T_Risk_Merge.Acct_Curr_Cde AS Bal_Curr_Cde, Sum(dwrpt_T_Risk_Merge.VALUE_BAL) AS SUM_BASE_BALANCE, Sum(dwrpt_T_Risk_Merge.VALUE_BAL_USEQ) AS SUM_USD_BALANCE
FROM dwrpt_T_Risk_Merge
WHERE (((dwrpt_T_Risk_Merge.[Bal_Dte_ID])>='20180401' And (dwrpt_T_Risk_Merge.[Bal_Dte_ID])<='20180401'))
GROUP BY dwrpt_T_Risk_Merge.Bal_Dte_ID, dwrpt_T_Risk_Merge.EQ_Branch_Name, dwrpt_T_Risk_Merge.Acct_Curr_Cde;

or simplify the query as follows

Code:
SELECT 
Acct_Curr_Cde as Bal_Curr_Cde,
sum(VALUE_BAL) as SUM_BASE_BALANCE,
sum(VALUE_BAL_USEQ) as SUM_USD_BALANCE
From dwrpt.T_Risk_Merge
where  
(Bal_Dte_ID>='20180401' and bal_dte_id<='20180401')
Order by EQ_Branch_Name
Group by Acct_Curr_Cde,;

Then you can link this to a select for bringing out the branch and date.
the group by clause has to contain all of the fields that are not being aggregated as you need a total value per line of output.

so an option would be something like this:
Code:
SELECT Distinct RiskMerge.Bal_Dte_ID, RiskMerge.EQ_Branch_Name, RiskMerge.Acct_Curr_Cde AS Bal_Curr_Cde, qryBalances.SUM_BASE_BALANCE, qryBalances.SUM_USD_BALANCE
FROM dwrpt_T_Risk_Merge AS RiskMerge 
INNER JOIN 
(
SELECT dwrpt_T_Risk_Merge.Acct_Curr_Cde AS Bal_Curr_Cde, Sum(dwrpt_T_Risk_Merge.VALUE_BAL) AS SUM_BASE_BALANCE, Sum(dwrpt_T_Risk_Merge.VALUE_BAL_USEQ) AS SUM_USD_BALANCE
FROM dwrpt_T_Risk_Merge
WHERE (((dwrpt_T_Risk_Merge.[Bal_Dte_ID])>='20180401' And (dwrpt_T_Risk_Merge.[Bal_Dte_ID])<='20180401'))
GROUP BY dwrpt_T_Risk_Merge.Acct_Curr_Cde
)
qryBalances ON RiskMerge.Acct_Curr_Cde = qryBalances.Bal_Curr_Cde;

Depends how you want to use it.

Ron
 
Upvote 0

Forum statistics

Threads
1,223,610
Messages
6,173,339
Members
452,510
Latest member
RCan29

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top