Click here to Skip to main content
14,875,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Query 1: select country,COUNT(*) as Registered_Candidates
from candidate_details
where entrydate between '2001-07-01' and '2013-07-16'
group by country
output Query 1:
country| Registered_Candidates
Algeria| 1
Australia| 1
Bangladesh| 1
Croatia| 1
Egypt| 1
Germany| 1
India| 38
New Zealand| 1
Pakistan| 1
Poland| 1
Russia| 1
Ukraine| 1
Yugoslavia| 2

Query 2: select country,COUNT(valid) as NonValidated
from candidate_details
where valid <> 1 and entrydate between '2001-07-01' and '2013-07-16'
group by country
Output query 2:
country| NonValidated
Algeria| 1
Australia| 1
Bangladesh| 1
Croatia| 1
Egypt| 1
Germany| 1
India| 33
New Zealand| 1
Pakistan| 1
Poland| 1
Russia| 1
Ukraine| 1
Yugoslavia| 2

I want query which will give me output as following:

country| NonValidated Registered_Candidates
Algeria| 1|1
Australia| 1|1
Bangladesh| 1|1
Croatia| 1|1
Egypt| 1|1
Germany| 1|1
India| 33|38
New Zealand| 1|1
Pakistan| 1|1
Poland| 1|1
Russia|1|1
Ukraine| 1|1
Yugoslavia| 2|1
Posted

Try This..
SQL
Select a.country,b.Nonvalidated,a.Registered_Candidates From
(select country,COUNT(*) as Registered_Candidates
from candidate_details
where entrydate between '2001-07-01' and '2013-07-16'
group by country)a
Join
(select country,COUNT(valid) as NonValidated
from candidate_details
where valid <>1 and entrydate between '2001-07-01' and '2013-07-16'
group by country
)b
on a.country=b.country
   
Comments
asassss 17-Jul-13 5:44am
   
Thanks man.. its working..
Raja Sekhar S 17-Jul-13 5:46am
   
You are welcome......
SQL
SELECT
    country,
    COUNT(CASE
            WHEN entrydate >= '2001-01-01' AND entrydate < '2013-08-01'AND valid<>1
                THEN entrydate
            ELSE NULL
            END) AS [NV] ,
    COUNT(CASE
            WHEN entrydate >= '2001-06-01' AND entrydate < '2013-08-01'
                THEN entrydate
            ELSE NULL
            END) AS [RC]
FROM candidate_details
GROUP BY country



--Second Solution

SQL
SELECT
    country,
    CAST(
    COUNT(CASE
            WHEN entrydate >= '2001-01-01' AND entrydate < '2013-08-01'AND valid<>1
                THEN entrydate
            ELSE NULL
            END) AS VarCHAR(3)) + '|' +
            CAST(
    COUNT(CASE
            WHEN entrydate >= '2001-06-01' AND entrydate < '2013-08-01'
                THEN entrydate
            ELSE NULL
            END) AS VarCHAR(3)) 'NV/RC'
FROM candidate_details
GROUP BY country
   
v2
Select a.country,b.Nonvalidated,a.Registered_Candidates,c.Validated From
(select country,COUNT(*) as Registered_Candidates
from candidate_details
where entrydate between '2001-07-01' and '2013-08-01'
group by country)a
Join
(select country,COUNT(valid) as NonValidated
from candidate_details
where valid <>1 and entrydate between '2001-07-01' and '2013-08-01'
group by country
)b
on a.country=b.country
left join
(
select country,ISNULL(COUNT(valid),0) as Validated
from candidate_details
where valid = 1 and entrydate between '2001-07-01' and '2013-07-16'
group by country
)c
on a.country = c.country

country Nonvalidated Registered_Candidates Validated
Algeria 1 1 NULL
Australia 1 1 NULL
Bangladesh 1 1 NULL
Croatia 1 1 NULL
Egypt 1 1 NULL
Germany 1 1 NULL
India 33 38 5
New Zealand 1 1 NULL
Pakistan 1 1 NULL
Poland 1 1 NULL
Russia 1 1 NULL
Ukraine 1 1 NULL
Yugoslavia 2 2 NULL


How to show '0' instead of Null.????????
   
Comments
asassss 17-Jul-13 6:42am
   
I got the ans of this above question:
soln: ISNULL(c.Validated,0)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900