SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kt
Yak Posting Veteran

82 Posts

Posted - 08/05/2013 :  11:35:57  Show Profile  Reply with Quote
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?

Thanks

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'


devqty
======
0
0
0
2
4
7
5

James K
Flowing Fount of Yak Knowledge

3638 Posts

Posted - 08/05/2013 :  11:43:06  Show Profile  Reply with Quote
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.
Go to Top of Page

kt
Yak Posting Veteran

82 Posts

Posted - 08/05/2013 :  11:54:32  Show Profile  Reply with Quote
alreay tried that but got error for Invalid column name devqty.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3638 Posts

Posted - 08/05/2013 :  12:06:03  Show Profile  Reply with Quote
quote:
Originally posted by kt

alreay 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
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30241 Posts

Posted - 08/05/2013 :  12:19:32  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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
Go to Top of Page

kt
Yak Posting Veteran

82 Posts

Posted - 08/05/2013 :  14:21:03  Show Profile  Reply with Quote
thanks for everyone
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000