Solved

VB.Net - SQL Query Help

Posted on 2013-12-20
8
227 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)
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 250 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 29
Sql Server group by 10 23
Sending receiving text messages in vb.net 15 32
vb.net and creating a class 5 14
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

813 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

10 Experts available now in Live!

Get 1:1 Help Now