Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  General SQL Server Forums  New to SQL Server Programming  query Reply to Topic  Printer Friendly
Author  Topic

kt
Yak Posting Veteran

88 Posts

 Posted - 08/05/2013 :  11:35:57 Hi,This query gave me the results below. I only want the result to with quanity only meaning no 0 from the results. Can you pls show me how?Thanksselect ISNULL((SELECT SUM(del_qty) FROM tbl_delcty WITH(NOLOCK) WHERE RIGHT(saty, CASE WHEN LEN(saty)>=5 THEN LEN(saty)-5 ELSE 0 END) = ct.cty AND ctr = 2 GROUP BY RIGHT(saty,CASE WHEN LEN(saty)>=5 THEN LEN(saty)-5 ELSE 0 END)),0) + ISNULL((SELECT ISNULL(SUM(del_qty),0) FROM tbl_crt WITH(NOLOCK) WHERE cty= ct.cty AND ctr = 2 GROUP BY cty),0) AS devqty FROM tbl_cth ct with (NoLock) WHERE gt = '6'devqty======0002475

James K
Flowing Fount of Yak Knowledge

3873 Posts

 Posted - 08/05/2013 :  11:43:06 You can add to the WHERE clause at the end. ```SELECT ISNULL(( SELECT SUM(del_qty) FROM tbl_delcty WITH ( NOLOCK ) WHERE RIGHT(saty, CASE WHEN LEN(saty) >= 5 THEN LEN(saty) - 5 ELSE 0 END) = ct.cty AND ctr = 2 GROUP BY RIGHT(saty, CASE WHEN LEN(saty) >= 5 THEN LEN(saty) - 5 ELSE 0 END) ), 0) + ISNULL(( SELECT ISNULL(SUM(del_qty), 0) FROM tbl_crt WITH ( NOLOCK ) WHERE cty = ct.cty AND ctr = 2 GROUP BY cty ), 0) AS devqty FROM tbl_cth ct WITH ( NOLOCK ) WHERE gt = '6' AND devqty <> 0```An additional comment: there may be opportunities to improve your code (for better performance, tighter logic, ease of understanding etc.), but I couldn't gleam enough info to suggest something from the query you posted.

kt
Yak Posting Veteran

88 Posts

 Posted - 08/05/2013 :  11:54:32 alreay tried that but got error for Invalid column name devqty.

James K
Flowing Fount of Yak Knowledge

3873 Posts

 Posted - 08/05/2013 :  12:06:03 quote:Originally posted by ktalreay tried that but got error for Invalid column name devqty.Of course! silly me!!```SELECT * FROM ( SELECT ISNULL(( SELECT SUM(del_qty) FROM tbl_delcty WITH ( NOLOCK ) WHERE RIGHT(saty, CASE WHEN LEN(saty) >= 5 THEN LEN(saty) - 5 ELSE 0 END) = ct.cty AND ctr = 2 GROUP BY RIGHT(saty, CASE WHEN LEN(saty) >= 5 THEN LEN(saty) - 5 ELSE 0 END) ), 0) + ISNULL(( SELECT ISNULL(SUM(del_qty), 0) FROM tbl_crt WITH ( NOLOCK ) WHERE cty = ct.cty AND ctr = 2 GROUP BY cty ), 0) AS devqty FROM tbl_cth ct WITH ( NOLOCK ) WHERE gt = '6' )s WHERE devqty <> 0```

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 08/05/2013 :  12:19:32 Try this one```SELECT ISNULL(f.qty, 0) + ISNULL(s.qty, 0) AS devqty FROM dbo.tbl_cth AS ct WITH (NOLOCK) LEFT JOIN ( SELECT SUBSTRING(saty, 6, LEN(saty)) AS cty, SUM(del_qty) AS qty FROM dbo.tbl_delcty WITH (NOLOCK) WHERE ctr = 2 GROUP BY SUBSTRING(saty, 6, LEN(saty)) ) AS f ON f.cty = ct.cty LEFT JOIN ( SELECT cty, SUM(del_qty) AS qty FROM dbo.tbl_crt WITH (NOLOCK) WHERE ctr = 2 GROUP BY cty ) AS s ON s.cty = ct.cty WHERE ct.gt = '6' AND ISNULL(f.qty, 0) + ISNULL(s.qty, 0) <> 0;```Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

kt
Yak Posting Veteran

88 Posts

 Posted - 08/05/2013 :  14:21:03 thanks for everyone
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC