简体   繁体   中英

T-SQL Value from 1st select on each row in 2nd select of a union all

Very simplified, my sql looks like this:

;with RawData as (
select from table1

union all

select from table2)

select * from rawdata

In the first select I get the On Hand quantity. In the 2nd select that column is zero because I'm getting what's on order. I want the On Hand quantity to repeat on each row. So instead of

8
0
0
0

I would have

8
8
8
8

How do I do that? Thanks!

You can try sum() over() as below:

;with RawData as (
...
)
select sum(yourcolumn) over() from RawData

If you have specific id column then you can use

select sum(yourcolumn) over(partition by id) from RawData 

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