473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tricky query...

44
Hi folks;

Hope the example data below displays properly for you?
Expand|Select|Wrap|Line Numbers
  1. CustName      FireID    CustID  PolicyNo  PolicyVal  BuildingVal
  2. Claire Sheene   8       88221    125584    12,500       12,500
  3. Claire Sheene   7       88220
  4. Robin Murphy    9       78552    365557    35,000       35,000
  5. Robin Murphy    6       74859    995554    25,000
This is the datasheet view of some tricky data I'm trying to manhandle. I'm going to be receiving 25000 records to analyse in the next week, and as usual they're going to be a mess! They're all details of fire insurance policies held by bank customers who have loans taken out against their businesses. Some customers have multiple customer numbers, to which are connected multiple insurance policies, each with their own IDs. My job is to find the total value of insurannce policies held by each individual and compare it to the total value of their assets insured, to make sure that if these places burn down then there's enough insurance in place to cover any losses. So I need to find a way to (in the case of the sample data above) add all the policy values and building values together for each customer. How does one do this with a query? To add PolicyVal in one record to PolicyVal in another but only if the CustName is the same? I've had a good look around the Web but without much luck. Any ideas folks?
Mar 26 '09 #1
7 1271
FishVal
2,653 Expert 2GB
Well. Actually it doesn't seem that tricky.

Since [CustName] field value uniquely identifies customer you may use query grouping records on values of this field.

Expand|Select|Wrap|Line Numbers
  1. SELECT CustName, Sum(PolicyVal) AS SumOfPolicyVal, Sum(BuildingVal) AS SumOfBuildingVal FROM [TableName] GROUP BY CustName;
  2.  
using HAVING clause you could filter those customers whos insurance doesn't cover building value

Expand|Select|Wrap|Line Numbers
  1. SELECT CustName, Sum(PolicyVal) AS SumOfPolicyVal, Sum(BuildingVal) AS SumOfBuildingVal FROM [TableName] GROUP BY CustName HAVING Sum(PolicyVal)<Sum(BuildingVal);
  2.  
Mar 26 '09 #2
NeoPa
32,556 Expert Mod 16PB
It does now ;)

Tip for the future :
Use spaces rather than tabs, and use the [ CODE ] tags, and it will layout as intended.
Mar 26 '09 #3
Whizzo
44
Many thanks, I'll see if I can get that working. In truth I've only just started creating queries in Design View within Access so I'm still finding the translation from raw code to the design grid a little opaque! I'm sure I'll be able to pull something out of the bag... Thanks!
Mar 26 '09 #4
NeoPa
32,556 Expert Mod 16PB
Some typical and basic code would be :
Expand|Select|Wrap|Line Numbers
  1. SELECT Sum(X) AS SumX,
  2.        Count(X) AS CountX,
  3.        ...
  4.  
  5. FROM ...
  6.  
  7. GROUP BY [CustName]
I would however, urge caution whenever grouping by an unformatted text fields (such as names). There are so many potential problems with this that enumerating them is beyond my time limits.

It is probable that this will not succeed completely and that some manual intervention will be required.
Mar 26 '09 #5
NeoPa
32,556 Expert Mod 16PB
@Whizzo
You know you can enter SQL into an Access query directly, and take it out too. Use View / SQL View & View / Design View to switch between alternative views of the same query.
Mar 26 '09 #6
Whizzo
44
I did try that, but the SQL in the text box has to be formatted just so. It also won't convert "experimental" code back unless it's been written specifically for your tables; Access spits it out again if there's references to objects that aren't in the DB.

I do have just one more question on the above, though...

As you can see in the sample data in the original quesiton, each individual can have several customer numbers. As part of the same query that adds up the liabilities is it possible for it to output the individual customer numbers too? So each individual will have a customer name in once text field, total value of all policies in another, and in a third all the policy numbers displayed as "1234, 2345, 3456"? This is so an advisor can then look up each individual policy from the main data and find which one isn't right.

Thanks!
Mar 26 '09 #7
NeoPa
32,556 Expert Mod 16PB
@Whizzo
Yes. It validates the SQL before trying to parse it. That's actually quite sensible if you think about it.
@Whizzo
I don't think there's an article on this as such, but this thread should give you what you need.
Combining Rows-Opposite of Union
Mar 26 '09 #8

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

Similar topics

15
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode()...
5
by: scott | last post by:
Hello, I'm trying to find the most optimal way to perform a tricky query. I'm hoping this is some sort of standard problem that has been solved before, but I'm not finding anything too useful so...
3
by: Novice | last post by:
Hey all, I've attempted to simplify my question (or at least the XML that is part of my example in my question): I have the following XML: <A> <B><C><D> I want to query the data in this node...
1
by: Pea | last post by:
I'm working with a system usage database. I want to filter out repetitive logins. The query I have retrieves data like this: USER_DATE USER_TIME1 USER_USERID USER_ACCOUNT...
6
by: pointBoarder | last post by:
Thanks in advance to all who read this. I've got 3 tables which were created from a txt file dumped from some old system. Header ID -- autonumber, primary OrderNum -- field I want Line
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
15
by: edouard.spooner | last post by:
Hi, I have a tricky SQL query problem that I'm having probs with. I have a table which resembles something like this Date | Price1 | Price2 | Price3 01 Jan 2006 | 100 | 100 | 100 02 Jan...
0
by: hdogg | last post by:
I am using oracle with php. I am trying to accomplish a left outer join. Here are the 3 queries. Query 1, contains all the data on the left the will show up with data on the right. Query 1 =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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...
0
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,...

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.