Solved

VB.Net - SQL Server Query Assistance

Posted on 2014-12-09
5
228 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

839 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