need assistance with SELECT query

kskinne

Well-known Member
Joined
Feb 17, 2002
Messages
1,267
Office Version
  1. 365
Platform
  1. Windows
I am not very familiar with SQL and need some help with the following SELECT:

SELECT CpnyID, Acct, SubAcct, FiscYr, Balance
FROM AcctHist
WHERE FiscYr=2004

this sometimes returns data where there are multiple records that have the same account number (Acct) but a different subaccount number (SubAcct), for example like this:


  • 1130.00 00.00.00 100.00
    1130.00 00.10.00 100.00
    1130.00 00.20.00 100.00

is there any way that i can restructure this query so that it combines all the amounts for the accounts that match, regardless of the subaccount, so that the above results would be displayed with a total for the account and without the subaccount, like this:


  • 1130.00 300.00


thanks,
kevin
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi, I'm not familiar with SQL but have you tried editing this line :

SELECT CpnyID, Acct, SubAcct, FiscYr, Balance

to look like this :

SELECT CpnyID, Acct, FiscYr, Balance

i.e. remove the SubAcct but leave the rest the same

HTH, Andrew. :)
 
Upvote 0
Kevin

Are you actually creating this query in the QBE (Query By Example) view of Access?

If so goto View>Totals and under the balance field select Sum from the dropdown. Also remove the SubAcct field.
 
Upvote 0
I'm not sure this is correctly answering your question, but would using a GROUP BY clause help ?

->

SELECT CpnyID, Acct, FiscYr, SUM(Balance)
FROM AcctHist
WHERE FiscYr=2004
GROUP BY CpnyID, Acct, FiscYr

Just thought I'd mention it in case !
 
Upvote 0
getting rid of the sub field, doing a SUM on the balance field, and using a GROUP BY clause took care of it - thanks for the help


kevin
 
Upvote 0
need more assistance....

ok, here's another twist:

SELECT PerPost, CpnyID, Acct,
sum(Hours) as 'Hours',
sum(LaborDollars) AS 'Labor',
sum(AccLveSpreadInOH) AS 'Leave',
sum(TaxSpread) AS 'Tax',
sum(PensionSpread) AS 'Pension',
sum(InsurSpread) AS 'Insurance'
FROM xvr_XLabDetail
GROUP BY PerPost, CpnyID, Acct

now i am trying to bring in more than one summed column as shown above, but it will not allow me to do this. is there something wrong with the query syntax?

thanks,
kevin

EDIT:
more specifically, the error message reads: "parameters are not allowed in queries that cannot be displayed graphically" which leads me to believe there is something wrong with my syntax
 
Upvote 0
sorry for all the pesky questions, but here's another one. after trying to modify that query a bit, when I return the data to my sheet (I'm using Excel's query wizard to get into MS Query and manually modify the SQL statement), the columns are no longer in the same order of layout on the sheet as they are in the results window in MS Query. Any idea why, and is there a way to force the columns to appear in a certain order from left to right?

thanks in advance,
kevin
 
Upvote 0
never mind, i was able to fix it by removing the reference to CpnyID, this was screwing it up because there are multiple CpnyID's for an account, just like there are multiple sub accounts for an account.
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,248
Members
451,756
Latest member
tommyw

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