Yeah, that's is what SQL is made for.

First, some comments on the SQL itself:

Remove the quotes. Without spaces or case sensitivity, there's no point. Worse, if the query is run in a case sensitive environment, it will fail. Quotes preserve case, but databases usually uppercase all names. So, unless the name was created with quotes and something other than uppercase, you probably want to avoid using them. There's also the clutter they create.

Keep like columns together. For ease of reading, keep columns from the same table together. Otherwise, it can get hard to identify which columns come from which table. As the order the columns are used in is how you use the variable in your code, there's no reason to order the columns in any specific way. So, i'd go for ease of reading.

Alias normally. Why people use coded names for table is beyond me. Regardless, they do it. But if you're going to alias the names in the query, use words that make sense and are easily identified.

Qualify all column names. When there is more than one table in the query, make sure all names are qualified. This avoids errors, and helps the reader understand what is going on.

Just say no to inner joins. ANSI SQL Join syntax is just plain wrong. While many disagree with my opinion, i have to mention how hard that query is to discern. Moved into the WHERE clause, makes it so much cleaner and obvious.

Taking those notes into consideration, the query can be rewritten as follows:

SELECT TOP 500 Service.Voucher_Number, Service.Service_Date_From, Service.Procedure_Code, -- Patient.Patient_Number, Patient.Patient_First_Name, Patient.Patient_Last_Name, -- Payment.Transaction_Type, Payment.Transfer_To_Carrier_Abbr FROM Ntier_Training.PM.vwGenSvcInfo Service, Ntier_Training.PM.vwGenPatInfo Patient, Ntier_Training.PM.vwGenSvcPmtInfo Payment WHERE Patient.Patient_ID = Service.Patient_ID AND Payment.Service_ID = Service.Service_ID ORDER BY Service.Voucher_Number;

Isn't that easier to read and understand? :)

As for the aggregation, you probably want to use GROUP BY. Without knowing exactly what is required, it is hard to suggest a specific query.


In reply to Re^2: Suggestions for handling a complex set of data by chacham
in thread Suggestions for handling a complex set of data by sweetblood

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.