?
Solved

VB.Net - SQL Query Help

Posted on 2013-12-20
8
Medium Priority
?
232 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
[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
8 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39731926
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 1000 total points
ID: 39731937
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
ID: 39731976
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Author Comment

by:Jimbo99999
ID: 39731986
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39731989
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
ID: 39732007
Excellent...thanks for the exlain.  I will try it now.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1000 total points
ID: 39732016
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
ID: 39736173
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

762 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