如何针对给定的主键在两列内执行少量计算

时间:2019-07-08 17:19:32

标签: sql stored-procedures cursor teradata

根据其他贡献者的要求编辑查询。非常感谢。 对于给定的主键(id_pk),让我们假设以下是值:

 1. id_pk | code_col| amt_col
 2. abc   | 10  | 1000
 3. abc   | 11  | 5
 4. abc   | 12  | 10
 5. abc   | 13  | 20

现在,我需要检查给定主键“ abc”是否在“ code_col”列下存在名为“ 10”的值。 如果满足此条件,则需要执行以下操作:

  1. 对于相同的主键abc,对于code_col值为11,从amt_col值10(即1000)中减去amt_col值(即5)。因此,上表的第二行中的最终值为0。
  2. 对于相同的主键abc,对于code_col值为12,从amt_col值10(即1000)中减去amt_col值(即10)。因此,上表的第三行应更新最终值为0。
  3. 对于相同的主键abc,对于code_col值为13,应从amt_col值10(即1000)中减去amt_col值(即20)。因此,最终值是-865(剩余值)应在上表的第四行/最后一行中更新。

结果应如下所示:

 1. id_pk | col1| col2
 2. abc   | 10  | 0
 3. abc   | 11  | 0
 4. abc   | 12  | 0
 5. abc   | 13  | -965

希望这很清楚。我需要在Teradata中实现此验证(查询/光标/ StoredProcedure /宏/其他任何内容也都受到欢迎。

如果文本显示为粗体,请将其复制粘贴到Notepad ++中,就像我以前在此处张贴的一样。

3 个答案:

答案 0 :(得分:0)

由于这里含糊不清(1000源自何处),(为什么跳过第一条记录?)等...这是一个累积总数的快速刺入,使您陷入困境:

class LiftPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LiftPageState();
  }
}

class _LiftPageState extends State<LiftPage> with TickerProviderStateMixin {
  AnimationController _pageLoadController;
  AnimationController _liftImageController;
  SequenceAnimation _liftPageLoadAnimation;
  SequenceAnimation _liftImageAnimation;
  double _screenWidth = 2000.0;
  double _screenHeight = 5000.0;

  @override
  void initState() {
    super.initState();
    initPlayer();

    _pageLoadController = AnimationController(vsync: this);
    _liftImageController = AnimationController(vsync: this);

    _liftImageAnimation = SequenceAnimationBuilder()
        .addAnimatable(
          animatable: Tween<double>(
            begin: 0,
            end: _screenHeight,
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: _timeForLiftUp),
          tag: "liftGoingUp",
        )
        .animate(_liftImageController);

    _liftPageLoadAnimation = SequenceAnimationBuilder()
        .addAnimatable(
          animatable: ColorTween(
            begin: Color(0xfff665c6),
            end: Color(0xffF599E9),
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: 4),
          tag: "color",
        )
        .addAnimatable(
          animatable: Tween<double>(
            begin: 125,
            end: 0,
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: 2),
          tag: "border",
        )
        .addAnimatable(
          animatable: Tween<double>(
            begin: 100,
            end: _screenHeight,
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: 4),
          tag: "height",
        )
        .addAnimatable(
          animatable: Tween<double>(
            begin: 100,
            end: _screenWidth,
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: 4),
          tag: "width",
        )
        .addAnimatable(
          animatable: ColorTween(
            begin: Colors.white,
            end: Color(0xffF599E9),
          ),
          from: Duration(seconds: 0),
          to: Duration(seconds: 1),
          tag: "iconFade",
        )
        .addAnimatable(
          animatable: ColorTween(
            begin: Color(0x00000000),
            end: Color(0xfff665c6),
          ),
          from: Duration(milliseconds: 2000),
          to: Duration(milliseconds: 2300),
          tag: "circleFadeIn",
        )
        .addAnimatable(
          animatable: ColorTween(
            begin: Color(0x00ffffff),
            end: Color(0xfff665c6),
          ),
          from: Duration(milliseconds: 2500),
          to: Duration(milliseconds: 3500),
          tag: "audioControlsFadeIn",
        )
        .animate(_pageLoadController);
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> childrenStack = <Widget>[
      Scaffold(
        body: AnimatedBuilder(
            animation: _pageLoadController,
            builder: (BuildContext context, Widget child) {
              return Stack(
                children: <Widget>[
                  Center(child: _placeButton()),
                  Center(
                    child: _placeCircle(),
                  ),
                  Align(
                    alignment: Alignment.topCenter,
                    child: _playBackControls(),
                  ),
                ],
              );
            }),
      ),
    ];

    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text('Lift Page'),
        ),
        backgroundColor: Color(0xfff665c6),
      ),
      body: Center(child: Stack(children: childrenStack)),
    ));
  }

  GestureDetector _placeCircle() {
    return GestureDetector(
      onTap: () {
        final _status = _pageLoadController.status;
        if (_status == AnimationStatus.completed) {
          _pageLoadController.reverse();
          _liftPage_liftButtonTapped();
        } else {
          _pageLoadController.forward();
          _liftPage_liftButtonTapped();
        }
      },
      child: Container(
        height: 100,
        width: 100,
        decoration: BoxDecoration(
          color: _liftPageLoadAnimation["circleFadeIn"].value,
          borderRadius: BorderRadius.circular(125.0),
        ),
      ),
    );
  }

答案 1 :(得分:0)

这似乎符合您的要求:

SELECT id_pk, code_col
  ,CASE
     WHEN code_col = Max(code_col) Over (PARTITION BY id_pk) -- "last" row?
     THEN -- negative code 10 amount
       Max(CASE WHEN code_col = 10 THEN -amt_col*2 end) Over (PARTITION BY id_pk)
          --  plus the sum of all other amounts
       + Sum(amt_col) Over (PARTITION BY id_pk)
     ELSE 0
   END
FROM tab
QUALIFY -- only ids with a code 10
   Max(CASE WHEN code_col = 10 THEN code_col end) Over (PARTITION BY id_pk) = 10

答案 2 :(得分:0)

这是可以转换为Teradata语法的常规逻辑。

SELECT id_pk,
code_col col1,
CASE 
    WHEN code_col = 13 THEN (Sum_10 - Sum_Other)
    ELSE 0
END col2
FROM your_table A
LEFT JOIN
(
    SELECT id_pk, 
    SUM(CASE WHEN code_col = 10 THEN amt_col ELSE NULL END) Sum_10,
    SUM(CASE WHEN code_col <> 10 THEN amt_col ELSE NULL END) Sum_Other 
    FROM your_table
    GROUP BY id_pk
)B ON A.id_pk = B.id_pk

或者,您也可以在下面的逻辑中进行尝试-

SELECT 
A.id_pk,
A.code_col col1,
CASE 
    WHEN A.code_col = 13 THEN
        (SELECT amt_col FROM your_table WHERE code_col = 10 AND id_pk = A.id_pk)
        -
        (SELECT SUM(amt_col) FROM your_table WHERE code_col <> 10 AND id_pk = A.id_pk)
    ELSE 0
END col2
FROM your_table A