Hello everyone, I have a table with the description as shown below:
But, I want to display the result like this:

Can someone teach me how to do it? thanks in advance
Hello everyone, I have a table with the description as shown below:
But, I want to display the result like this:

Can someone teach me how to do it? thanks in advance
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 14, 2023, 2:05 AM EST
Hello,
Please try the below optimized query to get same result.it will make only single call to table. we can use SUM and CASE.
SELECT SUM(CASE when a.Q3_IGA_pig <> '' THEN 1 ELSE 0 END) as Q3_IGA_pig, SUM(CASE when a.Q3_IGA_goat <> '' THEN 1 ELSE 0 END) as Q3_IGA_goat, SUM(CASE when a.Q3_IGA_poultry <> '' THEN 1 ELSE 0 END) as Q3_IGA_poultry, SUM(CASE when a.Q3_IGA_Fish <> '' THEN 1 ELSE 0 END) as Q3_IGA_Fish, SUM(CASE when a.Q3_IGA_Maize <> '' THEN 1 ELSE 0 END) as Q3_IGA_Maize, SUM(CASE when a.Q3_IGA_Cassava <> '' THEN 1 ELSE 0 END) as Q3_IGA_Cassava, SUM(CASE when a.Q3_IGA_Weaning <> '' THEN 1 ELSE 0 END) as Q3_IGA_Weaning, SUM(CASE when a.Q3_IGA_Others <> '' THEN 1 ELSE 0 END) as Q3_IGA_Others FROM [DBO].[LMFA_PRF_IGA] AS aThanks
Jes SieRank #1,285 · 1.3K reputationPosted Feb 14, 2023, 1:46 AM EST
Hi Naimish, thanks for your reply. Your solution will give me a vertical result. What I wanted is a horizontal result. So, below is what I did
select COUNT(a.Q3_IGA_pig) as Q3_IGA_pig, (select COUNT(a.Q3_IGA_goat) as Q3_IGA_goat from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_goat <> '') as Q3_IGA_goat, (select COUNT(a.Q3_IGA_poultry) as Q3_IGA_poultry from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_poultry <> '') as Q3_IGA_poultry, (select COUNT(a.Q3_IGA_Fish) as Q3_IGA_Fish from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_Fish <> '') as Q3_IGA_Fish, (select COUNT(a.Q3_IGA_Maize) as Q3_IGA_Maize from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_Maize <> '') as Q3_IGA_Maize, (select COUNT(a.Q3_IGA_Cassava) as Q3_IGA_Cassava from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_Cassava <> '') as Q3_IGA_Cassava, (select COUNT(a.Q3_IGA_Weaning) as Q3_IGA_Weaning from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_Weaning <> '') as Q3_IGA_Weaning, (select COUNT(a.Q3_IGA_Others) as Q3_IGA_Others from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_Others <> '') as Q3_IGA_Others from [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_pig <> ''The above-mentioned query gives me the result that I wanted. However, if there is a simpler way to do it, I need to try.
Manikandan R128 reputationPosted Feb 14, 2023, 1:47 AM EST
Hello
Small Try,
SELECT (Select COUNT(a.Q3_IGA_pig) FROM [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_pig <>)as Q3_IGA_pig, (Select COUNT(a.Q3_IGA_goat) FROM [dbo].[LMFA_PRF_IGA] a where a.Q3_IGA_goat <>)as Q3_IGA_goat, . . . . . Your Statements..Jignesh KumarRank #91 · 22.9K reputationPosted Feb 14, 2023, 2:04 AM EST
Hello Hold On,
You can use Pivot to acheve this one,
https://www.c-sharpcorner.com/UploadFile/suthish_nair/convert-rows-to-columns-in-sql-server-20082005/
https://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 14, 2023, 1:40 AM EST
Hello,
You can use UNION operator.
Example :
SELECT count(a.Q3_IGA_pig) AS Count FROM LMFA WHERE Q3_IGA_pig <> '' UNION SELECT count(a.Q3_IGA_Goat) AS Count FROM LMFA WHERE Q3_IGA_Goat <> '' UNION SELECT count(a.Q3_IGA_Fish) AS Count FROM LMFA WHERE Q3_IGA_Fish <> '' ----- Your other select statment.Thanks