WITH JC as -- Job Card
(
select distinct(JOBCARDNOSTR),'Job Card' as Document, JobCardId, count(*) Count from JobCardMaster where (JobCardId = 50075 ) group by JobCardId,JOBCARDNOSTR
),
PR as -- Purchase Request
(
select distinct(PurchaseRequestNo),'Purchase Request' as Document, JobCardId, count(*) Count from PurchaseRequestMaster where (JobCardId = 50075) group by JobCardId,PurchaseRequestNo
),
ENQ as ---- Enquiry
(
select distinct(enquiryno),'Enquiry' as Document, JobCardId, count(*) Count from EnquiryMaster where (JobCardId = 50075 ) group by JobCardId,enquiryno
),
CMPRTV as ---- Comparative
(
select distinct(ComparativeNo),'Comparative' as Document, JobCardId ,count(*) Count from ComparativeMaster where (JobCardId = 50075 ) group by JobCardId,ComparativeNo
),
PO as ---- Purchase Order
(
select 'Purchase Order' as Document, JobCardId, poorderno,count(*) Count from PurchaseOrderMaster where (JobCardId = 50075 ) group by JobCardId,poorderno
),
GRN as ---- GRN
(
Select distinct(GrnGinNo),'GRN' as Document, JobCardId, count(*) Count from Gin where (JobCardId = 50075 ) group by JobCardId,GrnGinNo
),
TI as ---- Tax Invoice
(
Select distinct(GrnExInvno),'Tax Invoice' as Document, JobCardId, count(*) Count from Gin where (JobCardId = 50075 ) group by JobCardId,GrnExInvno
),
DC as -- DC
(
Select distinct(dcrefno),'DC' as Document, JobCardId,count(*) Count from DCMaster where (JobCardId = 50075 ) group by JobCardId,dcrefno
)
select
ISNULL(JC.Count,0) as JCrdCount, JC.JOBCARDNOSTR,ISNULL(PR.Count,0) as PRCount, pr.PurchaseRequestNo
,ENQ.enquiryno, ISNULL(ENQ.Count,0) as ENQCount
,CMPRTV.ComparativeNo, ISNULL(CMPRTV.Count,0) as CMPRTVCount
, PO.poorderno,ISNULL(PO.Count,0) as POCount,
GRN.GrnGinNo,ISNULL(GRN.Count,0) as GRNCount
, TI.GrnExInvno,ISNULL(TI.Count,0) as TICount,
DC.dcrefno,ISNULL(DC.Count,0) as DCCount
FROM JC
--JCrd
left JOIN PR ON JC.JobCardId = PR.JobCardId
left JOIN ENQ ON JC.JobCardId = ENQ.JobCardId
left JOIN CMPRTV ON JC.JobCardId = CMPRTV.JobCardId
left JOIN PO ON JC.JobCardId = PO.JobCardId
left JOIN GRN ON JC.JobCardId = GRN.JobCardId
left JOIN TI ON JC.JobCardId = TI.JobCardId
left JOIN DC ON JC.JobCardId = DC.JobCardId
order by JC.JobCardId desc
distinct
不是一个函数。它并不意味着“在括号中紧随其后的值(某个值)上不同”,它只是意味着“在所有列上不同(其中一些可以在括号中)” .
因此,如果您只想在每个子查询中每个
JobCardId
一行,则仅按该分组。
WITH JC as
-- Job Card
(
select
MIN(JOBCARDNOSTR) AS JOBCARDNOSTR,
'Job Card' as Document,
JobCardId,
count(*) Count
from JobCardMaster
where JobCardId = 50075
group by JobCardId
),
PR as
-- Purchase Request
(
select
MIN(PurchaseRequestNo) AS PurchaseRequestNo,
'Purchase Request' as Document,
JobCardId, count(*) Count
from PurchaseRequestMaster
where (JobCardId = 50075)
group by JobCardId
),
ENQ as
---- Enquiry
(
select
MIN(enquiryno) AS enquiryno,
'Enquiry' as Document,
JobCardId,
count(*) Count
from EnquiryMaster
where (JobCardId = 50075 )
group by JobCardId
),
CMPRTV as
---- Comparative
(
select
MIN(ComparativeNo) AS ComparativeNo,
'Comparative' as Document,
JobCardId,
count(*) Count
from ComparativeMaster
where (JobCardId = 50075 )
group by JobCardId
),
PO as
---- Purchase Order
(
select
'Purchase Order' as Document,
JobCardId,
MIN(poorderno) AS poorderno,
count(*) Count
from PurchaseOrderMaster
where (JobCardId = 50075 )
group by JobCardId
),
GRN as
---- GRN
(
Select
MIN(GrnGinNo), 'GRN' as Document, JobCardId, count(*) Count
from Gin
where (JobCardId = 50075 )
group by JobCardId
),
TI as
---- Tax Invoice
(
Select MIN(GrnExInvno) AS GrnExInvno, 'Tax Invoice' as Document, JobCardId, count(*) Count
from Gin
where (JobCardId = 50075 )
group by JobCardId
),
DC as
-- DC
(
Select MIN(dcrefno) AS dcrefno, 'DC' as Document, JobCardId, count(*) Count
from DCMaster
where (JobCardId = 50075 )
group by JobCardId,dcrefno
)
select
ISNULL(JC.Count,0) as JCrdCount,
JC.JOBCARDNOSTR,
ISNULL(PR.Count,0) as PRCount,
pr.PurchaseRequestNo,
ENQ.enquiryno,
ISNULL(ENQ.Count,0) as ENQCount,
CMPRTV.ComparativeNo,
ISNULL(CMPRTV.Count,0) as CMPRTVCount,
PO.poorderno,
ISNULL(PO.Count,0) as POCount,
GRN.GrnGinNo,
ISNULL(GRN.Count,0) as GRNCount,
TI.GrnExInvno,
ISNULL(TI.Count,0) as TICount,
DC.dcrefno,
ISNULL(DC.Count,0) as DCCount
FROM JC
--JCrd
left JOIN PR ON JC.JobCardId = PR.JobCardId
left JOIN ENQ ON JC.JobCardId = ENQ.JobCardId
left JOIN CMPRTV ON JC.JobCardId = CMPRTV.JobCardId
left JOIN PO ON JC.JobCardId = PO.JobCardId
left JOIN GRN ON JC.JobCardId = GRN.JobCardId
left JOIN TI ON JC.JobCardId = TI.JobCardId
left JOIN DC ON JC.JobCardId = DC.JobCardId
order by
JC.JobCardId desc;