Link to home
Start Free TrialLog in
Avatar of goodk
goodkFlag for United States of America

asked on

ok, this query works - but how do I see the null values for Type?

Select Job,[type1],[type2]
FROM (
SELECT Job,type,
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR nYears IN([type1],[type2])
  ) AS pvt

my table is simple,
it has columns
 date,job,incredit,outdebit

I am using mssql 8 express
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

SELECT Job,type,
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
WHERE type IS NOT NULL

should work
sorry I misread it
Avatar of sshah254
sshah254

Try this ...

Select Job,[type1],[type2]
FROM (
SELECT Job,isnull(type, ''),
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR nYears IN([type1],[type2], '')
  ) AS pvt

Ss
Avatar of goodk

ASKER

ok, my fields are Job,Type,  InCredits and OutDebits and what I am saying is that if few types are null then it should add the values of the nulls.

I am getting typical errors in Server management studio
Select Job,[fee],[tax]
FROM (
SELECT Job,isnull(type, ''),
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR type IN([fee],[tax],'')
  ) AS pvt

Select Job,[fee],[tax],[nil]
FROM (
SELECT Job,isnull(type, 'nil'),
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR type IN([fee],[tax],[nil]
  ) AS pvt
Missed a bracket there


FOR type IN([fee],[tax],[nil])

Select Job,[fee],[tax],[nil]
FROM (
SELECT Job,isnull(type, 'nil'),
Isnull(InCredit,0)+isnull(OutDebit,0) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR type IN([fee],[tax],[nil])
  ) AS pvt
Avatar of goodk

ASKER

This is working fine, the only problem is that the column shows up even if there are no nulls, Is there a way to remove null column if all Type fields are filled? thanks



Select Job,[fee],[tax],[nil]
FROM (
SELECT Job,isnull(Type, 'nil') as xtype,
Isnull(InCredit,0)+isnull(OutDebit,0) AS SumOfAmount
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR xtype IN([fee],[tax],[nil])
  ) AS pvt


no directly. You will have to use dymamic SQL in that case. Check the below:
declare @strSQL varchar(max)
declare @cols varchar(max)


set @cols = stuff((SELECT distinct ', ' + isnull(Type, 'nil') FROM AceData for xml path('')), 1, 2, '')

set @strSQL = 'Select Job, ' + @cols
		FROM ( 
		SELECT Job,isnull(Type, ''nil'') as xtype, 
		Isnull(InCredit,0)+isnull(OutDebit,0) AS SumOfAmount 
		FROM AceData 
		) 
		ps PIVOT( 
			SUM (SumOfAmount) 
			FOR xtype IN(' + @cols + ') AS pvt '

exec(@strSQL)

Open in new window

Avatar of goodk

ASKER

what am I doing wrong? I get the following errors.

Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'nil'.
Msg 105, Level 15, State 1, Line 15
Unclosed quotation mark after the character string '

exec(@strSQL)
'.
sorry some bugs in line 5
declare @strSQL varchar(max)
declare @cols varchar(max)


set @cols = stuff((SELECT distinct '], [' + isnull(Type, 'nil') FROM AceData for xml path('')), 1, 2, '') + ']'

set @strSQL = 'Select Job, ' + @cols
		FROM ( 
		SELECT Job,isnull(Type, ''nil'') as xtype, 
		Isnull(InCredit,0)+isnull(OutDebit,0) AS SumOfAmount 
		FROM AceData 
		) 
		ps PIVOT( 
			SUM (SumOfAmount) 
			FOR xtype IN(' + @cols + ') AS pvt '

exec(@strSQL)

Open in new window

Avatar of goodk

ASKER

does not run - still see errors - I am using sql 8 express
Avatar of Sharath S
>> does not run - still see errors

What are the errors?
Avatar of goodk

ASKER


Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'nil'.
Msg 105, Level 15, State 1, Line 15
Unclosed quotation mark after the character string '

exec(@strSQL)
'.
check line 8 here:
declare @strSQL varchar(max)
declare @cols varchar(max)


set @cols = stuff((SELECT distinct '], [' + isnull(Type, 'nil') FROM AceData for xml path('')), 1, 2, '') + ']'

set @strSQL = 'Select Job, ' + @cols
		+ ' FROM ( 
		SELECT Job,isnull(Type, ''nil'') as xtype, 
		Isnull(InCredit,0)+isnull(OutDebit,0) AS SumOfAmount 
		FROM AceData 
		) 
		ps PIVOT( 
			SUM (SumOfAmount) 
			FOR xtype IN(' + @cols + ') AS pvt '

exec(@strSQL)

Open in new window

Avatar of goodk

ASKER

may be the last one - thanks

Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there was a missing bracket in line 15. This should work:

...                  FOR xtype IN(' + @cols + ')) AS pvt '
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of goodk

ASKER

thanks