如何获取WRT提交的最新记录的“组件”

时间:2019-04-14 08:05:30

标签: sql-server

基于组件需要获取最新记录,其中每个组件可能具有“ n”张票证,但是为了进行重新调查,将克隆其中的几张。

  SELECT DISTINCT T.* FROM my_table AS T

但是我还没有一个完美的答案

表格将会

ID  Summary                         Component   Status
1   Tyre repairing                  Car         Tested
2   Glass fitting                   Bus         Tested
3   Printer repair                  Printer     Confirm
4   CLONE - Tyre Repair             Car         Tested
5   Dashboard Clean-up              Car         Confirm
6   CLONE - CLONE - Tyre Repair     Car         Tested
7   CLONE - Glass fitting           Bus         Tested
8   Cleaning                        Computer    Tested

寻找输出

ID  Summary                         Component   Status
3   Printer repair                  Printer     Confirm
5   Dashboard Clean-up              Car         Confirm
6   CLONE - CLONE - Tyre Repair     Car         Tested
7   CLONE - Glass fitting           Bus         Tested
8   Cleaning                        Computer    Tested

有人可以帮我以上吗? 谢谢, 贾娜

2 个答案:

答案 0 :(得分:0)

假设public class Repository { private long rowId = -1; private CountDownLatch mLatch; public long insert(Player player) { mLatch = new CountDownLatch(1); new insertAsyncTask(mPlayerDao).execute(player); try { mLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } Log.i("LOG_TAG", String.valueOf(rowId)); return rowId; } class insertAsyncTask extends AsyncTask<Player, Void, Void> { @Override protected Void doInBackground(Player... players) { rowId = mDatabase.getContactDAO().addContact(players[0]); mLatch.countDown(); return null; } } } 列是一个标识列,因此可以用来确定哪个记录是最新的,则可以使用top 1 with ties并按row_number进行排序:

ID

这将为每个组件返回单个记录,其中该特定组件的ID最高。

答案 1 :(得分:0)

您是否需要每个组件一次出现?(包括克隆吗?) 我正确理解ID可能会有所帮助:

select RIGHT(Summary,len(summary)-len('CLONE -')) Summary into #t from my_table  where Summary like '%CLONE%'

select * from my_table  where Summary not in (select rtrim(ltrim(Summary)) from #t)
drop table #t

您也可以使用此面纱:

create view vw_InvalidSummary
as
    select RIGHT(Summary,len(summary)-len('CLONE -')) Summary from  my_table  where Summary like '%CLONE%'