Solved

VB.Net - SQL Query Help

Posted on 2013-12-20
8
225 Views
Last Modified: 2014-01-02
Good Day Experts!

I am not sure if I can do all the required "checks" in one query.  Hopefully you can help me find out if it is possible and how to do it.

Status table:
     - InvoiceNumber
     - TrackingNumber
     - ServiceFailure
     - SFRejected
     - DatePC

DWINonPay table:
     - InvoiceNumber
     - TrackingNumber
     - DateImported

Select * from Status with the following requirements:

Requirements:
     - Status.ServiceFailure = 1
     AND
     - Status.SFRejected = 1
     AND
     - (DW1NonPay InvoiceNumber/TrackingNumber from Status not found
        OR
       (DW1NonPay InvoiceNumber/TrackingNumber from Status found but
        DW1NonPay.DateImported > Status.DatePC)

Please ask questions and let me know your opinions.

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
8 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Not sure what you are trying to check?  Do the requirements mentioned above reflect individual checks, or are you simply trying to identify all the records across the two tables that satisfy all of those requirements, in a single query?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 250 total points
Comment Utility
Try this:
SELECT t1.* 
    FROM Status t1
         LEFT JOIN DWINonPay t2 ON t1.InvoiceNumber = t2.InvoiceNumber 
              AND t1.TrackingNumber = t2.TrackingNumber
    WHERE 
    (
        t1.ServiceFailure = 1
        AND t1.SFRejected = 1
        AND
        (
             t2.DateImported IS NULL
             OR t2.DateImported > t1.DatePC
        )
    )

Open in new window

0
 

Author Comment

by:Jimbo99999
Comment Utility
Hello fyed:

Your second part is what I am trying to do...I am just not that good at complicated SQL query "stuff":

"trying to identify all the records across the two tables that satisfy all of those requirements in a single query"

Thanks,
jimbo99999
0
 

Author Comment

by:Jimbo99999
Comment Utility
Hello carl_tawn:

Can you help me understand your query as I am not very good at complicated SQL query "stuff"?  Is it adding subsets of data together from both tables?

Thanks,
jimbo99999
0
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).

 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
No, it is doing a LEFT JOIN between the tables, so it will join but return NULL where there is no match. The rest is just applying the rules you listed, the brackets group the logic.
0
 

Author Comment

by:Jimbo99999
Comment Utility
Excellent...thanks for the exlain.  I will try it now.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
Comment Utility
SELECT S.InvoiceNumber,
      S.TrackingNumber,
   S.ServiceFailure,
   S.SFRejected,
   S.DatePC
FROM Status AS S
LEFT OUTER JOIN DWINonPay AS D
      ON S.InvoiceNumber = D.InvoiceNumber
      AND S.TrackingNumber = D.TrackingNumber
WHERE S.ServiceFailure = 1
   AND S.SFRejected = 1
   AND (D.InvoiceNumber IS NULL OR D.DateImported > S.DatePC)
0
 

Author Comment

by:Jimbo99999
Comment Utility
I am finally able to get back to this project and have received known data samples to test the query and logic in my code to.

Thanks for the responses and I will try them,
jimbo99999
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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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

16 Experts available now in Live!

Get 1:1 Help Now