"Basically what the title says"?
Uploaded a picture of the three tables... attached a script creating three tables, with the data needed (sql2008)...
PS1: table2 in the picture, has a column named "subgroupID" (the car-table), which is wrong, it should have been "groupID", my bad.
PS2: do not question the "ID" column in the "group-table", nor anything else, this is purely an example...
What I want is to update the table "Group", set the "active" column to 1, if, and only if number of cars below that group has a car that is available for sale.
How can a car be available for sale? If it is registered with a price in the "Cost-table (table3)", then the group and all subgroups this car is within shall be activated.
With those 3 tables in the picture, I want this result for the GroupTable:
Code:
ID groupID subgroupID active title
1 1 0 1 Cars
2 2 1 1 Red Cars
3 3 2 0 Red Jaguar
4 4 2 1 Red Porsche
5 5 2 1 Red Ferrari
6 6 0 0 Bikes
The main group "Cars" should turn into active, because a car below this group is for sale.
I want Red cars to be active, because some red car is for sale.
I want Red Jaguar to be NOT active (active = 0), because no jaguar has a registered cost (price/prize) in the Cost-table.
The cars Porsche and Ferrari has a cost/prize (a seller), hence update this group and set it to be active.
I want "Bikes" active to be 0, because no bikes exists.
Any suggestions?
So far I've got it "working",... This is close to what I want, but then again not really.
Code:
DECLARE @tbl1 TABLE
(groupID int)
DECLARE @tbl2 TABLE
(groupID int)
DECLARE @tbl3 TABLE
(groupID int)
/* LEVEL 0*/
INSERT INTO @tbl1(groupID)
(
SELECT DISTINCT t1.groupID
FROM groups as t1
LEFT JOIN groups as t2 ON t1.groupID = t2.subgroupID
LEFT JOIN groups as t3 ON t2.groupID = t3.subgroupID
LEFT JOIN car as t4 ON t3.groupID = t4.groupID
LEFT JOIN carcost as t5 ON t4.ID = t5.carID
where t1.active = 0 AND t2.active = 0 and t3.active = 0
GROUP BY t1.groupID, t1.title, t5.carID, t5.cost, t4.carname
HAVING COUNT(t5.ID) > 0
)
UPDATE groups
set active = 1
where groups.groupID IN (Select t1.groupID from @tbl1 as t1);
/* LEVEL 1*/
INSERT INTO @tbl2(groupID)
(
SELECT DISTINCT t1.groupID
FROM groups as t1
LEFT JOIN groups as t2 ON t1.groupID = t2.subgroupID
LEFT JOIN car as t3 ON t2.groupID = t3.groupID
LEFT JOIN carcost as t4 ON t3.ID = t4.carID
where t1.active = 0 AND t2.active = 0 and t1.subgroupID IN (SELECT groupID from @tbl1)
GROUP BY t1.groupID, t1.title, t4.carID, t4.cost, t3.carname
HAVING COUNT(t4.ID) > 0
)
UPDATE groups
set active = 1
where groups.groupID IN (Select t1.groupID from @tbl2 as t1);
/* LEVEL 2*/
INSERT INTO @tbl3(groupID)
(
SELECT DISTINCT t1.groupID
FROM groups as t1
LEFT JOIN car as t2 ON t1.groupID = t2.groupID
LEFT JOIN carcost as t3 ON t2.ID = t3.carID
where t1.active = 0 AND t1.subgroupID IN (SELECT groupID from @tbl2)
GROUP BY t1.groupID, t1.title, t3.carID, t3.cost, t2.carname
HAVING COUNT(t3.ID) > 0
)
UPDATE groups
set active = 1
where groups.groupID IN (Select t1.groupID from @tbl3 as t1);
SELECT * from groups;
As you have probably figured, this works only for a group where there is an exact amount of subgroups, in this case 2 subgroups, (level 0 > level 1 > level 2). If there is a car within the 2. subgroup with a price, then those groups are activated.
I want it to work without the limitation of 2 subgroups, lets say; 4 subgroups...
(of course: I know the total amount of subgroups and the minimum (1-5), hence I can copy paste this code, making it work for "all groups"... But, oh, cmon!

)
A bit off-topic:
Is there another function I can use instead of "Having count(ID) > 0"? My point:
I want to stop the "Count(ID)" if it reaches greater than 0, as in: if it is 1, do not need to count further rows...
The "car-cost"-Table has about 700.000-800.000 rows... So making the count(ID) count up to 30.000 is pointless.
Was I clear enough?
Edit: Hmpf... Maybe I should just do this programmatically, as in through C++/C#, through some recursion, and whatnot! But, oh... cmon!
