简体   繁体   中英

Extracting certain values from XML column

i have the following sql query:

 select         
I.N.value('@DRN', 'varchar(50)') as DRN,
I.N.value('@CR_GUID', 'varchar(50)') AS CR,
I.N.value('@DR_GUID', 'varchar(50)') AS DR,
I.N.value('@AMOUNT', 'money') as Amount,
I.N.value('@DATE', 'Date') as ProDate
from Table as T
cross apply T.column.nodes('/ITEMS/ITEM') as I(N)

Currently it will bring back everything, example:

DRN    CR         DR        Amount        Prodate
12     2p1rf      684pb     4686.23       2012-11-14
12     586io      fdc25     4686.23       2012-11-16
13     hnv7p      19i6f     1800.00       2012-11-20
14     cd7k0      9s2vc      570.50       2012-11-20
15     pm78s      qw3d5     8500.00       2012-11-23

how do i exclude the following from the results, this is from the same row in the xml column?

 DRN    CR         DR        Amount        Prodate
*12*     2p1rf      684pb     4686.23       2012-11-14
*12*     586io      fdc25     4686.23       2012-11-16

i only want the query to bring back rows with one item in the xml column not mutliple values.

thank you

how do i then go about to bring back only the first item in a row with multiple items:

 DRN    CR         DR        Amount        Prodate
*12*     2p1rf      684pb     4686.23       2012-11-14
*12*     586io      fdc25     4686.23       2012-11-16

so i want to only see the item with the date 2012-11-14 not everything pertaining to the row? i hope that makes sense?

You should be able to do an inline aggregate and filter on that

SELECT
    X.DRN, X.CR, X.DR, X.Amount, X.ProDate
FROM
    (
    SELECT
        COUNT(*) OVER (PARTITION BY I.N.value('@DRN', 'varchar(50)')) AS CountPerDRN,
        I.N.value('@DRN', 'varchar(50)') as DRN,
        I.N.value('@CR_GUID', 'varchar(50)') AS CR,
        I.N.value('@DR_GUID', 'varchar(50)') AS DR,
        I.N.value('@AMOUNT', 'money') as Amount,
        I.N.value('@DATE', 'Date') as ProDate
    from
        MyTable as T
        cross apply
        T.Mycolumn.nodes('/ITEMS/ITEM') as I(N)
    ) X
WHERE
   X.CountPerDRN= 1

Edit, after question addition

Simply change the inline aggregate to a window function
In this case, I've chosen the row based on the lowest ProDate

SELECT
    X.DRN, X.CR, X.DR, X.Amount, X.ProDate
FROM
    (
    SELECT
        ROW_NUMBER() OVER (
                  PARTITION BY I.N.value('@DRN', 'varchar(50)')
                  ORDER BY I.N.value('@DATE', 'Date')
                  ) AS rnPerDRN,
        I.N.value('@DRN', 'varchar(50)') as DRN,
        I.N.value('@CR_GUID', 'varchar(50)') AS CR,
        I.N.value('@DR_GUID', 'varchar(50)') AS DR,
        I.N.value('@AMOUNT', 'money') as Amount,
        I.N.value('@DATE', 'Date') as ProDate
    from
        MyTable as T
        cross apply
        T.Mycolumn.nodes('/ITEMS/ITEM') as I(N)
    ) X
WHERE
   X.rnPerDRN = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM