(closed)How to use groupy by with subquery which join table? - sql

I have no idea how to group by by the column from subquery.
I want to group by languageas below:
Here is my code:
select a.name, count(a.language) as count
from
(select
temp2.name,
countrylanguage.language
from
countrylanguage
right join
temp2
on
temp2.code = countrylanguage.countrycode
) as a
group by a.language;
Edited
I got a solution as below:
select temp2.name, count(countrylanguage.language)
from countrylanguage
join temp2 on temp2.code = countrylanguage.countrycode
group by temp2.name;

You can try below - you don't need any subquery
select temp2.name,count(countrylanguage.language)
from countrylanguage join temp2
on temp2.code = countrylanguage.countrycode
group by temp2.name

Table data :
SELECT * FROM countrylanguage
SELECT * FROM temp2
SELECT a.[Name], SUM(CASE WHEN a.[language] IS NULL THEN 0 ELSE 1 END) language_count
FROM
(SELECT
temp2.[Name],
countrylanguage.[language]
FROM temp2
LEFT JOIN countrylanguage ON temp2.code = countrylanguage.countrycode) a
GROUP BY a.name

Related

Get the count of records from another table

I have a view that looks like this:
which is defined using this SQL statement:
SELECT
p.p_ccode AS C_CODE,
p.p_pcode AS P_CODE,
p.p_phcode AS PH_CODE,
PO.m_doc_type AS TYPE,
PO.crtime AS DATE
FROM
TABLE1 AS p
LEFT OUTER JOIN
DOCTABLE AS PO ON PO.m_ccode = p.p_ccode
AND PO.m_pcode = p.p_pcode
AND PO.m_phcode = p.p_phcode
Now I want to count the records for another table (TABLE 2) based on the C_CODE, P_CODE and PH_CODE and display it as a column TOTAL in my view.
How can I do this?
one way is to use subquery:
SELECT
p.p_ccode AS C_CODE,
p.p_pcode AS P_CODE,
p.p_phcode AS PH_CODE,
PO.m_doc_type AS TYPE,
PO.crtime AS DATE,
(select count(*) from tale2 t2 where t2.C_CODE = p.p_ccode and t2.P_CODE = p.P_CODE and t2.PH_CODE = p.PH_CODE) as total
FROM TABLE1 AS p
LEFT OUTER JOIN DOCTABLE AS PO
ON PO.m_ccode = p.p_ccode
AND PO.m_pcode = p.p_pcode
AND PO.m_phcode = p.p_phcode
Left join to it, group by and then count --
SELECT
p.p_ccode AS C_CODE,
p.p_pcode AS P_CODE,
p.p_phcode AS PH_CODE,
PO.m_doc_type AS TYPE,
PO.crtime AS DATE,
COUNT(P2.*) AS TOTAL
FROM TABLE1 AS p
LEFT OUTER JOIN DOCTABLE AS PO
ON PO.m_ccode = p.p_ccode
AND PO.m_pcode = p.p_pcode
AND PO.m_phcode = p.p_phcode
LEFT OUTER JOIN TABLE2 AS p2
ON p.m_ccode = p2.p_ccode
AND p.m_pcode = p2.p_pcode
AND p.m_phcode = p2.p_phcode
GROUP BY p.p_ccode, p.p_pcode, p.p_phcode, PO.m_doc_type, PO.crtime
use an CTE (Common Table Expression)
WITH CTE_COUNT
AS(
SELECT
[C_CODE]
,[P_CODE]
,[PH_CODE]
,COUNT(*) AS [TOTAL]
FROM [dbo].[TABLE2]
GROUP BY
[C_CODE]
,[P_CODE]
,[PH_CODE]
)
SELECT
t1.[C_CODE]
,t1.[P_CODE]
,t1.[PH_CODE]
,t1.[TYPE]
,t1.[DATE]
,t2.[TOTAL]
FROM [dbo].[TABLE1] AS t1
LEFT JOIN CTE_COUNT AS t2
ON t2.[C_CODE] = t1.[C_CODE]
AND t2.[P_CODE] = t1.[P_CODE]
AND t2.[PH_CODE] = t1.[PH_CODE]

Combine two queries to get the data in two columns

SELECT
tblEmployeeMaster.TeamName, SUM(tblData.Quantity) AS 'TotalQuantity'
FROM
tblData
INNER JOIN
tblEmployeeMaster ON tblData.EntryByHQCode = tblEmployeeMaster.E_HQCode
INNER JOIN
tblPhotos ON tblEmployeeMaster.TeamNo = tblPhotos.TeamNo
WHERE
IsPSR = 'Y'
GROUP BY
tblPhotos.TeamSort, tblPhotos.TeamNo, tblPhotos.Data,
tblEmployeeMaster.TeamName
ORDER BY
tblPhotos.TeamSort DESC, TotalQuantity DESC
This returns
Using this statement
select TeamName, count(TeamName) AS 'Head Count'
from dbo.tblEmployeeMaster
where IsPSR = 'Y'
group by teamname
Which returns
I would like to combine these 2 queries in 1 to get the below result.
Tried union / union all but no success :(
Any help will be very much helpful.
You can simply use the sub-query as follows:
SELECT tblEmployeeMaster.TeamName, SUM(tblData.Quantity) AS 'TotalQuantity',
MAX(HEAD_COUNT) AS HEAD_COUNT, -- USE THIS VALUE FROM SUB-QUERY
CASE WHEN MAX(HEAD_COUNT) <> 0
THEN SUM(tblData.Quantity)/MAX(HEAD_COUNT)
END AS PER_MAN_CONTRIBUTION -- column asked in comment
FROM tblData INNER JOIN
tblEmployeeMaster ON tblData.EntryByHQCode = tblEmployeeMaster.E_HQCode INNER JOIN
tblPhotos ON tblEmployeeMaster.TeamNo = tblPhotos.TeamNo
-- FOLLOWING SUB-QUERY CAN BE USED
LEFT JOIN (select TeamName, count(TeamName) AS HEAD_COUNT
from dbo.tblEmployeeMaster
where IsPSR = 'Y' group by teamname) AS HC
ON HC.TeamName = tblEmployeeMaster.TeamName
where IsPSR = 'Y'
GROUP BY tblPhotos.TeamSort, tblPhotos.TeamNo, tblPhotos.Data,tblEmployeeMaster.TeamName
order by tblPhotos.TeamSort desc, TotalQuantity desc

Sql query with 2 conditions

i have these tables:
tblCountry - country_number , country_name
tblDivingClub - club_number , country_number(FK)
tblDiver - diver_number, diver_name, startWorkingDate, endrtWorkingDate.
tblWorks_for -diver_number(FK), club_number(FK)
i need to write a query, without views or temp tables, that will diplay a lits of countries with the fields: country_number , country_name, and the number of diving clubs in that country which have 25 or more working divers at the moment (end working date IS NULL).
thats the code i wrote so far:
SELECT country_number, country_name,
( SELECT count(distinct tblDivingClub.number)
FROM tblDivingClub
inner join tblCountry on tblDivingClub.country = tblCountry.country_number
WHERE (
( SELECT count(tblWorks_for.diver_number)
FROM tblWorks_for
INNER JOIN tblDivingClub on tblWorks_for.club_number = tblDivingClub.number
WHERE tblWorks_for.end_working_date IS null
and tblDivingClub.number = tblWorks_for.club_number) >25)
) as number_of_clubs
FROM tblCountry
INNER JOIN tblDivingClub on tblCountry.country_number = tblDivingClub.country
WHERE tblCountry.country_number = tblDivingClub.country
GROUP by country_number, country_name
It's a good thing SQL anticipates the need for aggregate conditionals - if you suspected there must be a much easier way, you were right. I think you want something like:
SELECT country_number, country_name, count(distinct tblDivingClub.number)
FROM tblCountry
INNER JOIN tblDivingClub on tblCountry.country_number = tblDivingClub.country
WHERE tblCountry.country_number = tblDivingClub.country
GROUP by country_number, country_name
HAVING count(case when tblWorks_for.end_working_date IS null then 1 else null end) > 25
Try something like this:
SELECT country_number, country_name, COUNT(DISTINCT club_number)
FROM
(
SELECT tc.country_number, tc.country_name, tdc.club_number, count(*) as tot_cnt
FROM tblCountry tc
INNER JOIN tblDivingClub AS tdc ON tdc.country_number = tc.country_number
INNER JOIN tblWorks_for AS tw ON tw.club_number = tdc.club_number
INNER JOIN tblDiver AS td ON td.diver_number = tw.diver_number
WHERE endrtWorkingDate IS NULL
GROUP BY tc.country_number, tc.country_name, tdc.club_number
HAVING count(*) >= 25
) as der
GROUP BY country_number, country_name;

Oracle query - how to make count to return values with 0

How can I make a count to return also the values with 0 in it.
Example:
select count(1), equipment_name
from alarms.new_alarms
where equipment_name in (
select eqp from ne_db.ne_list)
Group by equipment_name
It is returning only the counts with values higher than 0 , but I need to know the records that are not returning anything.
Any help is greatly appreciated.
thanks,
Marco
Try using LEFT JOIN,
SELECT a.eqp, COUNT(b.equipment_name) totalCount
FROM ne_db.ne_list a
LEFT JOIN alarms.new_alarms b
ON a.eqp = b.equipment_name
GROUP BY a.eqp
If the table ne_list has no duplicates, then you can do a left join. That assumption may not be true, so the safest way to convert this is by removing duplicates in a subquery:
select count(1), ne.equipment_name
from alarms.new_alarms ne left outer join
(select distinct eqp
from ne_db.ne_list
) eqp
on ne.equipment_name = eqp.eqp
Group by ne.equipment_name
You could use a left join:
select ne.equipment_name
, count(na.equipment_name)
from ne_db.ne_list ne
left join
alarms.new_alarms na
on ne.eqp = na.equipment_name
group by
ne.equipment_name
This should work too
(select equipment_name, count(*) as cnt from alarms.new_alarms
where equipment_name in (
select eqp from ne_db.ne_list
) group by equipment_name
)union(
select equipment_name, 0 as cnt from alarms.new_alarms
where equipment_name not in (
select eqp from ne_db.ne_list
) group by equipment_name
) order by equipment_name

how to get the count in SQL Server?

I have tried a lot to figure how to get the count from two tables with respect to master table
I have three tables
Using these table values I need to get this output..
Tried but could get the desired result
http://en.wikipedia.org/wiki/Join_(SQL)
SQL - LEFT OUTER JOIN and WHERE clause
http://forums.devshed.com/oracle-development-96/combination-of-left-outer-join-and-where-clause-383248.html
You have to first GROUP BY in subqueries, then JOIN to the main table:
SELECT
a.AttributeId
, COALECSE(cntE, 0) AS cntE
, COALECSE(cntM, 0) AS cntM
FROM
AttributeMaster AS a
LEFT JOIN
( SELECT
AttributeId
, COUNT(*) AS cntE
FROM
EmployeeMaster
GROUP BY
AttributeId
) em
ON em.AttributeId = a.AttributeId
LEFT JOIN
( SELECT
AttributeId
, COUNT(*) AS cntM
FROM
MonthlyDerivedMaster
GROUP BY
AttributeId
) mdm
ON mdm.AttributeId = a.AttributeId
SELECT AttributeId,
(SELECT COUNT(Eid) FROM EmployeeMaster WHERE AttributeMaster.AttributeId = EmployeeMaster.AttributeId) as master_eid,
(SELECT COUNT(Eid) FROM MonthnlyDerivedMaster WHERE AttributeMaster.AttributeId = MonthnlyDerivedMaster.AttributeId) as monthly_eid
FROM AttributeMaster