Skip to content
Advertisement

How to find the sum from value of sql column

I have two tables with following data.

studenttbl:

feetbl:

I need to find below data, but I am not getting balance value of seatno 1006:

I am using this SQL query:

Please help me with this.

Advertisement

Answer

This issue is due to INNER JOIN versus LEFT JOIN. When you INNER JOIN youll only get matches between two tables. When you LEFT JOIN, you will get all values from the left table and only matches form the right side.

Also, with this issue, your SUM function will break. You need to wrap the LEFT JOIN’d table’s column in ISNULL(col,0) so youre aggregates work correctly.

I also changed your date comparison in the WHERE clause to use <= and >=. Aaron Bertrandt goes over this topic a few times, here is a good link:

https://www.mssqltips.com/sqlservertutorial/9316/sql-server-between-dates-issue/

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement