Solved

VB.Net - SQL Server Query Assistance

Posted on 2014-12-09
5
227 Views
Last Modified: 2014-12-15
Good Day Experts!

I have been thinking about this query that I need to write before I can start on my new project but I cannot seem to come up with how to write it...it "feels" possible. Perhaps you can lend some insight. I need to look at my Invoice table and determine all the Invoice Numbers where the all the Orders for each Invoice have been filled. Here is some sample of what I would be looking at:

Invoice     Order     Filled
1234         A            1
1234         B            1
1234         C            1
5678         A            1
5678         B            1
5678         C            0
4321         A            0
4321         B            0
4321         C            0

I just can't seem to get started...can you help?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40489650
This will give you all invoice numbers that don't have a filled = 0.

It doesn't account for the condition where there is an order and the invoice hasn't been created yet.


select invoicenumber
from invoices
where not exists
(select invoicenumber from table where filled = 0)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40489651
<Air code.  I also do my own stunts>

Since Filled is a 1/0, a sum would give you all orders filled, and a count would give you the count.  Then just subtract the two, and any non-zero value means there are orders not filled.  
SELECT Invoice, Sum(Filled) as sum_filled, Count(Filled) as count_filled
FROM YourTable
HAVING Sum(Filled) <> Count(Filled)

Open in new window

0
 

Author Comment

by:Jimbo99999
ID: 40489746
Hello, thanks for responding.  I tried both.  I actually did not realize the Filled field was defined as a bit so I received an error.  Kyle, I was able to get your version working.  We will be setting up a test table later today/early tomm so I can make the records to test with using the query.  So, if you don't mind I will leave the question open in case I have any questions.

Thanks,
jimbo99999
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40500687
Do you need further assistance?
0
 

Author Comment

by:Jimbo99999
ID: 40500700
Thanks for the help...I had a small "fire" with another project for the past couple of days.  I just did some final testing with this and it is working great!

Thanks and have a good day,
jimbo99999
0

Featured Post

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.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

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

15 Experts available now in Live!

Get 1:1 Help Now