2 3 8 4 7 3 7 8 6 9 4 9 9 7 11 我需要一个查询来实现此输出解决方案 您好Krithiga, 查看以下示例TSQL脚本。 - 表创建语句 IF OBJECT_ID (' TempDB ..#TestSample') IS NOT NULL DROP 表 #TestSample CREATE TABLE #TestSample(Sno INT ,打开 INT ,Plusvalue INT ,MinusValue INT ,关闭 INT ) - 插入样本值 INSERT INTO #TestSample(Sno,Opening,Plusvalue,MinusValue,Closing) VALUES ( 1 , 0 , 5 , 2 , 0 ),( 2 , 0 , 8 , 4 , 0 ),( 3 , 0 , 8 , 6 , 0 ),( 4 , 0 , 9 , 7 , 0 ) - 给定数据 SELECT Sno,Openi ng,Plusvalue,MinusValue,结束 FROM #TestSample - 必需输出 SELECT T.Sno,(ISNULL(( SELECT SUM((Opening + Plusvalue-MinusValue)) FROM #TestSample WHERE Sno< T.Sno), 0 ))' 打开', T.Plusvalue,T.MinusValue,(ISNULL(( SELECT SUM((Opening + Plusvalue-MinusValue)) FROM #TestSample WHERE Sno< T.Sno), 0 )+ T.Plusvalue - T.MinusValue)' 关闭' FROM #TestSample T 问候, GVPrabu 嗨Prabhu, 如果我有一些内部分组如何进行相同操作。 例如 IF OBJECT_ID (' TempDB ..#TestSample') IS NOT NULL DROP 表 #TestSample CREATE TABLE #TestSample(Sno INT ,Group1 INT ,打开 INT ,Plusvalue INT ,MinusValue INT ,结束 INT ) INSERT INTO #TestSample(Sno,Group1,Opening,Plusvalue,MinusValue,Closing) VALUES ( 1 , 1 , 0 , 5 , 2 , 0 ),( 1 , 2 , 0 , 8 , 4 , 0 ),( 1 , 3 , 0 , 8 , 6 , 0 ),( 2 , 1 , 0 , 9 , 7 , 0 ),( 2 , 2 , 0 , 7 , 2 , 0 ) 选择 * 来自 #TestSample 我的要求输出是 Sno Group1开盘加值MinusValue收盘 1 1 0 5 2 3 1 2 3 8 4 7 1 3 7 8 6 9 2 1 0 9 7 2 2 2 2 7 2 7 (即)对于Sno 1单独组和Sno 2单独组。 请告诉我解决方案。 嗨Kirthiga, 试试这个 - 必需输出 SELECT T. Sno,Group1,(ISNULL(( SELECT SUM((Opening + Plusvalue-MinusValue)) FROM #TestSample WHERE Group1< T.Group1 AND Sno = T.SNo), 0 ))' 打开', T.Plusvalue,T.MinusValue,(ISNULL(( SELECT SUM((Opening + Plusvalue-MinusValue)) FROM #TestSample WHERE Group1< T.Group1 AND Sno = T.SNo), 0 )+ T.Plusvalue - T.MinusValue)' 关闭' FROM #TestSample T 祝你有愉快的一天..... :-) 问候, Venkatesh。 Hi,Im having a table with 4 columns as belowSnoOpeningPlusvalue MinusValueClosing105 2 0208 4 0308 6 0409 7 0Formula for arriving my closing column isClosing = Opening+Plusvalue-MinusValueFormula for arriving my opening column isOpening = Previous column closingMy required output isSnoOpeningPlusvalue MinusValueClosing105 2 3238 4 7378 6 9499 7 11I need a query to acheive this output 解决方案 Hi Krithiga,Check my following Sample TSQL Script.-- Table Creation StatementIF OBJECT_ID('TempDB..#TestSample') IS NOT NULL DROP TABLE #TestSampleCREATE TABLE #TestSample(Sno INT,Opening INT,Plusvalue INT,MinusValue INT,Closing INT)-- Insert Sample ValuesINSERT INTO #TestSample(Sno, Opening, Plusvalue, MinusValue, Closing)VALUES(1, 0, 5, 2, 0),(2, 0, 8, 4, 0),(3, 0, 8, 6, 0),(4, 0, 9, 7, 0)-- Given DataSELECT Sno, Opening, Plusvalue, MinusValue, Closing FROM #TestSample-- Required OutputSELECT T.Sno, (ISNULL((SELECT SUM((Opening+Plusvalue-MinusValue)) FROM #TestSample WHERE Sno < T.Sno),0)) 'Opening',T.Plusvalue, T.MinusValue,(ISNULL((SELECT SUM((Opening+Plusvalue-MinusValue)) FROM #TestSample WHERE Sno < T.Sno),0) + T.Plusvalue - T.MinusValue) 'Closing'FROM #TestSample TRegards,GVPrabuHi Prabhu,If I have some internal grouping how to proceed the same.For ExampleIF OBJECT_ID('TempDB..#TestSample') IS NOT NULL DROP TABLE #TestSampleCREATE TABLE #TestSample(Sno INT,Group1 INT,Opening INT,Plusvalue INT,MinusValue INT,Closing INT)INSERT INTO #TestSample(Sno, Group1, Opening, Plusvalue, MinusValue, Closing)VALUES(1, 1, 0, 5, 2, 0),(1, 2, 0, 8, 4, 0),(1, 3, 0, 8, 6, 0),(2, 1, 0, 9, 7, 0),(2, 2, 0, 7, 2, 0)select * from #TestSampleMy Required output isSnoGroup1OpeningPlusvalueMinusValueClosing110523123847137869210972222727(ie) For Sno 1 seperate group and for Sno 2 seperate group.Please tell me the solution.Hi Kirthiga,Try This-- Required OutputSELECT T.Sno, Group1, (ISNULL((SELECT SUM((Opening+Plusvalue-MinusValue)) FROM #TestSample WHERE Group1 < T.Group1 AND Sno=T.SNo),0)) 'Opening',T.Plusvalue, T.MinusValue,(ISNULL((SELECT SUM((Opening+Plusvalue-MinusValue)) FROM #TestSample WHERE Group1 < T.Group1 AND Sno=T.SNo),0) + T.Plusvalue - T.MinusValue) 'Closing'FROM #TestSample THave a nice day..... :-)Regards,Venkatesh. 这篇关于如何将以前的列输出作为当前列输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 17:19