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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 i need your help to do a specific select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sebastian11c
Posting Yak Master

125 Posts

Posted - 08/15/2012 :  18:37:53  Show Profile  Reply with Quote
hi there

i have a able called "bills"

CREATE TABLE [dbo].[bills](
[idbill] [int] NULL,
[productname] [varchar](50) NULL,
[city] [varchar](50) NULL
) ON [PRIMARY]

GO

and lets put some records


INSERT INTO [dbo].[bills]
([idbill]
,[productname]
,[city])
select 1, 'apple', 'miami'
union
select 1, 'cherry', 'miami'
union
select 2, 'apple', 'NY'
union
select 3, 'apple', 'LA'
union
select 3, 'cherry', 'LA'
union
select 4, 'apple', 'washington'
union
select 7, 'strawberry', 'washington'



and i need to do a select with a column to identify if in the same bill there are "apple" and "cherry" product becase if there are these 2 items in the same bill and the city<> 'miami' i want to identify as a "combo"

that the result that i want , and i need

idbill productname city identify
1 apple miami not combo
1 cherry miami not combo
2 apple NY not combo
3 apple LA combo
3 cherry LA combo
4 apple washington not combo
7 strawberry washington not combo

many many thanks for your help


kind regards

chadmat
The Chadinator

USA
1955 Posts

Posted - 08/15/2012 :  18:52:49  Show Profile  Visit chadmat's Homepage  Reply with Quote
I think this table is in need of some normalization.

-Chad
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 08/15/2012 :  21:33:15  Show Profile  Reply with Quote
hi chad coul you explain how to do it

im a newbie

thanks in advanced
Go to Top of Page

chadmat
The Chadinator

USA
1955 Posts

Posted - 08/15/2012 :  21:48:57  Show Profile  Visit chadmat's Homepage  Reply with Quote
I would need a lot more information, but there should probably be a Product Table, a Bill Table, maybe a customer table etc... Bing 3rd normal form, or database normalization.

-Chad
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/15/2012 :  22:50:18  Show Profile  Reply with Quote

SELECT t.*,
CASE WHEN t1.idbill IS NOT NULL THEN 'Combo' ELSE 'Non Combo' END
FROM Table t
LEFT JOIN (SELECT idbill
           FROM Table
           GROUP BY idbill
           HAVING COUNT(DISTINCT CASE WHEN productname IN ('apple','cherry') AND city <> 'miami' THEN productname ELSE NULL END) =2
          )t1
ON t1.idbill = t.idbill


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

Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 08/15/2012 :  23:01:49  Show Profile  Reply with Quote
always you, always you visakh16 with your magic

once again a fantastic code

many many thanks

thanks for share your knowledge
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/15/2012 :  23:23:27  Show Profile  Reply with Quote
you're welcome

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

Go to Top of Page

suraz
Starting Member

Nepal
2 Posts

Posted - 08/16/2012 :  07:20:39  Show Profile  Reply with Quote
##### Code Optimized #####

SELECT t.*,
CASE WHEN t1.idbill IS NOT NULL THEN 'Combo' ELSE 'Non Combo' END
FROM bills t
LEFT JOIN (SELECT idbill
FROM bills
WHERE city <>'miami'
GROUP BY idbill
HAVING COUNT(idbill)=2)t1 ON t1.idbill = t.idbill
Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 08/17/2012 :  13:11:27  Show Profile  Reply with Quote
thanks you suraz for your help

kund regards
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/17/2012 :  13:21:55  Show Profile  Reply with Quote
quote:
Originally posted by suraz

##### Code Optimized #####

SELECT t.*,
CASE WHEN t1.idbill IS NOT NULL THEN 'Combo' ELSE 'Non Combo' END
FROM bills t
LEFT JOIN (SELECT idbill
FROM bills
WHERE city <>'miami'
GROUP BY idbill
HAVING COUNT(idbill)=2)t1 ON t1.idbill = t.idbill


how is this satisfying original requirement? this is not even looking for productname for apple and cherry items

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

Go to Top of Page

sebastian11c
Posting Yak Master

125 Posts

Posted - 08/18/2012 :  09:59:01  Show Profile  Reply with Quote
hi visakh16

its true the code from suraz its not as complete as yours for my requiremets, but he tried to help me too with his answer..
so im just saying thank you

thanks again visakh16 for your great reply , really helps to me your code
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/18/2012 :  11:09:11  Show Profile  Reply with Quote
quote:
Originally posted by sebastian11c

hi visakh16

its true the code from suraz its not as complete as yours for my requiremets, but he tried to help me too with his answer..
so im just saying thank you

thanks again visakh16 for your great reply , really helps to me your code



Nope I was not commenting on that
I was just checking with Suraj to confirm whether I'm missing something in his suggestion

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

Go to Top of Page

suraz
Starting Member

Nepal
2 Posts

Posted - 08/20/2012 :  00:48:45  Show Profile  Reply with Quote
hi visakh16,

both queries returning the same result as sebastian11c's requirement and table/data structure.

Difference is only this:
Your query exactly meets the sebastian11c's requirement for the product "apple" and "cherry" and city<> 'miami' but it won't work when you want same result for other more products. Suppose you need to do same for strawberry then you add strawberry in your query i.e. every time need to change in query

You can do the same for thousand of products by using updated query and require no changes in query.

###################################################################
Thanks to All :)
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
643 Posts

Posted - 08/25/2012 :  21:56:55  Show Profile  Reply with Quote
quote:
Originally posted by suraz

hi visakh16,

both queries returning the same result as sebastian11c's requirement and table/data structure.

Difference is only this:
Your query exactly meets the sebastian11c's requirement for the product "apple" and "cherry" and city<> 'miami' but it won't work when you want same result for other more products. Suppose you need to do same for strawberry then you add strawberry in your query i.e. every time need to change in query

You can do the same for thousand of products by using updated query and require no changes in query.

###################################################################
Thanks to All :)




The problem is that the OP was very specific about the conditions that make up a combo. They must have both "apple" and "cherry" and not be "Miami". If we change the sample data a bit, we can see that your code doesn't meet that requirement.

INSERT INTO [dbo].[bills]
([idbill]
,[productname]
,[city])
select 1, 'apple', 'miami'
union
select 1, 'cherry', 'miami'
union
select 2, 'apple', 'NY'
union
select 3, 'apple', 'LA'
union
select 3, 'peach', 'LA' --Changed but still comes up as "Combo"
union
select 4, 'apple', 'washington'
union
select 7, 'strawberry', 'washington'


--Jeff Moden
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 09/03/2012 :  09:41:40  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by visakh16


SELECT t.*,
CASE WHEN t1.idbill IS NOT NULL THEN 'Combo' ELSE 'Non Combo' END
FROM Table t
LEFT JOIN (SELECT idbill
           FROM Table
           GROUP BY idbill
           HAVING COUNT(DISTINCT CASE WHEN productname IN ('apple','cherry') AND city <> 'miami' THEN productname ELSE NULL END) =2
          )t1
ON t1.idbill = t.idbill


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





If you use SUM(CASE WHEN ... THEN 1 ELSE 0 END)... you can avoid the unneccesary warning about NULL

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 09/03/2012 :  15:24:22  Show Profile  Reply with Quote
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16


SELECT t.*,
CASE WHEN t1.idbill IS NOT NULL THEN 'Combo' ELSE 'Non Combo' END
FROM Table t
LEFT JOIN (SELECT idbill
           FROM Table
           GROUP BY idbill
           HAVING COUNT(DISTINCT CASE WHEN productname IN ('apple','cherry') AND city <> 'miami' THEN productname ELSE NULL END) =2
          )t1
ON t1.idbill = t.idbill


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





If you use SUM(CASE WHEN ... THEN 1 ELSE 0 END)... you can avoid the unneccesary warning about NULL

Madhivanan

Failing to plan is Planning to fail


yep i know that
Anyways thnx for reminding

------------------------------------------------------------------------------------------------------
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.22 seconds. Powered By: Snitz Forums 2000