简体   繁体   English

在SQL Server中展平行

[英]Flatten rows in SQL Server

I need to flatten or collapse the rows from either tables before I can join both tables and perform calculation 在我可以连接两个表并执行计算之前,我需要展平或折叠任一表中的行

TABLE - A 表 - A.

StartDate   EndDate        ValueA

2/1/2012    2/1/2012    1

2/2/2012    2/2/2012    2

2/3/2012    2/3/2012    3

2/7/2012    2/8/2012    4

TABLE - B 表 - B

startdate   enddate        ValueB

2/1/2012    2/1/2012    4

2/2/2012    2/3/2012    5

2/7/2012    2/7/2012    6

2/8/2012    2/8/2012    7**

RESULT 结果

StartDate   EndDate        ValueA   ValueB

2/1/2012    2/1/2012    calc    calc

2/2/2012    2/3/2012    calc    calc

2/7/2012    2/8/2012    calc    calc

for a record in table A: if there are multiple records in table B which are within the start and EndDAte of table A, then I need to "flatten" or "collapse" those records in table B before I can join to Table A to perform my calculations 对于表A中的记录:如果表B中有多个记录在表A的start和EndDAte内,那么在我可以加入表A之前,我需要“压平”或“折叠”表B中的那些记录。执行我的计算

Similarly, the same condition can exist the other way, such that table A has multiple records, that fall within Start and EndDate of table B, so in this case I need to flatten those records in table A so that it conforms to Start and End Date of table B. 类似地,相同的条件可以以另一种方式存在,使得表A具有多个记录,这些记录属于表B的Start和EndDate,因此在这种情况下,我需要在表A中展平这些记录,使其符合Start和End表B的日期

I am able to acheive this using multiple cursors but the performance is pathetic, and I am hoping that someone would be able to provide a better solution to this problem 我能够使用多个游标来实现这一点,但性能很可怜,我希望有人能够为这个问题提供更好的解决方案

I hope my question is making sense to you guys 我希望我的问题对你们有意义

Thanks in advance 提前致谢

Give this one a shot (prob. not the most efficient... as I was in a hurry): 给这个人一个机会(概率不是最有效的...因为我匆忙):

drop table tablea
drop table tableb

CREATE TABLE TableA (startdate DATE, enddate DATE, value INT)
CREATE TABLE TableB (startdate DATE, enddate DATE, value INT)

INSERT TableA SELECT '2/1/2012', '2/1/2012', 1
INSERT TableA SELECT '2/2/2012', '2/2/2012', 2
INSERT TableA SELECT '2/3/2012', '2/3/2012', 3
INSERT TableA SELECT '2/7/2012', '2/8/2012', 4


INSERT TableB SELECT '2/1/2012', '2/1/2012', 4
INSERT TableB SELECT '2/2/2012', '2/3/2012', 5
INSERT TableB SELECT '2/7/2012', '2/7/2012', 6
INSERT TableB SELECT '2/8/2012', '2/8/2012', 7


;WITH tablea_cte AS (
  SELECT
    StartDate
    , EndDate
  FROM
    TableA a
  WHERE EXISTS (SELECT * FROM TableB b WHERE b.startdate >= a.startdate and b.enddate <= a.enddate)  
),
tableb_cte as (
  SELECT
    StartDate
    , EndDate
  FROM
    TableB b
  WHERE EXISTS (SELECT * FROM TableA a WHERE a.startdate >= b.startdate and a.enddate <= b.enddate)  
),
tableab_cte AS (
  SELECT * FROM  tableb_cte union select * FROM tablea_cte
),
sumab_cte as (
  SELECT
    ab.startdate
    , ab.enddate
    , calcA = (SELECT SUM (value) FROM TableA a where a.startdate >= ab.startdate and a.enddate <= ab.enddate)
    , calcB = (SELECT SUM (value) FROM TableB b where b.startdate >= ab.startdate and b.enddate <= ab.enddate)
  FROM
    tableab_cte ab
)
select * from sumab_cte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM