簡體   English   中英

過濾Autodesk Vault垂直數據,獲取每個工程圖的最新記錄

[英]Filtering Autodesk Vault vertical data, getting the newest record for each drawing

使用Microsoft SQL Server Express Edition(64位)10.0.550.0

我正在嘗試從Autodesk Vault服務器提取數據。 就我目前的知識水平而言,要獲取所需數據所涉及的SQL太高級了,因此我試圖使用來自Google和StackOverflow的代碼作為碎片來打個謎。 使用這個出色的答案,我能夠將垂直數據轉換為可管理的水平格式。

Autodesk Vault數據庫存儲有關CAD工程圖的信息(以及其他信息)。 主垂直表dbo.Property包含有關每個CAD工程圖的所有不同修訂的信息。 我當前面臨的問題是我獲取了太多數據。 我只想要每個CAD工程圖的最新版本的數據。

到目前為止,這是我的SQL

select 
    CreateDate,
    EntityID,
    PartNumber,
    CategoryName,
    [Subject],
    Title
from 
(
    select 
        EntityID,
        CreateDate,
        [53] as PartNumber,
        [28] as CategoryName,
        [42] as [Subject],
        [43] as Title
    from 
    (
        select
            p.Value, 
            p.PropertyDefID,
            p.EntityID,
            e.CreateDate
        from dbo.Property as p
        inner join dbo.Entity as e on p.EntityID = e.EntityId
        where p.PropertyDefID in(28, 42, 43, 53)
        and e.EntityClassID = 8
    ) t1
    pivot 
    (
        max(Value)
        for PropertyDefID in([28], [42], [43], [53])
    ) t2
) t3
where PartNumber is not null
and PartNumber != ''
and CategoryName = 'Drawing'
-- (1) additional condition
order by PartNumber, CreateDate desc

其中dbo.Property.Valuesql_variant數據類型。 上面的查詢產生了類似於以下內容的數據集:

CreateDate | EntityID | PartNumber | CategoryName | Subject | Title 
---------------------------------------------------------------------
2016-01-01 |    59046 |      10001 | Drawing      | Xxxxx   | Yyyyy
2016-05-01 |    60137 |      10001 | Drawing      | Xxxxx   | Yyyyy
2016-08-01 |    62518 |      10001 | Drawing      | Xxxx    | Yyyyyy
2016-12-16 |    63007 |      10001 | Drawing      | Xxxxxx  | Yyyyyy
2016-01-01 |    45776 |      10002 | Drawing      | Zzzzz   | NULL  
2016-11-01 |    65011 |      10002 | Drawing      | Zzzzzz  | NULL  
...
(about 23000 rows)

我的問題是我要獲取每個圖形的所有修訂版。 在上面的示例中,我只想要PartNumber=10001的最新版本(日期為“ 2016-12-16”等)。

我還查看了有關如何分組和選擇其中一列具有最大值的行的答案 ,但是我似乎無法弄清楚如何將兩者結合起來。 我嘗試將以下代碼段添加到上述查詢的注釋行中,但在許多不同級別上均失敗。

and (PartNumber, CreateDate) in 
(
    select PartNumber, max(CreateDate)
    from t3
    group by PartNumber 
)

盡管已經完成了透視,但仍將這個問題標記為“透視”的原因是,我懷疑透視是導致我麻煩的原因。 我只是還無法解決這些關鍵問題,而且我的SQL優化技能嚴重不足。 也許應該在內部進行過濾?

從@Strawberry提供的評論中汲取了靈感,我一直在努力並進行調整,直到發現一些可行的方法為止。 我必須在PIVOT內使用PIVOT才能正常工作。

編輯:起初我使用視圖,但是前提條件發生了變化,因為我必須與只讀數據庫用戶一起工作。 幸運的是,我仍然被允許創建臨時表。

這是最終結果。

if object_id('tempdb.dbo.#Properties', 'U') is not null
    drop table #Properties

create table #Properties 
(
    PartNumber  nvarchar(max),
    [Subject]   nvarchar(max),
    Title       nvarchar(max),
    CreateDate  datetime
)

insert into #Properties
(
    PartNumber,
    [Subject],
    Title,
    CreateDate
)
select 
    convert(nvarchar(max), PartNumber),
    convert(nvarchar(max), [Subject]), 
    convert(nvarchar(max), Title),
    convert(datetime, CreateDate)
from 
(
    select 
        EntityID,
        CreateDate,
        [53] as PartNumber,
        [42] as [Subject],
        [43] as Title
    from 
    (
        select
            p.Value, 
            p.PropertyDefID,
            p.EntityID,
            e.CreateDate
        from dbo.Property as p
        inner join dbo.Entity as e on p.EntityID = e.EntityId
        where p.PropertyDefID in (42, 43, 53)
        and e.EntityClassID = 8
        and p.EntityID in
        (
            select 
                max(EntityID) as MaxEntityID
            from 
            (
                select 
                    EntityID,
                    [28] as CategoryName,
                    [53] as PartNumber
                from
                (
                    select
                        p.Value,
                        p.EntityID,
                        p.PropertyDefID
                    from dbo.Property as p
                    inner join dbo.Entity as e on p.EntityID = e.EntityId
                    where p.PropertyDefID in (28, 53)
                    and e.EntityClassID = 8 -- FileIteration
                ) as t1
                pivot
                (
                    max(Value)
                    for PropertyDefID in ([28], [53])
                ) as t2
            ) as t3
            where CategoryName = 'Drawing'
            group by PartNumber
        )
    ) as t4
    pivot 
    (
        max(Value)
        for PropertyDefID in ([42], [43], [53])
    ) as t5
) as t6
where PartNumber is not null
and PartNumber != ''
order by PartNumber

select * from #Properties;
-- search conditions goes here

我不得不將建議的join更改為where x in(y)因為聯接非常慢(我在四分鍾后終止了查詢)。 現在生成的數據集(大約需要2秒鍾的時間)看起來很有希望:

PartNumber | Subject | Title  | CreateDate       | ...
-----------------------------------------------------------------------
100000     | Xxxxxx  | Yyyyyy | 2015-08-17 09-10 | ...
100001     | Zzzzzz  | NULL   | 2015-09-02 15-23 | ...
...
(about 8900 rows)

集合中沒有更多的舊版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM