Solved

VB.Net - SQL Query Help

Posted on 2013-12-20
8
226 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

862 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

23 Experts available now in Live!

Get 1:1 Help Now