Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with severalData
- as
- (
- select distinct point, date
- from Income
- union
- select distinct point, date
- from Outcome
- ),
- onceData as
- (
- select distinct point, date
- from Income_o
- union
- select distinct point, date
- from Outcome_o
- ),
- ignorePointDate as
- (
- select distinct CAST(lt.point AS NVARCHAR(255)) + CAST(lt.date AS NVARCHAR(255)) as pointDateId
- from onceData lt
- join severalData rt on lt.point = rt.point and lt.date = rt.date
- ),
- correctIncO as
- (
- select point, date, sum(inc) as inc
- from Income_o
- where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
- (select * from ignorePointDate)
- group by point, date
- ),
- correctOutO as
- (
- select point, date, sum(out) as out
- from Outcome_o
- where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
- (select * from ignorePointDate)
- group by point, date
- ),
- correctInc as
- (
- select point, date, sum(inc) as inc
- from Income
- where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
- (select * from ignorePointDate)
- group by point, date
- ),
- correctOut as
- (
- select point, date, sum(out) as out
- from Outcome
- where CAST(point AS NVARCHAR(255)) + CAST(date AS NVARCHAR(255)) not in
- (select * from ignorePointDate)
- group by point, date
- ),
- finalInc as
- (
- select point, date, sum(inc) as inc, tag
- from
- (
- select *, 'once' as tag from correctIncO union select *, 'several' as tag from correctInc
- ) t
- group by point, date, tag
- ),
- finalOut as
- (
- select point, date, sum(out) as out, tag
- from
- (
- select *, 'once' as tag from correctOutO union select *, 'several' as tag from correctOut
- ) t
- group by point, date, tag
- )
- select COALESCE(lt.point, rt.point),
- COALESCE(lt.date, rt.date),
- COALESCE(lt.inc, 0),
- COALESCE(rt.out, 0),
- COALESCE(lt.tag, rt.tag)
- from finalInc lt
- full join finalOut rt on lt.point = rt.point and lt.date = rt.date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement