Donnerstag, April 20, 2017

Oracle SQL | Group by Date

Das folgende Oracle-SQL gruppiert die fehlgeschlagenen Datensätze aus der Tabelle 'tabelle_mit_datum' nach Jahr, Monat und Tag:
SELECT
    COUNT(*) AnzahlFehler,
    TO_CHAR(cs.datum, 'YYYY') Jahr,
    TO_CHAR(cs.datum, 'MM') Monat,
    TO_CHAR(cs.datum, 'DD') Tag,
FROM
    tabelle_mit_datum tmd
WHERE
    tmd.status = 'Fehler'
    AND tmd.datum BETWEEN
        TO_DATE('01.01.2017', 'DD.MM.YYYY')
        AND TO_DATE('30.04.2017', 'DD.MM.YYYY')
GROUP BY
    TO_CHAR(cs.datum, 'YYYY') Jahr,
    TO_CHAR(cs.datum, 'MM') Monat,
    TO_CHAR(cs.datum, 'DD') Tag,
ORDER BY
    TO_CHAR(cs.datum, 'YYYY') Jahr DESC,
    TO_CHAR(cs.datum, 'MM') Monat DESC,
    TO_CHAR(cs.datum, 'DD') Tag DESC
;
Oracles TO_CHAR(date, dateformat) Funktion kann einige tolle Sachen. Der folgende Ausdruck liefert die Woche des Jahres zurück:
SELECT TO_CHAR(sysdate, 'IW') FROM dual;
Folgende Formate stehen als Parameter zur Verfügung:
YEARYear, spelled out
YYYY4-digit year
YYYLast 3 digits of year.
YYLast 2 digits of year.
YLast digit of year.
IYYLast 3 digits of ISO year.
IYLast 2 digits of ISO year.
ILast 1 digit of ISO year.
IYYY4-digit year based on the ISO standard
QQuarter of year (1, 2, 3, 4; JAN-MAR = 1).
MMMonth (01-12; JAN = 01).
MONAbbreviated name of month.
MONTHName of month, padded with blanks to length of 9 characters.
RMRoman numeral month (I-XII; JAN = I).
WWWeek of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
WWeek of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IWWeek of year (1-52 or 1-53) based on the ISO standard.
DDay of week (1-7).
DAYName of day.
DDDay of month (1-31).
DDDDay of year (1-366).
DYAbbreviated name of day.
JJulian day; the number of days since January 1, 4712 BC.
HHHour of day (1-12).
HH12Hour of day (1-12).
HH24Hour of day (0-23).
MIMinute (0-59).
SSSecond (0-59).
SSSSSSeconds past midnight (0-86399).
FFFractional seconds.

Keine Kommentare:

Kommentar veröffentlichen

AssertJ und java.util.List

AssertJ hat eine praktische Möglichkeit, Listen in JUnit Tests abzuprüfen. Insbesondere, wenn in der Liste komplexe Objekte abgelegt sind, s...