SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kwacz23
Starting Member

44 Posts

Posted - 12/07/2012 :  01:22:17  Show Profile  Reply with Quote
Hello !!

Please advise me in that issue. Please find below table

Order_Number Order_line Vendor
1 1 A
1 1.1 A
1 1.2 A
1 2 B
1 3 C
2 1 A
2 1.1 A
3 1 A
3 1.1 A
3 2 C

Order line means (for exapmle1 is a laptop and 1.1 is a keyboard from Vendor A)

How I can retrive data for whole order when vendor is for example only C.
When I use:
Select * from table
where vendor=C

I only got 2 records like:

1 3 C
3 2 C

but I need :
1 1 A
1 1.1 A
1 1.2 A
1 2 B
1 3 C
3 1 A
3 1.1 A
3 2 C

I hope it makes sense.

Thanks!







bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 12/07/2012 :  01:54:48  Show Profile  Reply with Quote
DECLARE @Orders TABLE(Order_Number int, Order_line varchar(6), Vendor char(1))
INSERT INTO @Orders
SELECT 1, '1', 'A' union all
SELECT 1, '1.1', 'A' union all
SELECT 1, '1.2', 'A' union all
SELECT 1, '2', 'B' union all
SELECT 1, '3', 'C' union all
SELECT 2, '1', 'A' union all
SELECT 2, '1.1', 'A' union all
SELECT 3, '1', 'A' union all
SELECT 3, '1.1', 'A' union all
SELECT 3, '2', 'C'

SELECT *
FROM @Orders
WHERE Order_Number IN (SELECT Order_Number FROM @Orders WHERE Vendor = 'C')


--
Chandu
Go to Top of Page

kwacz23
Starting Member

44 Posts

Posted - 12/07/2012 :  06:03:13  Show Profile  Reply with Quote
Thank you for your response !!!

Is it posible to display only order where is only one vendor
I mean in the order you have only things from one order.

I my table there aren't these example. So query shouldn't give any result

Regards
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 12/07/2012 :  06:09:32  Show Profile  Reply with Quote
quote:
Originally posted by kwacz23

Thank you for your response !!!

Is it posible to display only order where is only one vendor
I mean in the order you have only things from one order.

I my table there aren't these example. So query shouldn't give any result

Regards


Can you post sample data which is in ur table and expected output?

--
Chandu
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 12/07/2012 :  06:36:49  Show Profile  Reply with Quote
See this:
Here Vendor 'D' having only one Order_number 2. So assuming that you want data related to vendor 'D'..

DECLARE @Orders TABLE(Order_Number int, Order_line varchar(6), Vendor char(1))
INSERT INTO @Orders
SELECT 1, '1', 'A' union all
SELECT 1, '1.1', 'A' union all
SELECT 1, '1.2', 'A' union all
SELECT 1, '2', 'B' union all
SELECT 1, '3', 'C' union all
SELECT 2, '1', 'D' union all
SELECT 2, '1.1', 'D' union all
SELECT 3, '1', 'A' union all
SELECT 3, '1.1', 'A' union all
SELECT 3, '2', 'C'

SELECT * FROM @Orders 
WHERE Order_Number IN (SELECT Order_Number 
			FROM @Orders 
			GROUP BY Order_Number
			HAVING COUNT(distinct vendor) = 1
)




--
Chandu

Edited by - bandi on 12/07/2012 06:37:44
Go to Top of Page

kwacz23
Starting Member

44 Posts

Posted - 12/08/2012 :  12:27:45  Show Profile  Reply with Quote
Thanks a lot !
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2217 Posts

Posted - 12/10/2012 :  00:14:23  Show Profile  Reply with Quote
quote:
Originally posted by kwacz23

Thanks a lot !


Welcome

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/14/2012 :  02:17:21  Show Profile  Reply with Quote

SELECT *
FROM
(
SELECT *,
MAX(Vendor) OVER (PARTITION BY Order_number) AS MaxVendor,
,MIN(Vendor) OVER (PARTITION BY Order_number) AS MinVendor
FROM TAble
)t
Where MaxVendor = MinVendor


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.56 seconds. Powered By: Snitz Forums 2000