# Average Days Held by Material Type

**URL:** https://forum.innovativeusers.org/t/average-days-held-by-material-type/1190
**Category:** Polaris Repository
**Tags:** cp-migration, polaris-holds
**Created:** [June 2, 2023, 8:59pm UTC](https://forum.innovativeusers.org/t/average-days-held-by-material-type/1190 "2023-06-02T20:59:52Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://forum.innovativeusers.org/uploads/db7787/original/2X/8/8519ee8eef6492f310ea87d4ce482d931f01eef2.png) [@system](https://forum.innovativeusers.org/u/system)
#### Post date: [June 2, 2023, 8:59pm UTC](https://forum.innovativeusers.org/t/average-days-held-by-material-type/1190/1 "2023-06-02T20:59:52Z")

</div>

# Average Days Held by Material Type

This was a contribution from another source but I’m not sure who. If this is your code, let me know! It delivers the average number of days items are held per material type.

```sql
declare @startDate date = '2019-07-01 00:00:00.000'
declare @endDate date = '2021-08-31 23:59:59.999'

select
    d.MaterialType
    ,avg(d.LoanDays) [AvgLoanDays]
    ,min(d.LoanDays) [MinLoanDays]
    ,max(d.LoanDays) [MaxLoandays]
    ,count(*) [NumItemsNotEntirelyAccurate]
from 
    (select
        mt.Description [MaterialType]
        ,td_ll.numValue / 60.0 / 24.0 [LoanDays]
     from
        PolarisTransactions.Polaris.TransactionHeaders th
     join
        PolarisTransactions.Polaris.TransactionDetails td_ll on (td_ll.TransactionID = th.TransactionID and td_ll.TransactionSubTypeID = 323)
     join
        PolarisTransactions.Polaris.TransactionDetails td_item on (td_item.TransactionID = th.TransactionID and td_item.TransactionSubTypeID = 38)
     join
        Polaris.polaris.CircItemRecords cir on cir.ItemRecordID = td_item.numValue
     join
        Polaris.Polaris.MaterialTypes mt on mt.MaterialTypeID = cir.MaterialTypeID
     where
        th.TransactionTypeID = 6002
     and
        isnull(td_ll.numValue,0) between 1000 and 1000000 -- adjust these numbers to eliminate outliers but also further skew number of items downwards
     and
        th.TranClientDate between @startDate and @endDate
) d
group by
    d.MaterialType

```
