473,698 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combining Querries results in repeat data

418 Contributor
I have 3 queries that show expenses on various projects/funds. I would like to combine the results and show various expenses on each project/fund etc.

But when I try to combine these 3 queries, the data gets repeated multiple times and the result becomes huge.

Query 1: qryAPDetail has 92 records
Query 2: qryPayDetail has 77 records
Query 3: qryAllocDetail has 26 records

How do I combine these queries to get a combined result without a single data bering repeated?

Thanks.
Jun 3 '09 #1
7 2738
ChipR
1,287 Recognized Expert Top Contributor
I don't think there is enough information to offer assistance. Are these queries dealing with the same dataset? Are you trying to combine them with a UNION?
Jun 3 '09 #2
MNNovice
418 Contributor
ChipR:

Here are the SQL info on my queries.

QUERY 1 (AP Expenses)
qryECHOAPDetail


Expand|Select|Wrap|Line Numbers
  1. SELECT tblECHO.ECHOID, tblGrants.GrantNo, tblAccounts.AcctNo, tblAP.InvoiceDt, qryGrantFunds.FundNo, tblProjects.ProjectNo, tblAP.InvoiceNo, tblVendors.VendorName, tblSubClass.SubClassNo, tblAP.ContractNo, tblECHO.ECHONo, tblGrants.FedRatio, tblECHO.DtSubmitted, tblAP.ApAmount, tblECHO.DtReceived, tblAP.APID, [ApAmount]*[FedRatio] AS FedAmount, Round([FedAmount]+0.0001) AS NetAmount
  2. FROM tblVendors INNER JOIN (tblGrants RIGHT JOIN ((tblGrantFunds INNER JOIN qryGrantFunds ON tblGrantFunds.GrantFundID = qryGrantFunds.GrantFundID) INNER JOIN (tblECHO INNER JOIN (((tblAP INNER JOIN tblAccounts ON tblAP.AccountID = tblAccounts.AccountID) INNER JOIN tblSubClass ON tblAP.SubClassID = tblSubClass.SubClassID) INNER JOIN tblProjects ON tblAP.ProjectID = tblProjects.ProjectID) ON tblECHO.ECHOID = tblAP.ECHOID) ON tblGrantFunds.GrantFundID = tblAP.GrantFundID) ON tblGrants.GrantID = tblGrantFunds.GrandID) ON tblVendors.VendorID = tblAP.VendorID
  3. ORDER BY tblGrants.GrantNo, qryGrantFunds.FundNo, tblProjects.ProjectNo;

QUERY 2 (Payroll Expenses)
qryECHOPayDetai l



Expand|Select|Wrap|Line Numbers
  1. SELECT tblECHO.ECHOID, tblGrants.GrantNo, tblAccounts.AcctNo, qryGrantFunds.FundNo, tblProjects.ProjectNo, tblPayroll.PayrollNo, tblPayroll.PayrollDate, tblSubClass.SubClassNo, tblECHO.ECHONo, tblECHO.DtSubmitted, tblPayroll.PayAmount, tblECHO.DtReceived, tblPayroll.PayrollID
  2. FROM tblSubClass INNER JOIN (tblProjects INNER JOIN (tblGrants RIGHT JOIN (tblECHO INNER JOIN (tblAccounts INNER JOIN ((tblGrantFunds INNER JOIN qryGrantFunds ON tblGrantFunds.GrantFundID = qryGrantFunds.GrantFundID) INNER JOIN tblPayroll ON tblGrantFunds.GrantFundID = tblPayroll.GrantFundID) ON tblAccounts.AccountID = tblPayroll.AccountID) ON tblECHO.ECHOID = tblPayroll.ECHOID) ON tblGrants.GrantID = tblGrantFunds.GrandID) ON tblProjects.ProjectID = tblPayroll.ProjectID) ON tblSubClass.SubClassID = tblPayroll.SubClassID
  3. ORDER BY tblGrants.GrantNo, qryGrantFunds.FundNo, tblProjects.ProjectNo;


QUERY 3 (Allocation Cost)
qryEchoAllocati onDetail


Expand|Select|Wrap|Line Numbers
  1. SELECT tblECHO.ECHOID, tblGrants.GrantNo, tblAccounts.AcctNo, qryGrantFunds.FundNo, tblProjects.ProjectNo, tblAllocation.AllocationDate, tblSubClass.SubClassNo, tblECHO.ECHONo, tblECHO.DtSubmitted, tblAllocation.Amount, tblECHO.DtReceived, tblAllocation.AllocationID
  2. FROM tblSubClass INNER JOIN (tblProjects INNER JOIN (tblGrants RIGHT JOIN (tblECHO INNER JOIN (tblAccounts INNER JOIN ((tblGrantFunds INNER JOIN qryGrantFunds ON tblGrantFunds.GrantFundID = qryGrantFunds.GrantFundID) INNER JOIN tblAllocation ON tblGrantFunds.GrantFundID = tblAllocation.GrantFundID) ON tblAccounts.AccountID = tblAllocation.AccountID) ON tblECHO.ECHOID = tblAllocation.ECHOID) ON tblGrants.GrantID = tblGrantFunds.GrandID) ON tblProjects.ProjectID = tblAllocation.ProjectID) ON tblSubClass.SubClassID = tblAllocation.SubClassID
  3. ORDER BY tblGrants.GrantNo, qryGrantFunds.FundNo, tblProjects.ProjectNo;
What I want is a report to show combined result of these 3 queries. Which is why I need a combined query first.

The results should reflect expenses by Fund Number (FundNo). So that we can see how much money is spent in each Fund by SubClassNo and by Project within each category (AP, Payroll and Allocation).

I tried other threads that deal with Cross Tab Query, Union Query but I was not successful in getting the result I need.

Hope this makes sense. Thanks for your offer of help and for your time.
Jun 3 '09 #3
ChipR
1,287 Recognized Expert Top Contributor
It seems you want to list these things by Fund Number, so add all 3 queries to a new query that includes a table (or query) listing the Fund Numbers. Join them on the Fund Numbers, and then add the fields you need.
Jun 3 '09 #4
MNNovice
418 Contributor
ChipR:

It didn't work for me. I must be doing something wrong and am certain I didn't quite understand what you have asked me to do. But here is the new query SQL

Expand|Select|Wrap|Line Numbers
  1. SELECT tblFunds.FundNo, qryECHOAPDetail.ProjectNo, qryEchoAllocDetail.ProjectNo, qryECHOPayDetail.ProjectNo, qryECHOAPDetail.NetAmount, qryEchoAllocDetail.Amount, qryECHOPayDetail.PayAmount
  2. FROM qryECHOPayDetail INNER JOIN ((tblFunds INNER JOIN qryECHOAPDetail ON tblFunds.FundNo = qryECHOAPDetail.FundNo) INNER JOIN qryEchoAllocDetail ON tblFunds.FundNo = qryEchoAllocDetail.FundNo) ON qryECHOPayDetail.FundNo = qryECHOAPDetail.FundNo;
It gives me 1,563 records some of which are repeated. I tried to pull data for ProjectNo only. Since I didn't succeed with the ProjectNo. I didn't try with SubClassNo.

What I needed should look like this:

FundNo 871

EchoNo 09-066

SubClassNo 0999
ProjectNo 61750
ApAmount 40,529
PayAmount 13,319
AllocationCost 6,771


ProjectNo 62314
ApAmount 0
PayAmount 4,110
AllocCost 2,066


ProjectNo 65652
ApAmount 0
PayAmount 40
AllocCost 196

Project 61750 Total 60,419
Project 62314 Total 6,176
Project 65652 Total 236

SubClass 0999 Total 66,831
Fund 871 Total 66,831

It needs to combine these costs for all ECHOs. I only showed example from ECHO No. 09-066

Hope this makes sense. Thanks for your time and effort in finding a solution for me.
Jun 3 '09 #5
MNNovice
418 Contributor
Well, I found a solution to my union query with the help from Denburt. Here is what the SQL and it's working. Thanks for your time ChipR.

Expand|Select|Wrap|Line Numbers
  1. SELECT ECHOID, FundNo, SubClassNo, ProjectNo, Amount,"Allocation" as Category
  2. FROM qryEchoAllocDetail
  3.  
  4. UNION SELECT ECHOID, FundNo, SubClassNo, ProjectNo, PayAmount, "Payroll" as Category
  5. FROM qryECHOPayDetail;
  6.  
  7.  
  8. UNION SELECT ECHOID, FundNo, SubClassNo, ProjectNo, ApAmount, "AP" as Category
  9. FROM qryECHOAPDetail;
Jun 3 '09 #6
Denburt
1,356 Recognized Expert Top Contributor
A quick note, a Union Select will not show any duplicates, you might want to use a UNION ALL SELECT instead depending on the results you are looking for.
Here is a link that you may find useful.
http://www.w3schools.com/sql/sql_union.asp
Jun 3 '09 #7
NeoPa
32,569 Recognized Expert Moderator MVP
Joining data from different record sources can be done in many ways depending on exactly how you want them joined.

I seriously suggest you follow the link Den provided, as understanding this area of database work would benefit you greatly M (if my guess is right).
  • UNION -> Used for when you want want multiple data sources to be treated as a continued set of data. Essentially a single data source, but coming from multiple places.
  • JOIN -> Used for when you want the data from multiple sources to appear all together in a single output record. An example would be for lookups. Some data from both sources should match for this method (Normally PK == FK). Types of usable JOINs in Access are INNER JOIN, LEFT JOIN (outer) and RIGHT JOIN (outer).
  • FULL OUTER -> This is a full Cartesian Product. You were probably experiencing a little of this earlier. This is where each record from one source matches up with each record of every other one.
    EG. Table A has three records - A, B & C and Table 2 has two records - 1 & 2.
    With a FULL OUTER JOIN the results would be :
    A 1
    A 2
    B 1
    B 2
    C 1
    C 2

    It is possible to restrict the output records using filtering (WHERE clause), but if not you see the full cartesian product of the multiple record sources.
Jun 4 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4977
by: Alan | last post by:
I'm just about to start a project that needs to combine the results of a SQL Server query with the results of an Index Server query. The basic idea is that the user enters/selects a bunch of search criteria on a form. Most of the criteria selected by the user will be used to select records from the database - standard WHERE clause stuff - but the user can also enter free-text that should be searched for in associated uploaded documents. The...
8
8355
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though the records are sorted by row numbers. (I had to split the fields to different sheets because Excel has a limit of 256 fields in each sheet) My sheets are quite large (~55,000 rows and 200 columns each) and I'll have to repeat this action many...
3
1727
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I ideally I would like to have the user enter their results and then have a master summary created with the different word or HTML files on submit. (condition 1 + condition 2 = combining/displaying corresponding HTML files) I've gotten as far as...
1
1371
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this. We just converted to A2002 and some of my querries have stopped working and I can't figure out why. Basically, anything with a "NOT" is not working. For example: select * from b321bl where stdverifyby <> "demonstration"
5
2106
by: M.Stanley | last post by:
Hi, I'm attempting to create a query that will combine 2 columns of numbers into one. The followng comes from 1 table with 4 fields (A,B,C,D) A B RESULT 700 000 700000 700 001 700001 700 002 700002 ..
1
1439
by: striker77 | last post by:
i get a list of results based on an id and based on a status value that can be 'active', 'inprocess' or 'cancel' - it is possible that the same user has both 'active' and 'inprocess' states associated with him and i want to combine the values for this user and show this result as 1 row instead of 2 rows - and I want to concatenate selected values that differ , so something like this - status='Active+InProcess' so i currently get something...
3
1936
by: Jacko7289 | last post by:
Hi, It's me again :-) Just wondering if its possible to create one report using the output of two or more SQL Querries. Currently i've got two SQL querries. one generates the total number of graduate students for the year (SQLGrad) and the other number of graduate students per course per year (SQLGradcourse). Now, what i want to happen (if possible) is that once i clicked the report button it will generate in just one page both the results...
2
4127
by: J055 | last post by:
Hi I need to search a number of DataTables within a DataSet (with some relationships) and then display the filtered results in a GridView. The Columns that need to be displayed come from 2 of the tables but the search needs check 3 or 4 tables. Do I need to create a new DataTable which has the columns from both the tables so I can display in the Gridview or can I get the columns somehow from the DataSet without creating a new table? I...
3
2838
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.