Solved

Understanding SQL

Posted on 2014-09-29
5
103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Dave Ford
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 49

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 49

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

729 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