question

akhterhussain-3167 avatar image
0 Votes"
akhterhussain-3167 asked TiborKaraszi answered

Not to sum last inserted Record.

i want to not sum last inserted in table against I_ID column.

I_ID=222 ,have three value 500 ,600 and 400. i have inserted last value 400 against I_ID ,which should not be sum and result will be 1100.


 Create TABLE #Tbl_IB_Cus_Libility (Li_ID int,I_ID int,I_Amt int)
 Insert into #Tbl_IB_Cus_Libility values (1,222,500)
 Insert into #Tbl_IB_Cus_Libility values (2,222,600)
 Insert into #Tbl_IB_Cus_Libility values (3,222,400)
 Insert into #Tbl_IB_Cus_Libility values (4,225,200)


sql-server-generalsql-server-transact-sql
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TiborKaraszi avatar image
0 Votes"
TiborKaraszi answered

Here's one option:


  SELECT li.I_ID, SUM(I_Amt)
  FROM #Tbl_IB_Cus_Libility AS li
  WHERE li.Li_ID <> (SELECT MAX(i.Li_id) FROM #Tbl_IB_Cus_Libility AS i WHERE i.I_ID = li.I_ID)
  GROUP BY I_ID
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.