Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8667

Re: Dynamically Selecting columns in a query

$
0
0

Hi,

 

Try this query. The error for pivot table is removed. But you need to your UDF filed below query.

 

Select [C] as CardCode, [d] as ItemCode, [E] as Dscription,

 

'Aug 2013', 'Feb 2013', 'Jul 2013','Jan 2013','May 2013','Sep 2013','Dec 2013','Oct 2013','Mar 2013','Jun 2013','Apr 2013'

 

 

FROM

 

(SELECT P1.CardCode as C, P2.ItemCode as D, P2.Dscription as E,  P2.LineTotal as T, (CONVERT(CHAR(4), P1.DocDate, 100)

 

  + CONVERT(CHAR(4), P1.DocDate, 120)) as month FROM OPCH P1 INNER JOIN PCH1 P2 ON P1.DocEntry=P2.DocEntry 

 

WHERE (P2.ItemCode Like 'PROM%' AND P1.CardCode='Mike Ronnie' AND P1.DocDate BETWEEN DATEADD(year,-1,GETDATE()) and GETDATE()) OR (P2.ItemCode Like 'PROM%'  AND P1.DocDate BETWEEN DATEADD(year,-1,GETDATE()) and GETDATE())  GROUP BY P1.CardCode, P2.ItemCode, P2.Dscription,P1.DocDate,P2.LineTotal ) S 

 

Pivot

 

(SUM(T) FOR month IN ([Aug 2013],[Feb 2013],[Jul 2013],[Jan 2013],[May 2013],[Sep 2013],[Dec 2013],[Oct 2013],[Mar 2013],[Jun 2013],[Apr 2013])) P

 

Hope helpful

 

Thanks & Regards,

Nagarajan


Viewing all articles
Browse latest Browse all 8667

Trending Articles