GROUP BY将AS / 400日期格式转换为SQL日期

时间:2017-10-20 21:25:26

标签: sql date db2-400

最终我想在AS / 400(绿色屏幕)中的SQL中按月收集一些数据并将其分组。

这是初始SQL查询及其相应的结果:

SELECT ITNBRV, /* Item */
       ODDTRV, OQTYRV /* Order due due, Order quantity */
  FROM ORDREVLA /* MRP Recommendations */
 WHERE ITNBRV = '17000' OR ITNBRV = '19000' /* Returns only items 17000, 19000 */

Query Result Screen Shot

我想接受这些数据并总结如下:

Item number Order Due Date  Order Quantity
   17000         11/17           1296
   17000         12/17           1296
   17000         01/18           3564
   17000         02/18           3888
   19000         11/17          68100
   19000         12/17           1800
   19000         01/18          23220

这是我用来转换日期格式的内容:

  SELECT ITNBRV,
         month(
         substr(ODDTRV,4,2) ||'/'||
         substr(ODDTRV,6,2) ||'/'||
         substr(ODDTRV,2,2)) ||'/'||
         year(
         substr(ODDTRV,4,2) ||'/'||
         substr(ODDTRV,6,2) ||'/'||
         substr(ODDTRV,2,2)) AS Month_Year,
         OQTYRV
    FROM ORDREVLA
   WHERE ITNBRV = '17000' OR ITNBRV = '19000'

具有以下结果:

Converted Dates

我尝试使用具有相同转化数的GROUP BY:

  SELECT ITNBRV,
         month(
         substr(ODDTRV,4,2) ||'/'||
         substr(ODDTRV,6,2) ||'/'||
         substr(ODDTRV,2,2)) ||'/'||
         year(
         substr(ODDTRV,4,2) ||'/'||
         substr(ODDTRV,6,2) ||'/'||
         substr(ODDTRV,2,2)) AS Month_Year,
         OQTYRV
    FROM ORDREVLA
   WHERE ITNBRV = '17000' OR ITNBRV = '19000'
GROUP BY (month(
         substr(ODDTRV,4,2) ||'/'||
         substr(ODDTRV,6,2) ||'/'||
         substr(ODDTRV,2,2)))

但收到此错误消息:

Column ITNBRV or expression in SELECT list not valid.

谢谢!

2 个答案:

答案 0 :(得分:2)

不熟悉AS / 400 SQL,但看起来您需要按INTBRV分组并向ORDREVLA添加聚合。

import groovy.xml.QName
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil

File doc = new File("C:/Temp/letter_.xml")

def prnReq = new XmlSlurper().parse(doc)
prnReq.ltrPrnReqs.ltrPrnReq.each {    

    def encodedString = it.ltrData.toString()

    Base64.Decoder decoder = Base64.getMimeDecoder()
    byte[] decodedByteArray = decoder.decode(encodedString)

    def output = new String(decodedByteArray)

    println output   

    output.splitEachLine(';') { items ->
        println "raSalutation: " + items[0] 
        println "raFromAcc: " + items[1] 
        println "raPayableTo: " + items[2]         
        println "raSortCode: " + items[3] 
        println "raAccNum: " + items[4] 
        println "raReference: " + items[5] 
        println "raSendDate: " + items[6] 
        println "raRecDate: " + items[7] 
        println "raAmount: " + items[8] 
        println "raDummy1: " + items[9]         
        println "raFirstAmt: " + items[10]       
        println "raFirstDate: " + items[11]       
        println "raRegularAmt: " + items[12]       
        println "raRegularDate: " + items[13]       
        println "raFrequency: " + items[14]       
        println "raFee: " + items[15]

        def toAdd = '"<salutation>$item[0]</salutation>"'
        fragToAdd = new XmlSlurper().parseText(toAdd)
        prnReq.ltrPrnReqs.ltrPrnReq.ltrData.appendNode(fragToAdd)

    }

    String outputFileName = "C:/Temp/letter_.xml"

    XmlUtil xmlUtil = new XmlUtil()   
    xmlUtil.serialize(prnReq, new FileWriter(new File(outputFileName)))   

}

答案 1 :(得分:0)

就个人而言,我更喜欢使用公用表表达式(CTE)来处理它。

import "package:flutter/material.dart";

void main(){
  runApp(new MaterialApp(home:new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> with TickerProviderStateMixin{
  final List<MyTabs> _tabs = [new MyTabs(title: "Teal",color: Colors.teal[200]),
  new MyTabs(title: "Orange",color: Colors.orange[200])
  ];
  MyTabs _myHandler ;
  TabController _controller ;
  void initState() {
    super.initState();
    _controller = new TabController(length: 2, vsync: this);
    _myHandler = _tabs[0];
    _controller.addListener(_handleSelected);
  }
  void _handleSelected() {
    setState(() {
       _myHandler= _tabs[_controller.index];
    });
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text(_myHandler.title),
        backgroundColor: _myHandler.color,
        bottom: new TabBar(
            controller: _controller,
            tabs: <Tab>[
              new Tab(text: _tabs[0].title,),
              new Tab(text: _tabs[1].title,)
            ],
      ),),
    );
  }
}

class MyTabs {
  final String title;
  final Color color;
  MyTabs({this.title,this.color});
}