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.
| Author |
Topic |
|
sqldistinct
Starting Member
1 Post |
Posted - 2012-06-24 : 12:24:52
|
| Hi,Hope someone can help?I need to get an average number of hours it took install a distinct product_number and to subtotal the hours by serial number, to do this I need to sum up all of the number_hours fields for a distinct serial number for that distinct product_number, then take the sum of all those hours and divide by the number of distinct serial numbers for that distinct product id.In the example I pasted below, the distinct product_number is 00015546279 of which there are three distinct serial numbers (67125121, 67125128, and 67225130), the sum of all of the hours for this example's product_number is 60, I would then take that number and divide it by the number of distinct serial numbers found for that distinct product_number, 00015546279, of which there are three and arrive at my average of 20 hours for the install time for that product_number. My problem is how to build an sql statement to do this and get a results table that contains this information in a results list with subtotals by serial number and average hours by product_number.Thanks,Peteproduct_number serial_number iteml_number number_hours00015546279 67125121 110334 800015546279 67125121 110334 200015546279 67125128 106700 200015546279 67125128 106700 600015546279 67125128 106700 600015546279 67225130 83489 600015546279 67225130 83489 800015546279 67225130 83489 800015546279 67225130 83489 800015546279 67225130 83489 6 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-24 : 13:59:50
|
| [code]SELECT t.product_number,TotalHrsSerial,TotalHrs * 1.0/SerialCnt AS AvgHrsPrdtFROM(SELECT DISTINCT product_number,SUM(number_hours) OVER (PARTITION BY product_number, serial_number) AS TotalHrsSerialFROM table)tINNER JOIN (SELECT product_number,SUM(number_hours) AS TotalHrs,COUNT(DISTINCT serial_number) AS SerialCntFROM tableGROUP BY product_number)t1ON t1.product_number = t.product_number[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|