Solved

SQL Query

Posted on 2013-01-09
3
286 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 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