Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Understanding SQL

Posted on 2014-09-29
5
Medium Priority
?
106 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 70

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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

916 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