Solved

Understanding SQL

Posted on 2014-09-29
5
100 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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