Problems with Joining Pivot Table - sql

I am trying to join two tables one containing Dept codes another a pivot table but each time I try to join the table it throws me the following error:
An expression of non-boolean type specified in a context where a condition is expected, near 'D'. Is there a better way of doing this or have I overlooked an error. ?
I've tried various different joins but it all throws me the same error
SELECT D.Dept_Code, D.Department, S.Amount AS 'Total Amount' FROM DEPARTMENT AS D JOIN(
SELECT DEPT, (ISNULL([63121],0) +ISNULL([63122],0)+ ISNULL([63123],0)+ISNULL([63124],0)+ISNULL([63125],0)+ISNULL([63126],0)+ISNULL([63129],0)) AS 'Amount', (ISNULL([63131],0) + ISNULL([63139], 0)) AS '63130', ISNULL([63141], 0) AS '63141', ISNULL([63143],0) AS '63143', ISNULL([63144], 0) AS '63144', ISNULL([63145],0) AS '63145', ISNULL([63146],0) AS '63146', ISNULL([63149], 0) AS '632149'
FROM(SELECT DEPT, NATURE, SUM(CAST(Amount AS FLOAT)) AS AMOUNT FROM MFG_EXP_1 GROUP BY DEPT, NATURE) AS S
PIVOT
(
SUM(AMOUNT)
FOR NATURE IN ([63121], [63122], [63123], [63124], [63125], [63126], [63129], [63131], [63139], [63141], [63142], [63143], [63144], [63145], [63146], [63149])
) NATURE_CODE_BREAKDOWN) AS G ON D D.Dept = G.DEPT;
I try to join the table it throws me the following error:
An expression of non-boolean type specified in a context where a condition is expected, near 'D'
The Department table:
Dept_Code Dept Department
100 110 merchandsing
100 120 operations
the MFG_EXP 1 Table
Dept Nature Amount
110 1000 $200
120 2000 $300
`````````````````
When I Pivot the MFG_EXP_1 Table by the Nature codes I have the newly formed SourceTable Below:
`````````````````
Dept 1000 2000
110 $200.00 $0.00
120 $0.00 $300.00
I now want to join my SourceTable with my Department Table to have This as my final result
Dept Department 1000 2000
100 merchandise $200.00 $0.00
100 Operations $0.00 $300.00

I see a syntax problem at the end of your SQL statement:
D D.Dept = G.DEPT;
But in general if you use CTE and put your pivot table as CTE expression, you can simplify your SQL statement and easily test each separately.
Like this:
;USING myPivot AS (
-- just copied your statement here
SELECT DEPT, (ISNULL([63121],0) +ISNULL([63122],0)+ ISNULL([63123],0)+ISNULL([63124],0)+ISNULL([63125],0)+ISNULL([63126],0)+ISNULL([63129],0)) AS 'Amount', (ISNULL([63131],0) + ISNULL([63139], 0)) AS '63130', ISNULL([63141], 0) AS '63141', ISNULL([63143],0) AS '63143', ISNULL([63144], 0) AS '63144', ISNULL([63145],0) AS '63145', ISNULL([63146],0) AS '63146', ISNULL([63149], 0) AS '632149'
FROM(SELECT DEPT, NATURE, SUM(CAST(Amount AS FLOAT)) AS AMOUNT FROM MFG_EXP_1 GROUP BY DEPT, NATURE) AS S
PIVOT
(
SUM(AMOUNT)
FOR NATURE IN ([63121], [63122], [63123], [63124], [63125], [63126], [63129], [63131], [63139], [63141], [63142], [63143], [63144], [63145], [63146], [63149])
)
)
SELECT D.Dept_Code, D.Department, S.Amount AS 'Total Amount' FROM DEPARTMENT AS D
JOIN myPivot D ON D.Dept = G.DEPT

Here's a stab at fixing your query but not having anything to test it against it might still have some problem:
SELECT
D.Dept_Code,
D.Department,
S.Amount AS TotalAmount
FROM
DEPARTMENT AS D
INNER JOIN
(
SELECT
DEPT,
(ISNULL([63121],0) +ISNULL([63122],0)+ ISNULL([63123],0)+ISNULL([63124],0)+ISNULL([63125],0)+ISNULL([63126],0)+ISNULL([63129],0)) AS [Amount],
(ISNULL([63131],0) + ISNULL([63139], 0)) AS [63130],
ISNULL([63141], 0) AS [63141],
ISNULL([63143],0) AS [63143],
ISNULL([63144], 0) AS [63144],
ISNULL([63145],0) AS [63145],
ISNULL([63146],0) AS [63146],
ISNULL([63149], 0) AS [632149]
FROM
(
SELECT DEPT, NATURE, SUM(CAST(Amount AS FLOAT)) AS AMOUNT
FROM MFG_EXP_1
GROUP BY DEPT, NATURE
) AS S
PIVOT
(
SUM(AMOUNT)
FOR NATURE IN ([63121], [63122], [63123], [63124], [63125], [63126], [63129], [63131], [63139], [63141], [63142], [63143], [63144], [63145], [63146], [63149])
) NATURE_CODE_BREAKDOWN
) AS G ON D.Dept = G.DEPT;

Related

Do a Sum and a Difference

I hope someone could guide in the correct path . It's my first class in SQL.
SELECT distinct
a.LICENSEID,
a.license,
a.business_name,
a,year
a.TOTAL_AMOUNT_PAID,
SUM(e.COMPUTED_AMOUNT) over (partition by e.LICENSEID) as AMOUNT_OWNED,
FROM vw_business AS a
INNER JOIN vw_fees AS e ON e.LICENSEID = a.LICENSEID
WHERE LICENSE = '1000'
AND(e.STATUS='BILLED' OR e.STATUS='PAID')
This will give me a result like this:
LICENSEID LICENSE BUSINESS_NAME YEAR TOTAL_AMOUT_PAID AMOUNT_OWNED
1CA6918B 1000 CORTANA 2016 0.00 1000.00
EE6DBDD0 1000 CORTANA 2017 1000.00 1000.00
Basically, I want to add another column to calculate the Total Balance which should be the difference between AMOUNT_OWNED and TOTAL_AMOUNT_PAID. I tried adding another line after SUM like this:
(AMOUNT_OWNED - TOTAL_AMOUNT_PAID) AS TOTAL_BALANCED,
However, I get an error that doesn't recognized the TOTAL_BALANCED. I also tried adding the entire line of the SUM again with no luck.
Can you guys guide in the correct path? If this is possible. Thank you.
Alias names cannot be referred in same select query. You need to write the sum over() window aggregate again to find difference
Try this way
SELECT DISTINCT a.LICENSEID,
a.license,
a.business_name,
a.year, -- Here it is should be . instead of ,
a.TOTAL_AMOUNT_PAID,
Sum(e.COMPUTED_AMOUNT)OVER (partition BY e.LICENSEID) AS AMOUNT_OWNED,
a.TOTAL_AMOUNT_PAID - Sum(e.COMPUTED_AMOUNT)
OVER (
partition BY e.LICENSEID) AS TOTAL_BALANCED
FROM vw_business AS a
INNER JOIN vw_fees AS e
ON e.LICENSEID = a.LICENSEID
WHERE LICENSE = '1000'
AND e.STATUS IN ( 'BILLED', 'PAID' ) -- use IN clause
or use derived table, this is a better option when the expression is big. Query will be more readable
SELECT LICENSEID,
license,
business_name,
year,
TOTAL_AMOUNT_PAID,
AMOUNT_OWNED,
TOTAL_AMOUNT_PAID - AMOUNT_OWNED as TOTAL_BALANCED
FROM (SELECT DISTINCT a.LICENSEID,
a.license,
a.business_name,
a.year,-- Here it is should be . instead of ,
a.TOTAL_AMOUNT_PAID,
Sum(e.COMPUTED_AMOUNT)OVER (partition BY e.LICENSEID) AS AMOUNT_OWNED
FROM vw_business AS a
INNER JOIN vw_fees AS e
ON e.LICENSEID = a.LICENSEID
WHERE LICENSE = '1000'
AND e.STATUS IN ( 'BILLED', 'PAID' ) -- use IN clause
) a

How to keep a bucket using case statement even if the count for items in that bucket is 0?

Here's the data table named "Salary_table" that i've created for this question:
So I want to find the number of employees in each salary bucket in each department. the buckets are
"<$100" "$100-$200" and ">$200"
The desired output is:
Below is my code for achieving this task:
select distinct(st.department) as "Department",
sb.salary_bucket as "salary range", count(*)
from Salary_table st
Left join (
select department, employee, case
when salary < 100 then "<$100"
when salary between 100 and 200 then "$100-$200"
else ">$200"
end
as salary_bucket
from Salary_table
) sb
on sb.employee = st.employee
group by st.department, sb.salary_bucket
order by st.department, sb.salary_bucket
;
but my output is a bit short of what im expecting:
There are TWO problems with my current output:
The buckets with 0 employees earning the salary in the bucket range are not listed; I want it to be listed with a value "0"
The salary bucket is NOT in the right order, even though I added in the statement "order by" but I think it's b/c its texts so can't really do that.
I would really appreciate some hints and pointers on how to fix/achieve these two issues I've addressed above. Thank you so much!
what i've tried
I tried use "left join" but output came out the same
I tried adding the "order by" clause but doesnt seem to work on text buckets
You are sort of on the right track, but the idea is a bit more complicated. Use a cross join to get all the rows -- the buckets and departments. Then use left join to bring in the matching information and finally group by for the aggregation:
select d.department, b.salary_bucket,
count(sb.department) as cnt
from (select '<$100' as salary_bucket union all
select '$100-$200' union all
select '>$200'
) b cross join
(select distinct department from salary_table
) d left join
(select department, employee,
(case when salary < 100 then '<$100'
when salary between 100 and 200 then '$100-$200'
else '>$200'
end) as salary_bucket
from Salary_table
) sb
on sb.department = d.department and
sb.salary_bucket = b.salary_bucket
group by d.department, b.salary_bucket;

Find duplicates in MS SQL table

I know that this question has been asked several times but I still cannot figure out why my query is returning values which are not duplicates. I want my query to return only the records which have identical value in the column Credit. The query executes without any errors but values which are not duplicated are also being returned. This is my query:
Select
_bvGLTransactionsFull.AccountDesc,
_bvGLAccountsFinancial.Description,
_bvGLTransactionsFull.TxDate,
_bvGLTransactionsFull.Description,
_bvGLTransactionsFull.Credit,
_bvGLTransactionsFull.Reference,
_bvGLTransactionsFull.UserName
From
_bvGLAccountsFinancial Inner Join
_bvGLTransactionsFull On _bvGLAccountsFinancial.AccountLink =
_bvGLTransactionsFull.AccountLink
Where
_bvGLTransactionsFull.Credit
IN
(SELECT Credit AS NumOccurrences
FROM _bvGLTransactionsFull
GROUP BY Credit
HAVING (COUNT(Credit) > 1 ) )
Group By
_bvGLTransactionsFull.AccountDesc, _bvGLAccountsFinancial.Description,
_bvGLTransactionsFull.TxDate, _bvGLTransactionsFull.Description,
_bvGLTransactionsFull.Credit, _bvGLTransactionsFull.Reference,
_bvGLTransactionsFull.UserName, _bvGLAccountsFinancial.Master_Sub_Account,
IsNumeric(_bvGLTransactionsFull.Reference), _bvGLTransactionsFull.TrCode
Having
_bvGLTransactionsFull.TxDate > 01 / 11 / 2014 And
_bvGLTransactionsFull.Reference Like '5_____' And
_bvGLTransactionsFull.Credit > 0.01 And
_bvGLAccountsFinancial.Master_Sub_Account = '90210'
That's because you're matching on the credit field back to your table, which contains duplicates. You need to isolate the rows that are duplicated with ROW_NUMBER:
;WITH CTE AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY CREDIT ORDER BY (SELECT NULL)) AS RN
FROM _bvGLTransactionsFull)
Select
CTE.AccountDesc,
_bvGLAccountsFinancial.Description,
CTE.TxDate,
CTE.Description,
CTE.Credit,
CTE.Reference,
CTE.UserName
From
_bvGLAccountsFinancial Inner Join
CTE On _bvGLAccountsFinancial.AccountLink = CTE.AccountLink
WHERE CTE.RN > 1
Group By
CTE.AccountDesc, _bvGLAccountsFinancial.Description,
CTE.TxDate, CTE.Description,
CTE.Credit, CTE.Reference,
CTE.UserName, _bvGLAccountsFinancial.Master_Sub_Account,
IsNumeric(CTE.Reference), CTE.TrCode
Having
CTE.TxDate > 01 / 11 / 2014 And
CTE.Reference Like '5_____' And
CTE.Credit > 0.01 And
_bvGLAccountsFinancial.Master_Sub_Account = '90210'
Just as a side note, I would consider using aliases to shorten your queries and make them more readable. Prefixing the table name before each column in a join is very difficult to read.
I trust your code in terms of extracting all data per your criteria. With this, let me have a different approach and see your script "as-is". So then, lets keep first all the records in a temp.
Select
_bvGLTransactionsFull.AccountDesc,
_bvGLAccountsFinancial.Description,
_bvGLTransactionsFull.TxDate,
_bvGLTransactionsFull.Description,
_bvGLTransactionsFull.Credit,
_bvGLTransactionsFull.Reference,
_bvGLTransactionsFull.UserName
-- temp table
INTO #tmpTable
From
_bvGLAccountsFinancial Inner Join
_bvGLTransactionsFull On _bvGLAccountsFinancial.AccountLink =
_bvGLTransactionsFull.AccountLink
Where
_bvGLTransactionsFull.Credit
IN
(SELECT Credit AS NumOccurrences
FROM _bvGLTransactionsFull
GROUP BY Credit
HAVING (COUNT(Credit) > 1 ) )
Group By
_bvGLTransactionsFull.AccountDesc, _bvGLAccountsFinancial.Description,
_bvGLTransactionsFull.TxDate, _bvGLTransactionsFull.Description,
_bvGLTransactionsFull.Credit, _bvGLTransactionsFull.Reference,
_bvGLTransactionsFull.UserName, _bvGLAccountsFinancial.Master_Sub_Account,
IsNumeric(_bvGLTransactionsFull.Reference), _bvGLTransactionsFull.TrCode
Having
_bvGLTransactionsFull.TxDate > 01 / 11 / 2014 And
_bvGLTransactionsFull.Reference Like '5_____' And
_bvGLTransactionsFull.Credit > 0.01 And
_bvGLAccountsFinancial.Master_Sub_Account = '90210'
Then remove the "single occurrence" data by creating a row index and remove all those 1 time indexes.
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY Credit ORDER BY Credit) AS rowIdx
, *
FROM #tmpTable) AS innerTmp
WHERE
rowIdx != 1
You can change your preference through PARTITION BY <column name>.
Should you have any concerns, please raise it first as these are so far how I understood your case.
EDIT : To include those credits that has duplicates.
SELECT
tmp1.*
FROM #tmpTable tmp1
RIGHT JOIN (
SELECT
Credit
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY Credit ORDER BY Credit) AS rowIdx
, *
FROM #tmpTable) AS innerTmp
WHERE
rowIdx != 1
) AS tmp2
ON tmp1.Credit = tmp2.Credit

SQL: multiple counts from same table

I am having a real problem trying to get a query with the data I need. I have tried a few methods without success. I can get the data with 4 separate queries, just can't get hem into 1 query. All data comes from 1 table. I will list as much info as I can.
My data looks like this. I have a customerID and 3 columns that record who has worked on the record for that customer as well as the assigned acct manager
RecID_Customer___CreatedBy____LastUser____AcctMan
1-------1374----------Bob Jones--------Mary Willis------Bob Jones
2-------1375----------Mary Willis------Bob Jones--------Bob Jones
3-------1376----------Jay Scott--------Mary Willis-------Mary Willis
4-------1377----------Jay Scott--------Mary Willis------Jay Scott
5-------1378----------Bob Jones--------Jay Scott--------Jay Scott
I want the query to return the following data. See below for a description of how each is obtained.
Employee___Created__Modified__Mod Own__Created Own
Bob Jones--------2-----------1---------------1----------------1
Mary Willis------1-----------2---------------1----------------0
Jay Scott--------2-----------1---------------1----------------1
Created = Counts the number of records created by each Employee
Modified = Number of records where the Employee is listed as Last User
(except where they created the record)
Mod Own = Number of records for each where the LastUser = Acctman
(account manager)
Created Own = Number of Records created by the employee where they are
the account manager for that customer
I can get each of these from a query, just need to somehow combine them:
Select CreatedBy, COUNT(CreatedBy) as Created
FROM [dbo].[Cust_REc] GROUP By CreatedBy
Select LastUser, COUNT(LastUser) as Modified
FROM [dbo].[Cust_REc] Where LastUser != CreatedBy GROUP By LastUser
Select AcctMan, COUNT(AcctMan) as CreatePort
FROM [dbo].[Cust_REc] Where AcctMan = CreatedBy GROUP By AcctMan
Select AcctMan, COUNT(AcctMan) as ModPort
FROM [dbo].[Cust_REc] Where AcctMan = LastUser AND NOT AcctMan = CreatedBy GROUP By AcctMan
Can someone see a way to do this? I may have to join the table to itself, but my attempts have not given me the correct data.
The following will give you the results you're looking for.
select
e.employee,
create_count=(select count(*) from customers c where c.createdby=e.employee),
mod_count=(select count(*) from customers c where c.lastmodifiedby=e.employee),
create_own_count=(select count(*) from customers c where c.createdby=e.employee and c.acctman=e.employee),
mod_own_count=(select count(*) from customers c where c.lastmodifiedby=e.employee and c.acctman=e.employee)
from (
select employee=createdby from customers
union
select employee=lastmodifiedby from customers
union
select employee=acctman from customers
) e
Note: there are other approaches that are more efficient than this but potentially far more complex as well. Specifically, I would bet there is a master Employee table somewhere that would prevent you from having to do the inline view just to get the list of names.
this seems pretty straight forward. Try this:
select a.employee,b.created,c.modified ....
from (select distinct created_by from data) as a
inner join
(select created_by,count(*) as created from data group by created_by) as b
on a.employee = b.created_by)
inner join ....
This highly inefficient query may be a rough start to what you are looking for. Once you validate the data then there are things you can do to tidy it up and make it more efficient.
Also, I don't think you need the DISTINCT on the UNION part because the UNION will return DISTINCT values unless UNION ALL is specified.
SELECT
Employees.EmployeeID,
Created =(SELECT COUNT(*) FROM Cust_REc WHERE Cust_REc.CreatedBy=Employees.EmployeeID),
Mopdified =(SELECT COUNT(*) FROM Cust_REc WHERE Cust_REc.LastUser=Employees.EmployeeID AND Cust_REc.CreateBy<>Employees.EmployeeID),
ModOwn =
CASE WHEN NOT Empoyees.IsManager THEN NULL ELSE
(SELECT COUNT(*) FROM Cust_REc WHERE AcctMan=Employees.EmployeeID)
END,
CreatedOwn=(SELECT COUNT(*) FROM Cust_REc WHERE AcctMan=Employees.EmployeeID AND CReatedBy=Employees.EMployeeID)
FROM
(
SELECT
EmployeeID,
IsManager=CASE WHEN EXISTS(SELECT AcctMan FROM CustRec WHERE AcctMan=EmployeeID)
FROM
(
SELECT DISTINCT
EmployeeID
FROM
(
SELECT EmployeeID=CreatedBy FROM Cust_Rec
UNION
SELECT EmployeeID=LastUser FROM Cust_Rec
UNION
SELECT EmployeeID=AcctMan FROM Cust_Rec
)AS Z
)AS Y
)
AS Employees
I had the same issue with the Modified column. All the other columns worked okay. DCR example would work well with the join on an employees table if you have it.
SELECT CreatedBy AS [Employee],
COUNT(CreatedBy) AS [Created],
--Couldn't get modified to pull the right results
SUM(CASE WHEN LastUser = AcctMan THEN 1 ELSE 0 END) [Mod Own],
SUM(CASE WHEN CreatedBy = AcctMan THEN 1 ELSE 0 END) [Created Own]
FROM Cust_Rec
GROUP BY CreatedBy

SQL 2008 VS 2012 Error: Incorrect syntax near the keyword 'COMPUTE'

My friend sent me the commands that he wrote in server 2008 and they worked with no problems, mine however from a copy and past did not work with 2012. Is there any reason why? Here is the code:
Use Kudler_Database
SELECT AccountNumber, [Description], ShortDescription,Balance
FROM Chart_of_Accounts
ORDER BY left (AccountNumber, 2)
COMPUTE SUM(Balance) BY left (AccountNumber, 2)
COMPUTE SUM(Balance);
Here is the error :
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword
'COMPUTE'.
COMPUTE is no longer available in SQL server 2012, thats why you are getting that error. See this page:
Discontinued Database Engine Functionality in SQL Server 2012
It said that:
This topic describes the Database Engine features that are no longer
available in SQL Server 2012:
*Transact-SQL syntax | COMPUTE / COMPUTE BY *
A kind of hack with RollUp since Compute By is deprecated in SQL Server 2012 - (see "SQL SERVER – Use ROLL UP Clause instead of COMPUTE BY")
DECLARE #t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO #t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000
SELECT
AccountNumber
,Balance
,Total = SUM(Balance)
FROM #t
GROUP BY AccountNumber,Balance
WITH ROLLUP
Result
AccountNumber Balance total
1234567890 2000 2000
1234567890 4000 4000
1234567890 NULL 6000
2345678901 3000 3000
2345678901 NULL 3000
NULL NULL 9000
OR
you can use the below
DECLARE #t TABLE(AccountNumber VARCHAR(10),[Description] VARCHAR(100),ShortDescription VARCHAR(100),Balance INT)
INSERT INTO #t SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',2000 Union All
SELECT '2345678901','Some Description for 2nd Account','Short Description for 2nd Account',3000 Union All
SELECT '1234567890','Some Description for 1st Account','Short Description for 1st Account',4000
;With CTE AS
(
SELECT
AccountNumber
,[Description]
,ShortDescription
,Balance
,SubTotal = SUM(Balance) OVER (PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
,Rn = ROW_NUMBER() OVER(PARTITION BY AccountNumber ORDER BY LEFT (AccountNumber, 2))
FROM #t)
SELECT
AccountNumber
,[Description]
,ShortDescription
,Balance = CAST(Balance AS VARCHAR(10))
,SubTotal = CASE WHEN Rn != 1 THEN NULL ELSE SubTotal END
FROM CTE
UNION ALL
SELECT
' ', ' ',' ' ,'Total Amount' , SUM(Balance) FROM CTE
The output being
AccountNumber Description ShortDescription Balance SubTotal
1234567890 Some Description for 1st Account Short Description for 1st Account 2000 6000
1234567890 Some Description for 1st Account Short Description for 1st Account 4000 NULL
2345678901 Some Description for 2nd Account Short Description for 2nd Account 3000 3000
Total Amount 9000
You can create something similar with GROUPING SETS but it all comes in one resultset, eg something like:
SELECT AcGroup, AccountNumber, [Description], ShortDescription, SUM( Balance ) Balance, GROUPING_ID(AcGroup, AccountNumber)
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY GROUPING SETS( (AcGroup), ( AccountNumber, [Description], ShortDescription ), () )
SELECT AcGroup, SUM( Balance ) Balance
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY GROUPING SETS( AcGroup, () )
SELECT AcGroup, SUM( Balance ) Balance
FROM ( SELECT LEFT( AccountNumber, 2 ) AcGroup, * FROM Chart_of_Accounts ) x
GROUP BY AcGroup WITH CUBE
I've added GROUPING_ID() which makes it easier to work out if the source is an original, summary to total row.
I always wondered how you would consume something like that as the multiple resultsets make it difficult to handle. You can't pass it to another stored procedure, you can't copy it paste it directly to Excel (without messing around), passing to a .net client would be awkward. How did you consume the previous code?
HTH