简体   繁体   中英

SQL add calculated column to table

I am not fluent in SQL, but I am using Microsoft Query to create a new table from an old table and add a new column (a calculated column).

My calculation queries from the same table. I essentially want to retrieve a dollar figure from the same date one year ago and provide a calculated field that gives year-over-year growth by account. Each account will have several columns of unique data except for the date.

I am thinking that some sort of query stating that columns x, y and z match, but also including the date column -364.

If you can help, I'd be more than greatful.

Thanks, Mike

Assuming that you have your accountTotals in the same table, you could join that table to itself and specify different dates in your WHERE clause. The query should look something like this:

SELECT a.accountingDate, a.accountTotal,
b.accountingDate, b.accountTotal, (b.accountTotal - a.accountTotal) as ChangeInTotal
FROM sourceTbl a
JOIN sourceTbl b on a.x = b.x and a.y = b.y and a.z = b.z
WHERE a.accountingDate = <current year date>
and b.accountingDate = <prior year date>

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