Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have the following two queries which run separately. Is is possible to combine them in to a single query to save time?
SELECT COUNT(f_ID) AS f_totalactive FROM tb_assets WHERE f_assetname IS NOT NULL SELECT COUNT(f_ID) AS f_totalinactiveFROM tb_assetsWHERE f_assetname IS NULL
As you can see, one query gets the results where f_assetname is NULL (to get inactive) and the other where f_assetname is NOT NULL (to get the active assets). Because the "WHERE" clause is different between the two, I didn't know if it was possible to combine them.Thanks in advance!
Brett, thanks for the reply. I'm receiving an error on the "SUM"Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'SUM'.
SELECT SUM(CASE WHEN f_assetname IS NOT NULL THEN 1 ELSE 0 END) AS f_totalactiveSUM(CASE WHEN f_assetname IS NULL THEN 1 ELSE 0 END) AS f_totalinactiveFROM tb_assets
mattboy_slim
Yak Posting Veteran
72 Posts
Posted - 2012-03-27 : 13:26:06
Sorry, I should have looked closer. I had to add a comma at the end of the first line.
SELECT SUM(CASE WHEN f_assetname IS NOT NULL THEN 1 ELSE 0 END) AS f_totalactive,SUM(CASE WHEN f_assetname IS NULL THEN 1 ELSE 0 END) AS f_totalinactiveFROM tb_assets