Solved

SQL Query

Posted on 2013-01-09
3
282 Views
Last Modified: 2013-01-09
Hello,

I am in need of some assistance. I have an SQL DB that I need to run this query - however I do not know how to write the query beyond what is below.

SELECT [OrderNumber]
      ,[Date]
      ,[Branch]
      ,[InvoiceNumber]
      ,[Sku]
      ,[AccountNumber]
      ,[QtySold]
  FROM [SRIRRA50ED8483SQL].[dbo].[Sku]

Basically, I need to look at each InvoiceNumber and check to see if it contains any item [QtySold] => 12. If true than display all InvoiceNumbers that do not contain [Sku] = "QUIK".

Let me know if this is unclear or if you require further information.

Thank you very much for any assistance you can offer.

Ryan
0
Comment
Question by:Ryan Rood
  • 2
3 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 38759413
try

SELECT [OrderNumber]
      ,[Date]
      ,[Branch]
      ,[InvoiceNumber]
      ,[Sku]
      ,[AccountNumber]
      ,[QtySold]
  FROM [SRIRRA50ED8483SQL].[dbo].[Sku] a
  where exists(select 1 from [Sku] b where a.InvoiceNumber = b.InvoiceNumber and QtySold >= 12)
      and a.SKU <> 'QUIK'
0
 
LVL 9

Expert Comment

by:sognoct
ID: 38759415
SELECT [OrderNumber]
      ,[Date]
      ,[Branch]
      ,[InvoiceNumber]
      ,[Sku]
      ,[AccountNumber]
      ,[QtySold]
  FROM [SRIRRA50ED8483SQL].[dbo].[Sku]
 WHERE QtySold >= 12 AND NOT InvoiceNumber LIKE '%QUIK%'

Open in new window

0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38759426
oops

SELECT [OrderNumber]
      ,[Date]
      ,[Branch]
      ,[InvoiceNumber]
      ,[Sku]
      ,[AccountNumber]
      ,[QtySold]
  FROM [SRIRRA50ED8483SQL].[dbo].[Sku] a
  where exists(select 1 from [SRIRRA50ED8483SQL].[dbo].[Sku] b where a.InvoiceNumber = b.InvoiceNumber and QtySold >= 12)
      and not exists(select 1 from [SRIRRA50ED8483SQL].[dbo].[Sku] b where a.InvoiceNumber = b.InvoiceNumber and b.SKU = 'QUIK')
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

758 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

20 Experts available now in Live!

Get 1:1 Help Now