Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
SQL
SELECT     dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo)  AS   Quantity
FROM         dbo.tblCedula INNER JOIN
             dbo.tblRequests ON dbo.tblCedula.RequestNo = dbo.tblRequests.RequestNo
             RIGHT OUTER JOIN
             dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
 GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType


and this:

SQL
SELECT     dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo)  AS   Quantity
FROM         dbo.tblBarangayClearance INNER JOIN
             dbo.tblRequests ON dbo.tblBarangayClearance.RequestNo = dbo.tblRequests.RequestNo
             RIGHT OUTER JOIN
             dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
 GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType


in the first code i have this result:
RecId CertType Quantity
1 Cedula 4
2 Clearance 0

and in the second code i have this result:
RecId CertType Quantity
1 Cedula 0
2 Clearance 2

Now i want to fuse it so i can have a result something like this:
RecId CertType Quantity
1 Cedula 4
2 Clearance 2

Note:tblBarangayClearance and tblCedula is not related to each other. But they are related to tblRequests and tblBasicPermitFeeSchedule if it's not obvious in the code.
Thanks in advance.
Posted
Updated 27-Aug-15 17:25pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Aug-15 0:25am    
I have no idea what you mean by "fusion", but keep in mind that high temperature can destroy data.
—SA
PIEBALDconsult 28-Aug-15 0:52am    
UNION ?

1 solution

/*This should get you close*/
--one way is to use union all to get all rows then you perform the GROUPING once on the entire set. 
SELECT     dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo)  AS   Quantity
FROM         dbo.tblCedula INNER JOIN
             dbo.tblRequests ON dbo.tblCedula.RequestNo = dbo.tblRequests.RequestNo
             RIGHT OUTER JOIN
             dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
 
UNION ALL

SELECT     dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo)  AS   Quantity
FROM         dbo.tblBarangayClearance INNER JOIN
             dbo.tblRequests ON dbo.tblBarangayClearance.RequestNo = dbo.tblRequests.RequestNo
             RIGHT OUTER JOIN
             dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
 GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType

 --another way is to use a subquery
SELECT dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
	COUNT(dbo.tblRequests.RequestNo) AS QuantityRequests.
	(
		SELECT dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
		COUNT(dbo.tblRequests.RequestNo) AS Quantity
		FROM dbo.tblBarangayClearance 
		INNER JOIN dbo.tblRequests 
			ON dbo.tblBarangayClearance.RequestNo = dbo.tblRequests.RequestNo
		RIGHT OUTER JOIN dbo.tblBasicPermitFeeSchedule
			ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
		GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType
	) AS QuantityRequestNo
FROM dbo.tblCedula 
INNER JOIN dbo.tblRequests 
	ON dbo.tblCedula.RequestNo = dbo.tblRequests.RequestNo
RIGHT OUTER JOIN dbo.tblBasicPermitFeeSchedule 
	ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
 
Share this answer
 
Comments
Mehdi Gholam 28-Aug-15 1:36am    
5'ed
RodzGold 28-Aug-15 1:58am    
thanks but i got errors in both solution you gave. in the first solution this is ther error:
Column 'dbo.tblBasicPermitFeeSchedule.RecId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I tried removing the RecId column but still says the next column is invalid in the select list.

also in the second solution i got errors Incorrect syntax near the word 'AS' and as i tried removing the word,it keeps saying Incorrect syntax in the next word.
Mehdi Gholam 28-Aug-15 2:19am    
Then check your separate queries work in isolation first.
RodzGold 28-Aug-15 2:21am    
I tried modifying your code like this:
SELECT dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo) AS Quantity
FROM dbo.tblCedula INNER JOIN
dbo.tblRequests ON dbo.tblCedula.RequestNo = dbo.tblRequests.RequestNo
RIGHT OUTER JOIN
dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType
UNION
SELECT dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType,
COUNT(dbo.tblRequests.RequestNo) AS Quantity
FROM dbo.tblBarangayClearance INNER JOIN
dbo.tblRequests ON dbo.tblBarangayClearance.RequestNo = dbo.tblRequests.RequestNo
RIGHT OUTER JOIN
dbo.tblBasicPermitFeeSchedule ON dbo.tblRequests.CertType = dbo.tblBasicPermitFeeSchedule.RecId
GROUP BY dbo.tblBasicPermitFeeSchedule.RecId, dbo.tblBasicPermitFeeSchedule.CertType

but the result is like this;
RecID CertType Quantity
1 Clearance 2
1 Clearance 0
2 Cedula 4
2 Cedula 0

I want a result just like this:
RecID CertType Quantity
1 Clearance 2
2 Cedula 4


i think i'm getting closer. i hope someone can give me some light. Thanks.
jgakenhe 28-Aug-15 6:06am    
If you're still working on it, you might try changing the UNION ALL to UNION or change the RIGHT OUTER JOIN to INNER JOIN. The RIGHT OUTER JOIN might be creating those records with 0 quantity.

It is tough for me, as I don't have the data.

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



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