<small id='umIC0'></small><noframes id='umIC0'>

  • <tfoot id='umIC0'></tfoot>
      • <bdo id='umIC0'></bdo><ul id='umIC0'></ul>

      1. <legend id='umIC0'><style id='umIC0'><dir id='umIC0'><q id='umIC0'></q></dir></style></legend>

      2. <i id='umIC0'><tr id='umIC0'><dt id='umIC0'><q id='umIC0'><span id='umIC0'><b id='umIC0'><form id='umIC0'><ins id='umIC0'></ins><ul id='umIC0'></ul><sub id='umIC0'></sub></form><legend id='umIC0'></legend><bdo id='umIC0'><pre id='umIC0'><center id='umIC0'></center></pre></bdo></b><th id='umIC0'></th></span></q></dt></tr></i><div id='umIC0'><tfoot id='umIC0'></tfoot><dl id='umIC0'><fieldset id='umIC0'></fieldset></dl></div>
      3. T-SQL 子查询 Max(Date) 和 Joins

        T-SQL Subquery Max(Date) and Joins(T-SQL 子查询 Max(Date) 和 Joins)

        <small id='2E7em'></small><noframes id='2E7em'>

        <legend id='2E7em'><style id='2E7em'><dir id='2E7em'><q id='2E7em'></q></dir></style></legend>
          <tfoot id='2E7em'></tfoot>
            <bdo id='2E7em'></bdo><ul id='2E7em'></ul>
                <tbody id='2E7em'></tbody>

              1. <i id='2E7em'><tr id='2E7em'><dt id='2E7em'><q id='2E7em'><span id='2E7em'><b id='2E7em'><form id='2E7em'><ins id='2E7em'></ins><ul id='2E7em'></ul><sub id='2E7em'></sub></form><legend id='2E7em'></legend><bdo id='2E7em'><pre id='2E7em'><center id='2E7em'></center></pre></bdo></b><th id='2E7em'></th></span></q></dt></tr></i><div id='2E7em'><tfoot id='2E7em'></tfoot><dl id='2E7em'><fieldset id='2E7em'></fieldset></dl></div>
                1. 本文介绍了T-SQL 子查询 Max(Date) 和 Joins的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试连接多个表,但其中一个表有多个记录用于具有不同日期的 partid.我想获取最近日期的记录.

                  I'm trying to join multiple tables, but one of the tables has multiple records for a partid with different dates. I want to get the record with the most recent date.

                  以下是一些示例表:

                  Table: MyParts
                  Partid   Partnumber   Description
                  1        ABC-123      Pipe
                  2        ABC-124      Handle
                  3        ABC-125      Light
                  
                  
                  Table: MyPrices
                  Partid   Price        PriceDate
                  1        $1           1/1/2005
                  1        $2           1/1/2007
                  1        $3           1/1/2009
                  2        $2           1/1/2005
                  2        $4           1/1/2006
                  2        $5           1/1/2008
                  3        $10          1/1/2008
                  3        $12          1/1/2009
                  

                  如果我只是想找到某个零件的最新价格,我可以这样做:

                  If I was just wanted to find the most recent price for a certain part I could do:

                  SELECT * FROM MyPrice WHERE PriceDate = (SELECT MAX(PriceDate) 
                  FROM MyPrice WHERE Partid = 1)
                  

                  但是,我想先进行连接,然后为所有零件取回正确的价格,而不仅仅是一个零件.这是我尝试过的:

                  However I want to do a join first and get back the correct price for all parts not just one. This is what I have tried:

                  SELECT * FROM MyParts LEFT JOIN MyPrice ON MyParts.Partid = MyPrice.Partid WHERE 
                  MyPart.PriceDate = (SELECT MAX(PriceDate) FROM MyPrice)
                  

                  结果是错误的,因为它需要整个表格的最高价格日期.

                  The results are wrong as it takes the highest price date of the entire table.

                  SELECT * FROM MyParts LEFT JOIN MyPrice ON MyParts.Partid = MyPrice.Partid WHERE 
                  MyPart.PriceDate = (SELECT MAX(PriceDate) FROM MyPrice WHERE MyPrice.Partid =   
                  MyParts.Partid)
                  

                  出错了.

                  我该怎么做才能得到我想要的结果.

                  What can I do to get the results I want.

                  推荐答案

                  试试这个:

                  Select *,
                      Price = (Select top 1 Price 
                               From MyPrices 
                               where PartID = mp.PartID 
                               order by PriceDate desc
                              )
                  from MyParts mp
                  

                  这篇关于T-SQL 子查询 Max(Date) 和 Joins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                  <i id='hkKvf'><tr id='hkKvf'><dt id='hkKvf'><q id='hkKvf'><span id='hkKvf'><b id='hkKvf'><form id='hkKvf'><ins id='hkKvf'></ins><ul id='hkKvf'></ul><sub id='hkKvf'></sub></form><legend id='hkKvf'></legend><bdo id='hkKvf'><pre id='hkKvf'><center id='hkKvf'></center></pre></bdo></b><th id='hkKvf'></th></span></q></dt></tr></i><div id='hkKvf'><tfoot id='hkKvf'></tfoot><dl id='hkKvf'><fieldset id='hkKvf'></fieldset></dl></div>
                    <bdo id='hkKvf'></bdo><ul id='hkKvf'></ul>
                  • <tfoot id='hkKvf'></tfoot>

                    <small id='hkKvf'></small><noframes id='hkKvf'>

                      <legend id='hkKvf'><style id='hkKvf'><dir id='hkKvf'><q id='hkKvf'></q></dir></style></legend>
                          <tbody id='hkKvf'></tbody>