Queries :: Sum Field Based On Criteria - Return 0 If Not Met (AC2007)

Dec 6, 2013

I have two tables of data. One contains balances for a set of accounts (one unique balance per account, per date). The other contains transactions for a subset of the accounts (multiple records - or possibly none at all - per account, per date)

I'm trying to write a query to return the sum of the transactions from the Transaction table, for each unique account + date combination which is present in the Balance table.

Here is my SQL :

Quote:

SELECT tblBalances.BalDate, tblBalances.AccountID, Sum(tblTransactions.Amount) AS SumOfTransactions
FROM tblTransactions RIGHT JOIN tblBalances ON tblTransactions.AccountID = tblBalances.AccountID
WHERE (tblTransactions.TransDate=tblBalances.BalDate)
GROUP BY tblBalances.BalDate, tblBalances.AccountID;

This works fine with one major problem; if there are NO transactions for any given account + date in the Transaction table, I get no record for that combination in the dataset.

What I need is for the query to return a 0 in those situations (i.e. I should have as many records in my queried dataset as there are in my Balances table, but SumOfTransactions may be 0 for some of those balances)

I've tried the following but it has no effect on the outcome :

Quote:

SELECT tblBalances.BalDate, tblBalances.AccountID, Nz(Sum(tblTransactions.Amount),0) AS SumOfTransactions
FROM tblTransactions RIGHT JOIN tblBalances ON tblTransactions.AccountID = tblBalances.AccountID
WHERE (tblTransactions.TransDate=tblBalances.BalDate)
GROUP BY tblBalances.BalDate, tblBalances.AccountID;

Quote:

SELECT tblBalances.BalDate, tblBalances.AccountID, Sum(Nz(tblTransactions.Amount,0)) AS SumOfTransactions
FROM tblTransactions RIGHT JOIN tblBalances ON tblTransactions.AccountID = tblBalances.AccountID
WHERE (tblTransactions.TransDate=tblBalances.BalDate)
GROUP BY tblBalances.BalDate, tblBalances.AccountID;

View Replies


ADVERTISEMENT

General :: Return Based On Criteria - Records With Less Than 5 Characters Or Blank

Aug 28, 2013

I have a table with Zip CODE field. I uploaded my information to this table form an excel file. There were over 120K rows of data so no way i could do this in excel.

So I found that some of the ZIP codes are either blank or have less than 5 digits.

Can I build a a query with the ZIP code field that will return all zip code records with less than 5 characters or blank. I'd like to repair these zip codes and a update query will be my next step.

I repeat excel is not an option because when I try to sort by the Zip Code column my computer explodes (okay not literally) ....

View 3 Replies View Related

Queries :: Give Remarks With Number Based On Criteria Field

May 12, 2014

I want to get remarks as like this i have a query which is showing the total of different fields suppose the total is 77 and I want to give a crietria field by name of net level which shall work like this ..

if the number is from 0 to 33 then then the result should be C-1
if from 33 to 40 then C-2
if from 41 to 50 then C-3
if from 51 to 60 then B-1
if from 61 to 70 then B-2
if from 71 to 80 then B-3
if from 81 to 90 then A-2
if from 91 to 100 the A-1

View 8 Replies View Related

Queries :: Refer To Aggregate Function In WHERE Clause (AC2007)

Dec 10, 2013

I have a SQL query to gather data from a number of tables (balances, accounts, currencies)

Quote:

SELECT [tblBalances].[BalanceDate], [tblAccounts].[AccountNumber], [tblCurrencies].[Ccy], [tblBalances].[Amount], ([tblBalances].[Amount]*[tblRates].[FXRate]) AS AmountUSD
FROM (([tblBalances] INNER JOIN [tblAccounts] ON [tblBalances].[AccountID]=[tblAccounts].[AccountID]) INNER JOIN [tblCurrencies] ON [tblBalances].[CcyID]=[tblCurrencies].[CcyID]) INNER JOIN [tblRates] ON ([tblBalances].[BalanceDate]=[tblRates].[RateDate]) AND ([tblBalances].[CcyID]=[tblRates].[CcyID])
WHERE BalanceDate = #12/10/2013#

How do I add 'AmountUSD' to the WHERE clause (such that I can only return records above or below a certain value, for example)

Along the lines of :

Quote:

WHERE BalanceDate = #12/10/2013# AND AmountUSD>1000

I know it's an issue with referring to aggregated functions in the WHERE clause and you're supposed to use HAVING instead

View 12 Replies View Related

Queries :: Return Results That Exactly Match Criteria

May 9, 2013

I have 3 main tables: tblEmployees, tblJobs, and tblProcedures. (See attachment for relationship diagram and additional supplemental tables).A job can have multiple procedures and an employee can have multiple procedures too.

I need to write a query such that when searching by a specific job I can see all of the employees who are qualified for that job. This is done by seeing which employees have the procedures that belong to a job. But here's the catch: since a job can have multiple procedures, if an employee only has some of the procedures I don't want that particular employee to return as a search result. The employee must have ALL the procedures that belong to the selected job.

So for instance if I have:

tblJobs
Job1
tblEmployees
Emloyee1
Employee2

[code]...

If I search by Job1, I want only Employee2 to return as a result, NOT Employee1.I am at a lost for how to construct the SQL for something like that.

View 4 Replies View Related

Queries :: Expression / Criteria To Return Alphanumeric Values Only

Jun 30, 2015

How to write an expression/criteria in a query that will return only ID numbers with alpha numeric values. Example: My Id field contains both numeric 479621, 680530 and alpha numeric 132NAV100, 174NAV26a values. I want to run the query to return only the alpha numeric values.

I do not know SQL nor VBA.

View 11 Replies View Related

Queries :: Return Latest Record For Multiple Criteria

Mar 12, 2014

I have a table that contains readings from several pieces of equipment as well as the status of each one. Each record has a timestamp, equipment number, status, etc. What I want is to create a query that will return the latest record for each equipment number. Simplified example table:

Timestamp EquipmentNumber Status
Today ------Machine1 ----------Running
Today ------Machine2 ----------Running
Yesterday -Machine1 ----------Down
Yesterday -Machine2 ----------Running

There are more than 20 different Equipment numbers and they are read several times per day and sometimes some of them get missed. What I'm looking for is a way to get a list of all the machines with their latest reading so they can tell which machines are running and which are down based on the last time they were read (instead of specifying a date). I can get this for one machine with no problem. I'm having trouble getting it for more than one machine. I tried a union query (with just 2 of the machines included for testing) but it only returns the results from one machine:

Code:

SELECT TOP 1 TestCompressorRoundQuery.LoggedAt, TestCompressorRoundQuery.AssetNumber,
TestCompressorRoundQuery.CompressorID, TestCompressorRoundQuery.Status, TestCompressorRoundQuery.CompressorIntegrity, TestCompressorRoundQuery.Notes
FROM TestCompressorRoundQuery
WHERE (((TestCompressorRoundQuery.AssetNumber)="104399"))
UNION ALL

[Code] ....

I'd rather not have to create a seperate query for each machine and then join all of those together! I think perhaps a Union query might not be the correct approach. All the data is coming from only one table.

View 6 Replies View Related

Queries :: Return Result When Enter Month As Search Criteria

Jun 15, 2015

I cannot get my query to return result when I enter the month of "June" as search criteria. June is listed in the table and query, every other month is returned except June.

I have attached two example for your reference.

View 9 Replies View Related

Queries :: Failing To Filter By Criteria After Return Values From A Function?

May 15, 2013

I have two date fields in a table. I need to find the days between these dates and, if its greater than 7 days, I want to display the record in a report, so far, I have found a hand function that allows me to enter two dates and it returns a long data value representing the number of days in between the two dates. (google "I've developed the following code to count the business days between 2 dates." and its the second one that starts as SOLVED I made no mods to it as it does what i need it to do.

So, i added this to its own module within the data base for use within a query. My test query basically displays the unique ID, the start date and the end date and then displays the values returned from the function. here is the SQL:

SELECT [Main Table].[Unique ID], [Main Table].[Start date], [Main Table].[End Date], (Business_Days_Between([End Date],[Start date])) AS [Days between]
FROM [Main Table]
WHERE ((([Main Table].[Start date]) Is Not Null) AND (([Main Table].[End Date]) Is Not Null));

When ran, this Query works... However, when I enter a criteria like =2 or > 7, it says data type mismatch. I have even attempted the CInt() function to make sure its formated as int but i continue to get the same error.

View 11 Replies View Related

Queries :: How To Return Value Based On Row And Column Location

Aug 9, 2013

I inherited a spreadsheet that uses an Index (Match formulate to return a value. I want to use Access to manage the data going forward. I am just starting to build my tables in Access so I have some flexibility and want to do it right. Here is a sample of the data:

KeyEnroll JanFebMarAprMay
abc1/2013103521
def3/201300789
ghi5/201300008

So if Enroll date starts in Jan I want the 10 amt returned from the Jan column. If the Enroll date starts in March, for that client I want the March amy returned, etc.

results: for client "DEF" the amt returned should be "7" because their enrollment date is 3/2013.

View 4 Replies View Related

Queries :: Parameter Query Criteria To Return Dates With 2 Years Remaining

Sep 17, 2013

I am trying to create a parameter query to return dates that have 2 years remaining.

For example I have dates for when mortgages expire, and I want to recognise the dates that have two years remaining using a parameter query but I can't figure out if I use DateAdd or DateDiff.

View 2 Replies View Related

Return Value From A Table Based On A Text Field

Aug 27, 2006

I have created a text field on a form that uses the DateSerial function to return the value of last month e.g. Jul-06.

I have a table that contains months (e.g. Jul-06, Jun-06 etc) and a corresponding numeric value (e.g. 1,2,3).

I have another text box on the same form that I want to display the numeric value in based on the value that has been returned by the DateSerialfunction. What properties/control source should I set for this field? Or do I need to write some VBA?

Any help would be appreciated.

View 2 Replies View Related

Return Multiple Results Based On A Field

Aug 27, 2015

Event#
EID
Arrived Time
Code
Cancelled Time

[code]...

I need to find all EIDs where the Code is 611 where an Event# starting with F was Cancelled along with the time of the cancellation. For these results I also need the Arrived Time for the Event# starting with E for that EID.For instance, a result I'm looking for would be:

EID
Event# (F)
Cancelled Time
Event# (E)
Arrived Time

[code]...

View 1 Replies View Related

Queries :: Query To Run Before Report Based On Criteria Based From Two Combo Boxes On Form

Mar 20, 2013

I have a report that gets its data from a query. I need the query to run before the report based on criteria based from two combo boxes on a form.

View 3 Replies View Related

Queries :: Return Records Between Dates Based On 2 Date Fields In A Table

Apr 24, 2013

I have a table which includes a start date field and completion date field for housebuilding.

I am trying to extract all records that have either a started date or a completed date between 2 dates supplied by the user. I have tried to use Between on both fields but that doesn't return results between the fields.

It workd if I just do it on EITHER the start date field OR the completion date field so that implies to me that I need to break it into 2 queries, one returning start date recrods and the other returning completion date records but then I would need to have somthing that removes records that appear in both the start date and the completion date results.

View 7 Replies View Related

Queries :: Form Field Return A Null Then Look At Field In Table

Jun 10, 2013

Trying to run a query using criteria to populate the query by looking at information from a field on a form, if from is closed I need that criteria to look at the table and return all date in table.

View 14 Replies View Related

Tables :: Autopopulation Of Field From Another Table Based On Another Field Criteria

Dec 5, 2012

I have a main table which is autonumbered on unitid, I have another table which is linked via the unitid which holds items purchased for the main table, so the second table is a subform of the suppliers and will have multiple entries of the same unitid. What I would like to do is when an item is entered into the second table, check the main table to see if it exists, if it does populate the unitid field with the unitid from the main table, and if not exist create a new record in the main table with the next unitid number.

This in essence is what I want, bearing in mind that the tblinvoices is a subform and the tblmain is not open.

If ([tblinvoices]![Unit] = [tblmain]![Unit]) Then
[tblinvoices]![UnitID] = [tblinvoices]![UnitID]
End If

View 3 Replies View Related

Queries :: Return / Extract Just Text From Field

Aug 5, 2013

I have a field which contains "01501 PRIKLJ. VRHNIKA - LJ / Stevno mesto" .

1.) I would like to extract just text part from that field, how? *number is always on beginning but different 1-10000

2.) Optional, how could I delete in this new only text field part of text after / ...*some rows have some dont /

View 1 Replies View Related

Queries :: Search For Certain Text In A Field And Return Value

Jan 1, 2015

I am trying to search for specific text in a field and returns its value. For instance some of string includes Sub, L2L, Temp, Model, or MTM and I would like a query to return these values if found.

View 2 Replies View Related

Queries :: Obtain Field Info When Return Is Used

Feb 7, 2014

I'm trying to obtain all of the information in a text field for a query.

The user entered information and used a return (enter key) to enter information on a new line within the same field. The information after the return is not captured in the field in the query. Is there anyway I can capture all of this info?

View 3 Replies View Related

Criteria Based On The Value In Another Field

Feb 15, 2006

Hey guys, apologies in advance for a question which you may have answered many times, I have searched the net, my textbook and these forums but to no avail.

I have a table, 'tblStock'.

I have two fields within this table, 'Stock Level' and 'Minimum Stock'.

I want to run a query on this table, finding all records where the value in 'Stock Level' is LOWER than the value in 'Minimum Stock'.

How would I go about setting up this query? What criteria would I type? I have tried typing <"Minimum Stock" in the criteria for 'Stock Level' but it didn't work.

Thanks in advance... this will be a big help. :)

View 7 Replies View Related

Criteria Based On Value In Another Field

Apr 3, 2012

I have a query in Access with the following fields (grouped):

"Name" "Facility" "Number of Visits"

I want to limit the query to only pull those names that have at least one visit to a specific facility (let's say facility "A"), but I also need to see all of the other facilities that those who visited Facility A visited. So in this case "= "A"" in the criteria section of the Facility field wouldn't work. I think I need to plug something in the criteria section under the Name field, but I am not sure what.

View 1 Replies View Related

Queries :: Criteria Based On Checkbox

Aug 12, 2013

I have table.[reconciled] tat allows for a check box. i want to enter in the criteria filed in query design that i only want to see the unchecked or false entries. i have tried writing the criteria multiple ways and i cannot get it to filter out on my sub form query.

SELECT FuelmanImport_tbl.Reconciled, [Driver PIN].Division, [Driver PIN].[Transportation Supervisor],
FuelmanImport_tbl.[Transaction Date], FuelmanImport_tbl.[Card Number], [Driver PIN].Driver,
FuelmanImport_tbl.[Cardholder Name], FuelmanImport_tbl.[Prompted ID], FuelmanImport_tbl.[MCC Description],
FuelmanImport_tbl.[Merchant Name], FuelmanImport_tbl.[Merchant Address], FuelmanImport_tbl.[Merchant City],

[Code] ....

View 14 Replies View Related

Queries :: Return Records With Both Empty And Populated Field?

Apr 8, 2013

I am trying to create a query that returns records whether a field has data or not...

There are three fields in question, SSN, DOB (this is a date field), POB (this is a foreign key representing a state in the query shows the actual state). Now unless the criterion is different then I just need the answer for one I can reproduce.

I would like to do this in the criteria box in the query.

The query pulls from one table, some of the employees in this table have the three fields populated some don't. I would like the query to return all employees...

View 2 Replies View Related

Queries :: Return Records If One Field Is True And Another False

Jul 23, 2013

I am trying to create a query pulling from several tables. I will use the example below to illustrate what I'd like to do.

I have a field called 'Acc_Num', one called 'Stat_Code', and a third called 'Cat_Code'.

Each 'Acc_Num' can have multiple records because there are multiple Stat_Code and Cat_Code values.

What I want to do is isolate just the Acc_Num records where Stat_Code = 1 and Cat_Code equals A1.

I also want to isolate the Acc_num records where the Stat_code = 1 and where Cat_code does not exist.

View 7 Replies View Related

Queries :: Query To Return A Wildcard Search On A Field

May 21, 2015

How do I design a query to return a result in a wildcard format? So that I could enter a part of a name, and it returns all the names that include that part of name?

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved