SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


need help..cant understand how to approach


need help..cant understand how to approach

Author
Message
prathibha_aviator
prathibha_aviator
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 271
Customer

CustomerID CustomerName Birthdate
1 John Doe 1/1/1970 08:31 AM
2 Jane Doe 1/1/1971 01:18 PM
3 Jon Public 1/1/1972 11:58 PM
4 Jane Public 1/1/1973 07:00 AM
5 John Smith 1/1/1974 08:31 AM
Order

OrderID PO Number OrderDate
1000 ABC123 1/1/2012 01:00 PM
2000 112233 2/1/2012 02:00 AM
3000 XYZ987 3/1/2012 03:00 PM
4000 50000 4/1/2012 04:00 AM
5000 Verbal 5/1/2012 05:00 AM
CustomerOrders

CustomerID OrderID IsShipped
1 1000 False
1 3000 True
3 4000 False
2 2000 True
5 5000 True

Question is

Provide the sql statement that will return the PO Number if the CustomerID is greater than 3 and the OrderID if the CustomerID is 3 or less.

--Pra:-):-)--------------------------------------------------------------------------------
BriPan
BriPan
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 296
Try this One

select
* -- What ever column you want
,(case when Customer.CustomerID >3 then Order.PO else Order.OrderID end) as 'PO/OrderID'
from
Customer
inner join CustomerOrders on Customer.CustomerID =CustomerOrders.CustomerID
inner join Order on CustomerOrders.OrderID =Order.OrderID


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)SSC Guru (208K reputation)

Group: General Forum Members
Points: 208095 Visits: 41970
prathibha_aviator (10/28/2012)
Customer

CustomerID CustomerName Birthdate
1 John Doe 1/1/1970 08:31 AM
2 Jane Doe 1/1/1971 01:18 PM
3 Jon Public 1/1/1972 11:58 PM
4 Jane Public 1/1/1973 07:00 AM
5 John Smith 1/1/1974 08:31 AM
Order

OrderID PO Number OrderDate
1000 ABC123 1/1/2012 01:00 PM
2000 112233 2/1/2012 02:00 AM
3000 XYZ987 3/1/2012 03:00 PM
4000 50000 4/1/2012 04:00 AM
5000 Verbal 5/1/2012 05:00 AM
CustomerOrders

CustomerID OrderID IsShipped
1 1000 False
1 3000 True
3 4000 False
2 2000 True
5 5000 True

Question is

Provide the sql statement that will return the PO Number if the CustomerID is greater than 3 and the OrderID if the CustomerID is 3 or less.


Since this looks a whole lot like homework, I recommend that you take a look at the CASE statement in Books Online (the free help system that comes with SQL Server).

Also, take a look at the first link in my signature line below. You'll get better answers that way.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
prathibha_aviator
prathibha_aviator
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 271
Thanks for the reply I tried using this and got some error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'Verbal' to data type int.

Do i need to use cast or convert functions..?

--Pra:-):-)--------------------------------------------------------------------------------
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60467 Visits: 17954
prathibha_aviator (10/29/2012)
Thanks for the reply I tried using this and got some error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'Verbal' to data type int.

Do i need to use cast or convert functions..?


Remember that we can't see your screen. When you say "I tried using this and got some error " it doesn't say what you tried.

You should read the article that Jeff suggested previously. Also Jeff stated, this sounds a lot like homework and as a result most people around here are not going to hand you an answer, instead we will guide you on how to find the answer. That means you actually learn the material instead of just getting the assignment done.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
prathibha_aviator
prathibha_aviator
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 271
BriPan (10/28/2012)
Try this One

select
* -- What ever column you want
,(case when Customer.CustomerID >3 then Order.PO else Order.OrderID end) as 'PO/OrderID'
from
Customer
inner join CustomerOrders on Customer.CustomerID =CustomerOrders.CustomerID
inner join Order on CustomerOrders.OrderID =Order.OrderID



getting an error saying converting the field value 'verbal' to int is impossible... Why is it bothered about the datatypes of the same column when we r using select statement??? Select just returns the values right??

--Pra:-):-)--------------------------------------------------------------------------------
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60467 Visits: 17954
prathibha_aviator (10/29/2012)
BriPan (10/28/2012)
Try this One

select
* -- What ever column you want
,(case when Customer.CustomerID >3 then Order.PO else Order.OrderID end) as 'PO/OrderID'
from
Customer
inner join CustomerOrders on Customer.CustomerID =CustomerOrders.CustomerID
inner join Order on CustomerOrders.OrderID =Order.OrderID



getting an error saying converting the field value 'verbal' to int is impossible... Why is it bothered about the datatypes of the same column when we r using select statement??? Select just returns the values right??


It does but the values for a given column all have to in the same datatype. I am guessing here that OrderID is an int? This means that because of your case expression you are trying to return int and varchar data. It will attempt to convert to int because of datatype precedence. That means it will implicitly attempt to convert to int and the value 'verbal' will fail. To get around this you need to explicitly convert to a lower precendence (varchar). Instead of returning OrderID you will need to cast it as varchar. That help?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
prathibha_aviator
prathibha_aviator
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 271
Perfect.... I got the result now.....Thanku again Sean...

select
Customer.CustomerID, (case when Customer.CustomerID >3 then Orders.PONumber else cast(Orders.OrderID AS varchar) end) as 'PONumber/OrderID'
from
Customer
inner join CustomerOrders on Customer.CustomerID =CustomerOrders.CustomerID
inner join Orders on CustomerOrders.OrderID =Orders.OrderID

--Pra:-):-)--------------------------------------------------------------------------------
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60467 Visits: 17954
You're welcome. Glad you figured it out and thanks for letting us know. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search