Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with a SELECT query consisting one table!

Author  Topic 

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2010-06-07 : 19:40:12
Hi All,

I have table call Aggregate with following fileds. I am putting all records for some of the AggregateID's as an example. I have ordered the records by AggregateID.

AggregateID, Description, ProductID, ParentID, SumofSales
A0000003065, SMALL CAMEO-PRODUCE, 000000083066, 2641.92
A0000004012, NAVEL ORANGE FANCY JUMBO-PRODUCE, 000000083107, 61122.17
A0000004012, NAVEL ORANGE FANCY JUMBO-PRODUCE, 000000003372, 442.53
A0022700093, CG LINE EXCT-.02 OZ, 002270009338, 138.00
A0022700093, CG LINE EXCT-.02 OZ, 002270009340, 114.00
A0022700093, CG LINE EXCT-.02 OZ, 002270009339, 99.00
A0027000909, CG VLMEXCT MAS-.24 OZ, 002270009097, 298.00
A0027000909, CG VLMEXCT MAS-.24 OZ, 002270009096, 298.00

As you can see in my above example there are 6 distinct Aggregate ID's.

I want to have a SELECT query with a new column called "ParentID". ParentID would be same as ProductID but will be based on highest SumofSales for each set of Aggregate ID. So for the above records my SELECT should return the following records.

AggregateID, Description, ProductID, ParentID, SumofSales, ParentID
A0000003065, SMALL CAMEO-PRODUCE, 000000083066, 2641.92, 000000083066
A0000004012, NAVEL ORANGE FANCY JUMBO-PRODUCE, 000000083107, 61122.17, 000000083107
A0000004012, NAVEL ORANGE FANCY JUMBO-PRODUCE, 000000003372, 442.53, 000000083107
A0022700093, CG LINE EXCT-.02 OZ, 002270009340, 114.00, 002270009338
A0022700093, CG LINE EXCT-.02 OZ, 002270009338, 138.00, 002270009338
A0022700093, CG LINE EXCT-.02 OZ, 002270009339, 99.00, 002270009338
A0027000909, CG VLMEXCT MAS-.24 OZ, 002270009097, 298.00, 002270009097
A0027000909, CG VLMEXCT MAS-.24 OZ, 002270009096, 298.00, 002270009097

As you see above in my output each set of Aggregate should always have one same ParentID which is determined by highest sales.

So the SELECT query should check and do the following.

a) If there is only one record for an AggregateID, choose the ProductID as a ParentID for that AggregateID record (that is the case in case of A0000003065 AggregateID).
b) If there are multiple records for an AggregateID, choose the ProductID as a ParentID for respective AggregateID records with the highest SumofSales (ParentID 000000083107 for AggregateID A0000004012. ParentID 002270009338 for AggregateID A0022700093. )
c) If all the ProductID's of a same AggregateID has same SumofSales, randomly choose on of the ProductID within that AggregateID as a ParentID (002270009097 ParentID for AggregateID A0027000909).

How can I do that in a select query please suggest promptly. Thanks a lot for all your valueable help and time.

Thanks,

Zee

zeeshan13
Constraint Violating Yak Guru

347 Posts

Posted - 2010-06-07 : 20:03:36
Can someone help please? Thanks a load for your time.
Go to Top of Page
   

- Advertisement -