Solved

Understanding SQL

Posted on 2014-09-29
5
101 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:Scott Pletcher
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

856 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