I am not sure i understand the issue here. I'm having a hardtime understanding it from you example and explanation.

Now, some general comments on you code:

$DBH = &connect or die "Cannot connect to the sql server \n";

This is mysql, right? SQL Server is another product and may confuse you at a later date. Consider changing the message to "Cannot connect to database."

datas

A nitpick, but, "datum" is singular, "data" is plural. "Datas" is just plain wrong. :) Although rarely done, if you want to be correct, change the columns to "datum1" and "datum2" and call the table "data".

There is no reason to place the query-parts in parenthesis. UNION ALL works just fine without them.

The second query-part does not need aliases. They will be ignored anyway. Column names in a UNION (ALL) always follow the names in the first query-part.

Both subtables are aliased "new". While the name is not referred to, and context will separate them, it is best to use different names to avoid confusion. Further, the name "new" is a poor choice, as it is often a keyword and can cause errors or confusion.

While there is an ORDER BY in the sub-queries to help with LIMIT and OFFSET, there is no ORDER BY on the outer query. Usually this isn't a problem, as a UNION ALL places this results of the second part after those of the first part. But this is not how it has to be, it just happens to work out that way. If order is important, you should put an ORDER BY on the outer query, and remember to use column ordinals and not names there, unless your RDBMS supports names there.


In reply to Re: perl sql to separate and sort the column separately? by chacham
in thread perl sql to separate and sort the column separately? by gpssana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.