Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4615244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:48:44+00:00 2026-05-22T01:48:44+00:00

I want my SQL query can run like this. I have two tables: Key1,

  • 0

I want my SQL query can run like this.

enter image description here

I have two tables:

  1. Key1, Date, In
  2. key2, Date, Out

Until now I achieved this with a UNION:

select Date , In , '' as 'out'
from table1

Union ALL

select Date, '' as In, Out
from table2

How about the balance ?

Please help me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:48:44+00:00Added an answer on May 22, 2026 at 1:48 am

    At the present time, the fastest and practically only, way to calculate running totals in SQL Server is to use a cursor.

    Declare @RunningTotals Table    
        (
        PrimaryKeyCol int not null Primary Key
        , TableName nvarchar(128) not null
        , Total money not null Default ( 0 )
        )
    
    Declare @Values Cursor
    Declare @PrimaryKeyCol int  
    Declare @TableName nvarchar(128)
    Declare @Date datetime
    Declare @In money
    Declare @Out money
    Set @Values = Cursor Fast_Forward For
            Select Key1, 'Table1' As TableName, Date , In , Null as out
            From table1
            Union All
            Select Key2, 'Table2', Date, Null as In, Out
            From Table2     
            Order By Date
    
    Open @Values
    Fetch Next From @Values Into @PrimaryKeyCol, @TableName, @In, @Out
    
    Set @Total = 0
    While @@Fetch_Status = 0
    Begin
        Set @Total = @Total + Coalesce(@In,0) - Coalesce(@Out,0)
    
        Insert @RunningTotals( PrimaryKeyCol, TableName, Total ) 
        Values( @PrimaryKeyCol, @TableName, @Total )
    
        Fetch Next From @Values Into @PrimaryKeyCol, @TableName, @In, @Out
    End
    
    Close @Values
    Deallocate @Values
    
    Select Date, In, Out, RT.Total
    From (
            Select Key1 As PrimaryKeyCol, 'Table1' As TableName, Date , In , Null as out
            From table1
            Union All
            Select Key2, 'Table2', Date, Null as In, Out
            From Table2     
            ) As Z
        Join @RunningTotals As RT
            On RT.PrimaryKeyCol = T.PrimaryKeyCol
                And RT.TableName = Z.TableName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard that Linq query write once can be run on SQL and
I want to execute an SQL query like: select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2'; Now the
I want to find a sql command or something that can do this where
Hey.. I've run into a problem using several like in my sql-query (generated with
If all tables I want to delete from have the column gamer_id can i
I have a sql query in MySQL and I want an expression that matches
How can I run MSSQL .sql files with php? I have a collection of
I want to find an SQL query to find rows where field1 does not
I want to be able to pass something into an SQL query to determine
I have two tables. Widgets, which has information about each widget (Color, size, etc);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.