简体   繁体   中英

Doing a full outer join on two tables to create datatable asp.net c#

I have two tables: Incomings and Expenditures. Incomings has IncDate and IncCost and Expenditures has ExpDate and ExpCost. IncCost and ExpCost are stored as a double and IncDate and ExpDate are stored as a string dd/MM/yyyy (bad practice I know). I am trying to merge IncDate and ExpDate and sum up the costs for each day. I am trying to add this data to a datatable so that I can create a chart with it. The error I am getting says "Incorrect syntax near the keyword 'AS'" although that's all it says so it's not much use. Here is the code I am using to try and create this datatable

DataTable dt = new DataTable();
    public DataTable DatatableNew()
    {

        SqlDataAdapter Adp = new SqlDataAdapter("select CONVERT(DATETIME, IncDate, 103) AS IncDate, SUM(IncCost) AS IncCost, CONVERT(DATETIME, ExpDate, 103) AS ExpDate, SUM(ExpCost) AS ExpCost from Incomings i FULL OUTER JOIN Expenditures e ON i.IncDate = e.ExpDate AS FinalDate GROUP BY CONVERT(DATETIME, FinalDate, 103) ORDER BY CONVERT(DATETIME, FinalDate, 103)", con);
        dt.AcceptChanges();
        Adp.Fill(dt);
        return dt;

    }

Any help is greatly appreciated. Cheers in advance

Error is with your "AS FinalDate" statement (shown in bold in the query below) which you have used in ON clause. Not sure how come that As clause has suddenly erupted in an ON clause. Aliasing of columns (using the AS keyword) is not allowed with ON clause in SQL. Try fixing that to sort out your issue. If FinalDate is computed field then that should come in SELECT clause.

SELECT CONVERT(DATETIME, IncDate, 103) AS IncDate,
SUM(IncCost) AS IncCost, 
CONVERT(DATETIME, ExpDate, 103) AS ExpDate, 
SUM(ExpCost) AS ExpCost 
FROM Incomings i 
FULL OUTER JOIN Expenditures e 
ON i.IncDate = e.ExpDate 

GROUP BY CONVERT(DATETIME, FinalDate, 103) 
ORDER BY CONVERT(DATETIME, FinalDate, 103)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM