Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am use a dynamic pivot table.But there is a problem Null value handle,
I am mention compiled query


SQL
With PivotData as ( SELECT ShapeName,
InvoiceDetails.Purity,

cast(InvoiceDetails.Purity as varchar(10)) + 'KRate' as RValue,

(select  ISNULL( sum (InvD.NetWeight - InvD.Stoneweight * 0.2 ),0.00)  from InvoiceDetails InvD where InvD.InvoiceId = 'Ex2832' and InvD.Purity=InvoiceDetails.Purity) as 'GoldWeight',

(select Rate from GoldPrice Where Purity=InvoiceDetails.Purity) as 'GoldPrice'
FROM Invoice
INNER JOIN   InvoiceDetails ON Invoice.InvoiceId  =InvoiceDetails.InvoiceId
Where Invoice.InvoiceId ='Ex2832' )

 select * from (
select * from PivotData
  pivot (max ( GoldPrice) for  RValue in ( [14KRate],[18KRate],[22KRate]) ) as pvt
  )
 as PWD   



Result is --------


 



ShapeName	        Purity	GoldWeight	14KRate	18KRate	22KRate
EarRings	         22	4.946	         NULL	NULL	46.12
Faceted Gold Beads 18K	 18	5.590	         NULL	37.73	NULL
Gold Caps 14K	         14	3.900	         29.35	NULL	NULL
Three Piece Set	         18	5.590	         NULL	37.73	NULL



But my requirement is -----
ShapeName	        Purity	GoldWeight	14KRate	18KRate	22KRate
EarRings	         22	4.946	         29.35	37.73	46.12
Faceted Gold Beads 18K	 18	5.590	         29.35	37.73	46.12
Gold Caps 14K	         14	3.900	         29.35	37.73	46.12
Three Piece Set	         18	5.590	         29.35	37.73	46.12



any body help me please how to handle this.
Posted

1 solution

Now i am get solution


SQL
With PivotData as ( SELECT ShapeName,
InvoiceDetails.Purity,

cast(InvoiceDetails.Purity as varchar(10)) + 'KRate' as RValue,

(select Rate from GoldPrice Where Purity=InvoiceDetails.Purity) as 'GoldPrice'
FROM Invoice
INNER JOIN   InvoiceDetails ON Invoice.InvoiceId  =InvoiceDetails.InvoiceId
Where Invoice.InvoiceId ='Ex2832' )

select ShapeName,Purity,case when  ([14KRate]) IS null   then  (select max([14KRate]) from PivotData pivot (max ( GoldPrice) for  RValue in ([14KRate]) ) as pv) else [14KRate]end  as [14KRate],case when  ([18KRate]) IS null   then  (select max([18KRate]) from PivotData pivot (max ( GoldPrice) for  RValue in ([18KRate]) ) as pv) else [18KRate]end  as [18KRate],case when  ([22KRate]) IS null   then  (select max([22KRate]) from PivotData pivot (max ( GoldPrice) for  RValue in ([22KRate]) ) as pv) else [22KRate]end  as [22KRate] from PivotData
pivot (MAX(GoldPrice) for RValue in ( [14KRate],[18KRate],[22KRate])) as pvt


And Get Result

ShapeName	          Purity	14KRate	18KRate	22KRate
Gold Caps 14K	              14	29.35	37.73	46.12
Faceted Gold Beads 18K	      18	29.35	37.73	46.12
Three Piece Set               18	29.35	37.73	46.12
EarRings	              22	29.35	37.73	46.12
 
Share this answer
 

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