473,404 Members | 2,213 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,404 software developers and data experts.

Single Max Child Row (Based on a Count) for All Parent Rows

Let's say I have a table with a list of young folk (table1). This table is related to a table that lists all their booty calls (table2). A third table lists their booty calls' booty calls (table3). For the purposes of this example the tables are set up as a one to many relationship.

The young folks in table1 want to lower their risk of contracting an STD by kicking their single most sexually active booty call to the curb. So the challenge is to create a query that displays all the young folk in table1 along with who they should kick to the curb. Who they should kick to the curb is based on how many booty calls their booty calls have.

This is where I'm at...

select t1.ID, t2.ID,
(Select Count(t3.ID) from table3 t3 Where t3.Parent_ID = t2.ID) as BootyCalls
From table1 t1
join table2 t2 on t2.Parent_ID = t1.ID

That query displays all the rows from table1, all the rows from table2 and a BootyCall column which displays the count of table2's booty calls. The problem is that I want to display all the young folk in table1 and their single most sexually active booty call from table2.

Any ideas how to accomplish this in a single query? Thanks in advance for any help!
Jul 11 '07 #1
3 2212
Infide
28
Let's say I have a table with a list of young folk (table1). This table is related to a table that lists all their booty calls (table2). A third table lists their booty calls' booty calls (table3). For the purposes of this example the tables are set up as a one to many relationship.

The young folks in table1 want to lower their risk of contracting an STD by kicking their single most sexually active booty call to the curb. So the challenge is to create a query that displays all the young folk in table1 along with who they should kick to the curb. Who they should kick to the curb is based on how many booty calls their booty calls have.

This is where I'm at...

select t1.ID, t2.ID,
(Select Count(t3.ID) from table3 t3 Where t3.Parent_ID = t2.ID) as BootyCalls
From table1 t1
join table2 t2 on t2.Parent_ID = t1.ID

That query displays all the rows from table1, all the rows from table2 and a BootyCall column which displays the count of table2's booty calls. The problem is that I want to display all the young folk in table1 and their single most sexually active booty call from table2.

Any ideas how to accomplish this in a single query? Thanks in advance for any help!
try

Expand|Select|Wrap|Line Numbers
  1. SELECT T1.*, T2.*, bootydata.*
  2. FROM TABLE1
  3. inner join table t2
  4.     on t2.id = t1.id
  5. inner join
  6.     (SELECT Max(bootycalls), t3.id,t2.id
  7.         FROM 
  8.         (SELECT count(*) as bootycalls, t3.id, t2.id
  9.                  from table3 t3 inner join 
  10.                          table t2 on t2.id = t3.id
  11.                  GROUP BY t3.id, t2.id) as BootyData
  12. on t2.id = bootydata.id
  13.  
  14.  
The trick is the sub select which returns the max count of another sub select that performs the required aggregating.
The grouping of the derived table should provide a 1 to 1 match up of the people in table 2 with their maximum count of children in table 3.
Jul 11 '07 #2
try

Expand|Select|Wrap|Line Numbers
  1. SELECT T1.*, T2.*, bootydata.*
  2. FROM TABLE1
  3. inner join table t2
  4.     on t2.id = t1.id
  5. inner join
  6.     (SELECT Max(bootycalls), t3.id,t2.id
  7.         FROM 
  8.         (SELECT count(*) as bootycalls, t3.id, t2.id
  9.                  from table3 t3 inner join 
  10.                          table t2 on t2.id = t3.id
  11.                  GROUP BY t3.id, t2.id) as BootyData
  12. on t2.id = bootydata.id
  13.  
  14.  
Thanks! But it's missing a parentheses in this line "GROUP BY t3.id, t2.id) as BootyData" and trying to close it results in various errors such as multi-part identifiers unable to be bound and invalid columns.
Jul 11 '07 #3
Infide
28
Expand|Select|Wrap|Line Numbers
  1. SELECT T1.*, T2.*, bootydata.*
  2. FROM TABLE1
  3. inner join table t2
  4.     on t2.id = t1.id
  5. inner join
  6.     (SELECT Max(bootycalls), t3.id,t2.id
  7.         FROM 
  8.         (SELECT count(*) as bootycalls, t3.id, t2.id
  9.                  from table3 t3 inner join 
  10.                          table t2 on t2.id = t3.id
  11.                  GROUP BY t3.id, t2.id) as BootyData
  12. on t2.id = bootydata.id
  13.  
Thanks! But it's missing a parentheses in this line "GROUP BY t3.id, t2.id) as BootyData" and trying to close it results in various errors such as multi-part identifiers unable to be bound and invalid columns.
Looks like I didn't alias my columns in my derived table and I missed a group by. My bad.

The trick is creating a derived table for your third table that summarizes what you want. You need to know table 2's most frequent booty calls from table 3?

This select:
(SELECT Max(bootycalls), t3id,t2id
FROM
(SELECT count(*) as bootycalls, t3.id as t3id, t2.id as t2id
from table3 t3 inner join
table t2 on t2.id = t3.id
GROUP BY t3.id, t2.id) as BootyData
GROUP BY t3id,t2id
) as Data

This gives you the summary of highest t3 child counts to the t2 parent. Join this derived table back onto the t2 table and then t1 table.
Jul 11 '07 #4

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

Similar topics

2
by: Caroline | last post by:
I seem to always want to do this type of join, but I can never find how to do this. I am trying to join two tables. The parent table will always have one row of data and the child may have 0 to...
4
by: I am Sam | last post by:
I have two tables pluged into a dataRelation Collection and I need to get the count of rows in the child table. Anyone have ideas on how I can accomplish this?
1
by: Glenn T. Kitchen | last post by:
Dear Group, I'm having problems retrieving the child rows of a parent row. The parent table is Users and the child table is Addresses. I used the Schema to create a DataRelation between the...
0
by: Ambica Jain | last post by:
I have a data grid called Files, which has some columns like FileName, Col1, Col2, ... , Col8. Then i have a combobox which allows user to select from Col1 to Col8 and based on this selection, i...
0
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row...
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
8
by: Rick | last post by:
VS 2005 I' m setting up a parent/child datagridviews in a form. I am doing a lot of this by hand coding in order to get the feel of things. I want a change in the parent table to trigger a...
1
nev
by: nev | last post by:
Good day Sirs & Madams, I know some of you will find this problem very simple. Kindly share a little knowledge to me on how to deal with this. Thank you. I have a parent-child table relation. when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.