goodk
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
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
sorry I misread it
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
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
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
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])
FOR type IN([fee],[tax],[nil])
Select Job,[fee],[tax],[nil]
FROM (
SELECT Job,isnull(type, 'nil'),
Isnull(InCredit,0)+isnull(
FROM AceData
)
ps PIVOT(
SUM (SumOfAmount)
FOR type IN([fee],[tax],[nil])
) AS pvt
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
Select Job,[fee],[tax],[nil]
FROM (
SELECT Job,isnull(Type, 'nil') as xtype,
Isnull(InCredit,0)+isnull(
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)
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)
'.
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)
ASKER
does not run - still see errors - I am using sql 8 express
>> does not run - still see errors
What are the errors?
What are the errors?
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)
ASKER
may be the last one - thanks
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
there was a missing bracket in line 15. This should work:
... FOR xtype IN(' + @cols + ')) AS pvt '
... FOR xtype IN(' + @cols + ')) AS pvt '
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
(InCredit+OutDebit) AS SumOfAmount
FROM AceData
WHERE type IS NOT NULL
should work