473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Building a query based on one field’s value but grouping it by another…..

kcdoell
230 New Member
Hello:

I just learned how to put crosstabs queries together but this one in particular is adding a new dimension in which I was hoping someone could give me some direction. I have the following tables

The Tables:

Expand|Select|Wrap|Line Numbers
  1. Table Name =  tblStaticAllForecast        
  2.  
  3. Field    Type    IndexInfo
  4. YearID;    Number;    Year
  5. MonthID;    Number;    Foreign Key for the Month ID
  6. CreditRegIDFK;    Number;    Foreign Key for the Credit Region ID
  7. ProductIDFK;    Number;    Foreign Key for the Product ID
  8. GWP;    Number;    Gross Premium
  9. NWP;    Number;    Gross Premium
  10. Fweek;    Number;    Week number of a month (1-5)
  11. Binding_Percentage;    Number;    Binding Percentage
  12.  
Expand|Select|Wrap|Line Numbers
  1. Table Name =  tblPrior        
  2.  
  3. CreditRegIDFK;    Number;    Foreign Key for the Credit Region ID
  4. Field    Type    IndexInfo
  5. YearID;    Number;    Year
  6. MonthID;    Number;    Foreign Key for the Month ID
  7. GWP;    Number;    Gross Premium
  8. NWP;    Number;    Gross Premium
  9. ProductIDFK;    Number;    Foreign Key for the Product ID
  10. ProductName;    Text;    Product Name
  11.  
Expand|Select|Wrap|Line Numbers
  1. Table Name =  tblbudget        
  2.  
  3. CreditRegIDFK;    Number;    Foreign Key for the Credit Region ID
  4. Field    Type    IndexInfo
  5. YearID;    Number;    Year
  6. MonthID;    Number;    Foreign Key for the Month ID
  7. GWP;    Number;    Gross Premium
  8. NWP;    Number;    Gross Premium
  9. ProductIDFK;    Number;    Foreign Key for the Product ID
  10. ProductName;    Text;    Product Name
  11.  
Expand|Select|Wrap|Line Numbers
  1. Table Name =  tblCreditRegion        
  2.  
  3. Field    Type    IndexInfo
  4. CreditRegID;    AutoNumber;    The Credit ID
  5. CreditRegionName;    Text;    Product Name
  6.  
Expand|Select|Wrap|Line Numbers
  1. Table Name =  tblproduct        
  2.  
  3. Field    Type    IndexInfo
  4. ProductID;    AutoNumber;    The Product ID
  5. ProductName;    Text;    Product Name
  6. ProductType; Text;    Product  Type
  7.  
I need to run a report where I have to show all of the credit regions [GWP] values but also group them by [ProductType]. I got to the point where I have a query that displays all of the Credit Regions [GWP] values (12 in this case). This is comprised of three other queries (“ctqQueryC_G”, “QryProd_Credit_Bud”, & “QryProd_Credit_Prior”) that I joined together on [CreditRegID] via my “tblCreditRegion” table:

Query Name = qryGWP_Prod_Credit:

Expand|Select|Wrap|Line Numbers
  1.  SELECT tblCreditRegion.CreditRegionName, CLng(Nz([QryProd_Credit_Bud.SumOfGWP],0)) AS GWP_Bud, CLng(Nz([Week 1],0)) AS Week_1, CLng(Nz([Week 2],0)) AS Week_2, CLng(Nz([Week 3],0)) AS Week_3, CLng(Nz([Week 4],0)) AS Week_4, CLng(Nz([Week 5],0)) AS Week_5, CLng(Nz([QryProd_Credit_Prior.SumOfGWP],0)) AS GWP_PRI
  2. FROM ((tblCreditRegion LEFT JOIN ctqQueryC_G ON tblCreditRegion.CreditRegID = ctqQueryC_G.CreditRegIDFK) LEFT JOIN QryProd_Credit_Bud ON tblCreditRegion.CreditRegID = QryProd_Credit_Bud.CreditRegIDFK) LEFT JOIN QryProd_Credit_Prior ON tblCreditRegion.CreditRegID = QryProd_Credit_Prior.CreditRegIDFK
  3. GROUP BY tblCreditRegion.CreditRegionName, CLng(Nz([QryProd_Credit_Bud.SumOfGWP],0)), CLng(Nz([Week 1],0)), CLng(Nz([Week 2],0)), CLng(Nz([Week 3],0)), CLng(Nz([Week 4],0)), CLng(Nz([Week 5],0)), CLng(Nz([QryProd_Credit_Prior.SumOfGWP],0));
  4.  
Now I need to get the [ProductType] in the query so that I can group it by Product Type in my report. I tried to include my product table to the above query but I hit errors. I also tried to add [ProductType] to my individual queries (“ctqQueryC_G”, “QryProd_Credit_Bud”, & “QryProd_Credit_Prior”) and then somehow include it in the above mention final query but got lost.

Can anyone give me a sense of how to go about incorporating this additional grouping/breakout? I mean breakout because that is what I would be doing; breaking out the [GWP] values by Credit Region (Which I already did above) but then by Product Type........

Thanks,

Keith.
May 12 '08 #1
12 2440
nico5038
3,080 Recognized Expert Specialist
Hi Keith

I'm a bit stunned about the tablefields, especially in tblPrior and tblBudget to find:
ProductName; Text; Product Name
They should be derived from the tblProduct when needed...

I also wonder why there's a table tblPrior, is it to store previous YearMonth's ?

OK, for the query I would add the tblProduct with a JOIN to the "master table" tblCreditRegion, when this always hold the product of the entire row. When the products differ in the queries the table is LEFT JOINing to, I would like to know why and how the structure should be.
Having JOINed the table will enable the addition of the ProductType.

Nic;o)
May 12 '08 #2
kcdoell
230 New Member
Hi Keith

I'm a bit stunned about the tablefields, especially in tblPrior and tblBudget to find:
ProductName; Text; Product Name
They should be derived from the tblProduct when needed...

I also wonder why there's a table tblPrior, is it to store previous YearMonth's ?....

Nic;o)

Nico:

Yes, it is to store previous years months. In fact, the Budget table and the Prior table are not directly tied to my DB in the sense that records are being written to them via my DB. Rather they are for reporting purposes. These are in fact tables that are loaded at the begin of the year and then left static. I originally requested that they give me “Product ID” so that I could tie them to my tables that were dynamic (Each record represents a [GWP] value for each [ProductID]). I imported these tables and then did a one to many relationship on ProductID but I am lost on how to tie Product Type to my queries (In a non-direct way) so I included both the” “Product Name” “Product Type” to both the budget and prior tables, so far it was the only way I knew how to do it…. If you have a better way of referencing the Product Type that would be greatly appreciated…. I figure there is a way via queries but I simply don’t know how……
May 12 '08 #3
nico5038
3,080 Recognized Expert Specialist
Just check the result of the query and check or all productID's (and thus the names) are identical on one row. When they are you can JOIN like I proposed, otherwise you have a problem, as different products can have different types and which one to take for reporting.....

Nic;o)
May 12 '08 #4
kcdoell
230 New Member
Hi Keith

OK, for the query I would add the tblProduct with a JOIN to the "master table" tblCreditRegion, when this always hold the product of the entire row. When the products differ in the queries the table is LEFT JOINing to, I would like to know why and how the structure should be.
Having JOINed the table will enable the addition of the ProductType.

Nic;o)

I am confused, you want me to join my tblProduct to my tblCreditRegion?? How can I join them if the tblCreditRegion does not have the field ProductID??

Thanks for the reply.

Keith.
May 12 '08 #5
nico5038
3,080 Recognized Expert Specialist
Oops, mixed it with your first table tblStaticAllForecast.
I can't see what has been coded in the queries the tblCreditRegion is linked to.
Just add to those queries the tblProduct and include the ProductType to see or each individual row has the same product (and thus ProductType)

Nic;o)
May 12 '08 #6
kcdoell
230 New Member
Oops, mixed it with your first table tblStaticAllForecast.
I can't see what has been coded in the queries the tblCreditRegion is linked to.
Just add to those queries the tblProduct and include the ProductType to see or each individual row has the same product (and thus ProductType)

Nic;o)

Nico:

I am still stuck on this one... I added the Product table and thus the product type field to the individual queries. Now in my final query I have the three queries joined to my Credit Region so that I can get all the region names. I thought I would only get 36 records since there is only 12 credit regions and 3 product types. Instead, I am getting 85 records. Looks like it is duplicating some records in the query even though I did not change the join property above.

Do I need to now add the Product table to this final query?? and if so, should it be joined with something. Like I said, I am already joining my Credit Region table with the CreditRegionID so I don't know how to factor in the Product Type. Is this a double join move??

This report displays all Credit Regions but is broken up by Product Type.

Thanks,

Keith.
May 13 '08 #7
nico5038
3,080 Recognized Expert Specialist
Hmm, hard for me to see your tables structure in the queries, but I get the impression you would need a UNION to handle this.

F.e.: create a query for tblPrior and JOIN with the Credit Regions and the Product, now do the same for the other two tables and make sure that they have all three the same number of columns and that the fields correspond.
Now create a new query like:
select * from qryUNION1
UNION
select * from qryUNION2
UNION
select * from qryUNION3;

Nic;o)
May 13 '08 #8
kcdoell
230 New Member
Hmm, hard for me to see your tables structure in the queries, but I get the impression you would need a UNION to handle this.

F.e.: create a query for tblPrior and JOIN with the Credit Regions and the Product, now do the same for the other two tables and make sure that they have all three the same number of columns and that the fields correspond.
Now create a new query like:
select * from qryUNION1
UNION
select * from qryUNION2
UNION
select * from qryUNION3;

Nic;o)

Okay but one of my queries has a field that is [FWeek] which my other two did not have. Below was my Union statement:

Expand|Select|Wrap|Line Numbers
  1. select * from ctqQueryC_G
  2. UNION
  3. select * from QryProd_Credit_Bud
  4. UNION
  5. select * from ctqQueryC_G;
  6.  
Afterwards as you warned, I got the following error:

Expand|Select|Wrap|Line Numbers
  1. The number of columns in the two selected tables or queries of a union query do not match. (Error 3307)
  2. The two tables or queries joined by the UNION operation must generate the same number of columns. Remove columns from the SELECT statement that has too many columns or include more columns in the SELECT statement that has too few.
How do I work around that?? That is to say I dont understand the work around they are describing "statement that has too many columns or include more columns in the SELECT statement that has too few."

Currently when I run the individual queries I get the following visual:

ctqQueryC_G:

ProductType___CreditRegIDFK___Week 1___Week 2___Week 3___Week 4___Week 5
Casualty___________2___________3517______________3 471

QryProd_Credit_Bud:

ProductType_____CreditRegIDFK_____SumOfNWP______Su mOfGWP
Casualty__________Atlanta___________2885__________ 3398

QryProd_Credit_Prior:

ProductType______CreditRegIDFK______SumOfNWP______ SumOfGWP
Casualty____________Atlanta___________2828________ __3316

Do I have to somehow create dummy fields so that every query has seven columns since my "ctqQueryC_G" has seven?

Thanks for getting back to me..

Keith.
May 13 '08 #9
kcdoell
230 New Member
Nico:

Before responding to my last reply let me try something, I have been reading up on Union queries, something I never did before.....

I will get back

Thanks and thanks for the idea.

Keith.
May 13 '08 #10
nico5038
3,080 Recognized Expert Specialist
You already gave the solution: dummy columns :-)
Just make sure they are of the same "data type". For numbers use 0 and text use "" (Empty string)
The names of the columns of the first query are used for the final result, so use the AS predicate for the dummy columns !

Nic;o)
May 14 '08 #11
kcdoell
230 New Member
Nico:

When I run the union query I now get this message:
Expand|Select|Wrap|Line Numbers
  1. This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables. (Error 3071)
  2.  
My individual queries work fine. Have you ever run into this message?
May 14 '08 #12
kcdoell
230 New Member
Nico:

I set up my queries like the following (how I inputted the dummy fileds of which I called the same field name as the other query but did a ["qry field name": Null] in the query field. I needed to change the Null to 0 since it was numeric..

Expand|Select|Wrap|Line Numbers
  1. ctqQueryC_G                                        
  2. CreditRegIDFK    ProductType    GWP_BUD    NWP_BUD    GWP_PRI    NWP_PRI    Week 1    Week 2    Week 3    Week 4    Week 5
  3. Atlanta    Other    0    0    0    0    1022        1474        
  4.  
  5.  
  6. QryProd_Credit_Bud                                        
  7. CreditRegIDFK    ProductType    GWP_BUD    NWP_BUD    GWP_PRI    NWP_PRI    Week 1    Week 2    Week 3    Week 4    Week 5
  8. Atlanta    Casualty    3398    2885    0    0    0    0    0    0    0
  9.  
  10.  
  11. QryProd_Credit_Prior                                        
  12. CreditRegIDFK    ProductType    GWP_BUD    NWP_BUD    GWP_PRI    NWP_PRI    Week 1    Week 2    Week 3    Week 4    Week 5
  13. Atlanta    Casualty    0    0    3316    2828    0    0    0    0    0
That made the error go away and the query ran... Getting closer.....

Keith.
May 14 '08 #13

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

Similar topics

2
7579
by: Bennett Haselton | last post by:
I'm looking for a PHP tutorial that specializes in how to build sites that are based around user logins. i.e. the user logs in on the front page, and are taken to a main login page where fields on...
2
1855
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
5
2238
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted...
4
2642
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
3
1353
by: Casper's Friend | last post by:
It's been a while since I've had to build anything in Access, so forgive me if this is simple...I'm probably just having quite the mental block here. I have a fairly simply query...3 fields which...
4
1766
by: Geoff | last post by:
I need to produce a report based on a query. Cost is a calculated field and its value is dependent on another field, in the query, called Session. There are 5 different Session codes each...
13
3956
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
3
5190
by: skennd | last post by:
Here's my problem in exact replication: I have used the find duplicate query in Access, and the query determined the following duplicate values by the following query: In (SELECT FROM As...
4
2514
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
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...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
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...
0
5475
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,...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
302
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...

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.