How to get the correct Return_Amt from a table?

 Author Message David Looi Forum Newbie Group: General Forum Members Points: 2 Visits: 17 Please can anyone help me?Table Name: tableSTKItemID varchar(8)ItemDesc varchar(100)Price moneyTable Name: tableABCItemID varchar(8)ConditionAmt moneyReturn_Amt moneyThe ConditionAmt and Return_Amt in the tableABC can be some other values based on the ItemID.Here is the sample with different values.ItemID ItemDesc Price 04400110 A4 Paper 30.0008800220 Bic Pen 5.0 20.00ItemID ConditionAmt Return_Amt04400110 100.00 28.5004400110 200.00 27.0004400110 300.00 25.5008800220 50.00 19.0008800220 80.00 18.0008800220 150.00 15.0008800220 200.00 14.00I need a query to get the correct value from the Return_Amt based on the ConditionAmt of a total purchase amt on the particular ItemID.Examples A.John have a total purchase amt of 105.00.When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 100.00 and below 200 and below 300 the Return_Amt is 28.50 as SellingPrice.B.John have a total purchase amt of 250.00.When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 200.00 and below 300 the Return_Amt is 27.00 as SellingPrice.C.John have a total purchase amt of 400.00.When he purchase ItemID 04400110 while total purchase amt is more than ConditionAmt 300.00 the Return_Amt is 28.50 as SellingPrice.D.John have a total purchase amt of 50.00.When he purchase ItemID 04400110 while total purchase amt is less than ConditionAmt 100.00 therefore the ItemID 04400110 Price 30.00 as SellingPrice.E.Mary have a total purchase amt of 95.00.When she purchase ItemID 08800220 while total purchase amt is more than ConditionAmt 80.00 and below 150 and below 200 the Return_Amt is 18.00 as SellingPrice.Thanks.jwlooi Kingston Dhasian SSChampion Group: General Forum Members Points: 11427 Visits: 5331 One of the ways to achieve it..`DECLARE @tableSTK TABLE( ItemID VARCHAR(8), ItemDesc VARCHAR(100), Price MONEY)DECLARE @tableABC TABLE( ItemID VARCHAR(8), ConditionAmt MONEY, Return_Amt MONEY)INSERT @tableSTK ( ItemID, ItemDesc, Price )SELECT '04400110', 'A4 Paper', 30.00 UNION ALLSELECT '08800220', 'Bic Pen 5.0', 20.00INSERT @tableABC ( ItemID, ConditionAmt, Return_Amt )SELECT '04400110', 100.00, 28.50 UNION ALLSELECT '04400110', 200.00, 27.00 UNION ALLSELECT '04400110', 300.00, 25.50 UNION ALLSELECT '08800220', 50.00, 19.00 UNION ALLSELECT '08800220', 80.00, 18.00 UNION ALLSELECT '08800220', 150.00, 15.00 UNION ALLSELECT '08800220', 200.00, 14.00DECLARE @ConditionAmt INT, @ItemID VARCHAR(8)SELECT @ConditionAmt = 95.00, @ItemID = '08800220'; WITH cte_table AS( SELECT ROW_NUMBER() OVER( PARTITION BY ItemID ORDER BY ConditionAmt ) AS RN, * FROM ( SELECT ItemID, 0 AS ConditionAmt, Price FROM @tableSTK UNION ALL SELECT ItemID, ConditionAmt, Return_Amt FROM @tableABC ) AS T)SELECT T1.ItemID, T1.PriceFROM cte_table AS T1LEFT JOIN cte_table AS T2 ON T1.ItemID = T2.ItemID AND T1.RN = T2.RN - 1WHERE T1.ItemID = @ItemID AND T1.ConditionAmt <= @ConditionAmt AND ( T2.ConditionAmt > @ConditionAmt OR T2.ConditionAmt IS NULL )` Kingston DhasianHow to post data/code on a forum to get the best help - Jeff Modenhttp://www.sqlservercentral.com/articles/Best+Practices/61537/ David Looi Forum Newbie Group: General Forum Members Points: 2 Visits: 17 WOW! Thanks for your help. It solve my problem.jwlooiKingston Dhasian (9/11/2013)One of the ways to achieve it..`DECLARE @tableSTK TABLE( ItemID VARCHAR(8), ItemDesc VARCHAR(100), Price MONEY)DECLARE @tableABC TABLE( ItemID VARCHAR(8), ConditionAmt MONEY, Return_Amt MONEY)INSERT @tableSTK ( ItemID, ItemDesc, Price )SELECT '04400110', 'A4 Paper', 30.00 UNION ALLSELECT '08800220', 'Bic Pen 5.0', 20.00INSERT @tableABC ( ItemID, ConditionAmt, Return_Amt )SELECT '04400110', 100.00, 28.50 UNION ALLSELECT '04400110', 200.00, 27.00 UNION ALLSELECT '04400110', 300.00, 25.50 UNION ALLSELECT '08800220', 50.00, 19.00 UNION ALLSELECT '08800220', 80.00, 18.00 UNION ALLSELECT '08800220', 150.00, 15.00 UNION ALLSELECT '08800220', 200.00, 14.00DECLARE @ConditionAmt INT, @ItemID VARCHAR(8)SELECT @ConditionAmt = 95.00, @ItemID = '08800220'; WITH cte_table AS( SELECT ROW_NUMBER() OVER( PARTITION BY ItemID ORDER BY ConditionAmt ) AS RN, * FROM ( SELECT ItemID, 0 AS ConditionAmt, Price FROM @tableSTK UNION ALL SELECT ItemID, ConditionAmt, Return_Amt FROM @tableABC ) AS T)SELECT T1.ItemID, T1.PriceFROM cte_table AS T1LEFT JOIN cte_table AS T2 ON T1.ItemID = T2.ItemID AND T1.RN = T2.RN - 1WHERE T1.ItemID = @ItemID AND T1.ConditionAmt <= @ConditionAmt AND ( T2.ConditionAmt > @ConditionAmt OR T2.ConditionAmt IS NULL )`

Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12