Solved

Display records where count is greater than 3 all in one query instead of multiple views SQL Server

Posted on 2016-10-25
2
34 Views
Last Modified: 2016-10-25
Hi all.

Currently I have 2 views that tell me if an invoice whose Status = 2 has more than 3 purchase orders assigned to it. I'd like to have one query do all of this instead of 2 views.

Table1: InvoiceNumber, Status
Table2: InvoiceNumber, PurchaseOrderNumber, ItemNumber

Sample data:
Table1 =
9099, 2
9098, 2
9097, 1

Table2=
9099, PO1, 787
9099, PO1, 654
9099, PO2, 124
9099, PO3, 445
9099, PO4, 457
9098, PO5, 789
9098, PO6, 111
9097, PO1, 455

So the query should display the following result: InvoiceNumber = 9099

I would love to be able to display the invoice number and the purchase orders:
9099, PO1
9099, PO2
9099, PO3
9099, PO4

But if that's too complicated or would still require multiple views then I'd be ok with just the invoice number.

Any ideas?

Thank you in advance.
0
Comment
Question by:printmedia
2 Comments
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 41859256
Here:
-- create invoice table
if object_id('tempdb..#invoice') is not null
	drop table #invoice
create table #invoice (InvoiceNumber int, Status int)
insert into #invoice 
select 
	*
from
	(values 
		(9099, 2),
		(9098, 2),
		(9097, 1)
	) q(InvoiceNumber,Status)

-- create order table
if object_id('tempdb..#order') is not null
	drop table #order
create table #order (InvoiceNumber int, PurchaseOrderNumber varchar(10), ItemNumber int)
insert into #order
select 
	*
from
	(values 
		(9099, 'PO1', 787),
		(9099, 'PO1', 654),
		(9099, 'PO2', 124),
		(9099, 'PO3', 445),
		(9099, 'PO4', 457),
		(9098, 'PO5', 789),
		(9098, 'PO6', 111),
		(9097, 'PO1', 455)
	) q(InvoiceNumber, PurchaseOrderNumber, ItemNumber)

-- check tables content
select * from #invoice
select * from #order

-- query to return those invoices that have 3 or more distinct orders
select distinct
	InvoiceNumber, 
	PurchaseOrderNumber
from 
	#order i
where
	(select count(distinct PurchaseOrderNumber) from #order where InvoiceNumber=i.InvoiceNumber)>=3

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 41859266
Worked great. Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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