简体   繁体   English

从 XML 文件通过 SQL 读取最大值

[英]Reading Max value from the XML file VIA SQL

My data is stored in a table Transactiontable .我的数据存储在表Transactiontable中。 I have columns in the table, one of which is PostIT that stores XML data:我在表中有列,其中之一是存储PostIT数据的 PostIT:

enter image description here ---TABLE IMAGE在此处输入图像描述--- 表格图像

enter image description here ---XML IMAGE在此处输入图像描述---XML IMAGE

From the xml file I have to read the max or the most recent transaction datetime.从 xml 文件中,我必须读取最大或最近的交易日期时间。

I am able to read the first node我能够读取第一个节点

select 
    convert(xml.Transactiontable).value ('Transaction/DateTime[2]','DATETIME') 
from 
    dbo.transactiontable;

I have to read all the transactional datetime and I should be able to select the max or the latest transaction我必须阅读所有交易日期时间,我应该能够 select 最大或最新交易

I tried cross apply and did not work我尝试了交叉申请,但没有奏效

select 
    T.N.value ('Transaction/DateTime[2]','DATETIME') 
from
    dbo.transactiontable
cross apply 
    XXX.nodes('TRANSACTION') as T(N)

There are several things to say first:首先有几点要说:

  • Please do not paste your sample data as picture.请不要将您的示例数据粘贴为图片。 Nobody wants to type this in.没有人愿意输入这个。
  • Best was, to provide a MCVE or a fiddle with DDL, sample data and your own attempt together with the expected output.最好的方法是提供 MCVE 或 DDL、示例数据和您自己的尝试以及预期的 output。
  • Always tag as specifically as possible.始终尽可能具体地标记。 Tag with your tools and provide vendor and version.使用您的工具标记并提供供应商和版本。

Now to your question:现在回答你的问题:

Again there is something to say first:再次有话要先说:

  • Your XML contains date-time values in a culture dependant format.您的 XML 包含文化相关格式的日期时间值。 This is dangerous.这是危险的。 some systems will take "01/05/2019" as the first of May, others will read the 5th of January.一些系统会将“01/05/2019”作为 5 月 1 日,其他系统将读取 1 月 5 日。
  • The XML is hard to read. XML 很难阅读。 If I get the picture correctly, you have alternating <Datetime> and <TRANSACTION_KEY> elements.如果我得到正确的图片,你有交替的<Datetime><TRANSACTION_KEY>元素。 It seems to be their sort order to tigh them together.这似乎是他们的排序顺序,将他们紧紧联系在一起。 This is a bad design...这是一个糟糕的设计...

Try this as first step:试试这个作为第一步:

SELECT dt.value('text()[1]','nvarchar(100)') AS SomeDateTimeValueAsString
FROM YourTable t
CROSS APPLY t.TheXmlColumn.nodes('/DATA/CO/TRANSACTION/DateTime') A(dt);

If this is not enough to get you running, you'll have to invest more effort to your question.如果这还不足以让你跑起来,你将不得不在你的问题上投入更多的精力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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