Solved

Understanding SQL

Posted on 2014-09-29
5
97 Views
Last Modified: 2014-09-30
Below is the SQL Statement I am trying to alter.  Can someone help me under stand what the
FROM portion is stating.

SELECT
      B.ORDER.DATE,
      B.ITEM.DESCRIPTION,
      B.QUANTITY,
      B.AMOUNT.BILLED,
      B.CANCEL.DATE,
      B.TYPE.OF.CANCEL,
      B.DAL.CANCELLED.BY,
      B.DAL.REASON.NOTES,
      B.DAL.REASON.TEXT,
      B.BILL.DATE,
FROM
      BILLING.TRANSACTIONS AS B LEFT OUTER JOIN DAILY.ACTIVITY.LOG AS D ON B.@ID = CAST
      (D.BILLING.TRANSACTION.NUM AS VARCHAR)
WHERE
      B.CANCEL.DATE = CURRENT_DATE
      AND B.CANCEL.DATE > CURRENT_DATE - 1
0
Comment
Question by:garyrobbins
5 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40351018
The SQL's not valid.  The @ in that location is not valid, and the extra '.'s in the column names are not valid.

Other than the syntax errors, though, it's a standard LEFT JOIN: from table1 left join to table2.
0
 
LVL 18

Expert Comment

by:daveslash
ID: 40351097
Scott's right. The syntax is slightly missed up, but what exactly are you asking?

Do you understand LEFT OUTER JOIN?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40351429
There is also an "incomplete query" issue because your select clause ends in a comma

If I assumed all of the illegal . are underscores _
e.g.
B.ORDER.DATE  is really
B.ORDER_DATE

and we remove that trailing comma and ignore the @ you query would look like this:
SELECT
      B.ORDER_DATE
    , B.ITEM_DESCRIPTION
    , B.QUANTITY
    , B.AMOUNT_BILLED
    , B.CANCEL_DATE
    , B.TYPE_OF_CANCEL
    , B.DAL_CANCELLED_BY
    , B.DAL_REASON_NOTES
    , B.DAL_REASON_TEXT
    , B.BILL_DATE

FROM BILLING.TRANSACTIONS AS B
      LEFT OUTER JOIN DAILY_ACTIVITY_LOG AS D
                  ON B.ID = CAST(D.BILLING_TRANSACTION_NUM AS varchar)

WHERE B.CANCEL_DATE = CURRENT_DATE
      AND B.CANCEL_DATE > CURRENT_DATE - 1

Open in new window

That FROM clause joins a billing transactions table to an activity log table using a join type that allows unmatched records in the transactions table to be listed; a LEFT OUTER JOIN. That is, IF there is no activity log information for a transaction, that transaction will still be shown.

A LEFT OUTER JOIN may also be used without the "outer" and are quite commonly referred to as "LEFT JOIN"

The JOIN CONDITION being used is "B.ID = CAST(D.BILLING_TRANSACTION_NUM AS varchar)"

This join condition isn't good from a performance perspective, as it changes data type and this robs the optimizer the opportunity of using an index on the field D.BILLING_TRANSACTION_NUM

The other issue with this join condition is it "looks weird" because B.ID would normally be associated with an integer (int or bigint) so it doesn't make much sense to change D.BILLING_TRANSACTION_NUM to varchar if it is being compared to an integer. Hence there may be another type conversion occurring on B.ID (an "implicit type conversion") which will further slow down the join's performance.

Regarding the where clause, I don't know what "CURRENT_DATE" is meant to be but in SQL Server you can get the current date via a system function getdate() (this has date plus time) and you can get the current date via
CAST(getdate() AS DATE)
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40351449
by the way, are you actually using Microsoft SQL Server?
if not, what is your database type please?
0
 

Author Closing Comment

by:garyrobbins
ID: 40352028
Thank you for the thorough response it answered some other questions i had also.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now