Skip to Content
author's profile photo Former Member
Former Member

Order by in Union all

Hello,

How to use two columns in order by while using UNION ALL in a query in SBO HANA database

I am trying to get the list of all sales made in the company.

Please check and revert with the query. thanks in advance

Regards,

JK Prabhu

SELECT 'Inv' as "Type", YEAR(T0."DocDate"), T1."Dscription" "Chemical", T0."CardName" "Customer", SUM(T1."Quantity") "Quantity", T1."unitMsr", SUM(T1."LineTotal") "Value" FROM OINV T0 INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OCRD T2 ON T0."CardCode" = T2."CardCode" WHERE T0."CANCELED" = 'N' GROUP BY YEAR(T0."DocDate"), T1."Dscription", T0."CardName", T1."unitMsr"

UNION ALL

SELECT 'CN' "Type", YEAR(T0."DocDate"), T1."Dscription" "Chemical", T0."CardName" "Customer", SUM(T1."Quantity") "Quantity", T1."unitMsr", -SUM(T1."LineTotal") "Value" FROM ORIN T0 INNER JOIN RIN1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OCRD T2 ON T0."CardCode" = T2."CardCode" WHERE T0."CANCELED" = 'N' GROUP BY YEAR(T0."DocDate"), T1."Dscription", T0."CardName", T1."unitMsr"

ORDER BY YEAR(T0."DocDate")

In this query i want to sort by the two columns:- YEAR(T0."DocDate"), T1."Dscription"

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • author's profile photo Former Member
    Former Member
    Posted on May 31, 2016 at 03:21 PM

    Hi,

    I can't find an error in your query !?

    you can try this at the end of Query:

    ORDER BY T0.DocDate, T1.Dscription

    hth

    Markus

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 21, 2016 at 03:06 PM

    Dear Prabhu,

    Kindly write the Order by at last union all as union all will have the same fields.

    Regards,

    Vestrics.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 22, 2016 at 04:30 AM

    Hi JK

    For order by simply use Order by YEAR(T0."DocDate"), T1."Dscription"

    or ORDER BY YEAR(T0."DocDate"),[Dscription]

    here is the full query

    SELECT 'Inv' as "Type", YEAR(T0."DocDate"), T1."Dscription" "Chemical", T0."CardName" "Customer", SUM(T1."Quantity") "Quantity", T1."unitMsr", SUM(T1."LineTotal") "Value"

    FROM OINV T0 INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OCRD T2 ON T0."CardCode" = T2."CardCode"

    WHERE T0."CANCELED" = 'N'

    GROUP BY YEAR(T0."DocDate"), T1."Dscription", T0."CardName", T1."unitMsr"

    UNION ALL

    SELECT 'CN' "Type", YEAR(T0."DocDate"), T1."Dscription" "Chemical", T0."CardName" "Customer", SUM(T1."Quantity") "Quantity", T1."unitMsr", -SUM(T1."LineTotal") "Value"

    FROM ORIN T0 INNER JOIN RIN1 T1 ON T0."DocEntry" = T1."DocEntry" INNER JOIN OCRD T2 ON T0."CardCode" = T2."CardCode"

    WHERE T0."CANCELED" = 'N'

    GROUP BY YEAR(T0."DocDate"), T1."Dscription", T0."CardName", T1."unitMsr"

    ORDER BY YEAR(T0."DocDate"),[Dscription]

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.