Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

join two PIVOT table

$
0
0

with NoOfOrder 
as (
SELECT  Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Jan,Feb,Mar
FROM (
    select LEFT(datename(month,InvoiceDate),3) mon,InvoiceNo as InvoiceNo
    from tbl_InvoiceMain ,tbl_OrderMain,tbl_CompanyMaster    
    where tbl_InvoiceMain.OrderID = tbl_OrderMain.OrderID

	and (CAST(tbl_InvoiceMain.InvoiceDate AS date) BETWEEN tbl_CompanyMaster.YearStart  AND tbl_CompanyMaster.YearEnd)
) P
PIVOT (count(InvoiceNo)for mon in (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)) PV ),
OntimeDelivery 
as (
SELECT  Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Jan,Feb,Mar
FROM (
    select LEFT(datename(month,InvoiceDate),3) mon,InvoiceNo as InvoiceNo
    from tbl_InvoiceMain ,tbl_OrderMain,tbl_CompanyMaster    
    where tbl_InvoiceMain.OrderID = tbl_OrderMain.OrderID

	and (CAST(tbl_InvoiceMain.InvoiceDate AS date) BETWEEN tbl_CompanyMaster.YearStart  AND tbl_CompanyMaster.YearEnd)
	and  CAST(tbl_InvoiceMain.InvoiceDate AS date) <= CAST(tbl_OrderMain.ScheduledDispatchDate AS date)
) P
PIVOT (count(InvoiceNo)for mon in (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)) PV )

select * from NoOfOrder
union all
select * from OntimeDelivery 

It giving resulte

Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec	Jan	Feb	Mar
18	35	39	52	32	47	47	22	14	0	0	0
9	10	16	22	6	11	19	10	5	0	0	0

Expected Resulte

			Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec	Jan	Feb	Mar	Total
NoOfOrder		18	35	39	52	32	47	47	22	14	0	0	0	306
OnTimeDelivered		9	10	16	22	6	11	19	10	5	0	0	0	108
DeliverPerformance%	50.00	28.57	41.03	42.31	18.75	23.40	40.43	45.45	35.71	0.00	0.00	0.00	35.29

DeliverPerformance% formula = (OnTimeDelivered/NoOfOrder) * 100 e.g ( (9/18)*100) = 50

thanks in advance



i am using sql server 2008 i have quer

i am using sql server 2008 i have quer


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>