October 28, 2012 at 10:19 pm
Customer
CustomerIDCustomerNameBirthdate
1John Doe1/1/1970 08:31 AM
2Jane Doe1/1/1971 01:18 PM
3Jon Public1/1/1972 11:58 PM
4Jane Public1/1/1973 07:00 AM
5John Smith1/1/1974 08:31 AM
Order
OrderIDPO NumberOrderDate
1000ABC1231/1/2012 01:00 PM
20001122332/1/2012 02:00 AM
3000XYZ9873/1/2012 03:00 PM
4000500004/1/2012 04:00 AM
5000Verbal5/1/2012 05:00 AM
CustomerOrders
CustomerIDOrderIDIsShipped
11000False
13000True
34000False
22000True
55000True
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:-):-)--------------------------------------------------------------------------------
October 28, 2012 at 10:45 pm
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
October 28, 2012 at 10:46 pm
prathibha_aviator (10/28/2012)
CustomerCustomerIDCustomerNameBirthdate
1John Doe1/1/1970 08:31 AM
2Jane Doe1/1/1971 01:18 PM
3Jon Public1/1/1972 11:58 PM
4Jane Public1/1/1973 07:00 AM
5John Smith1/1/1974 08:31 AM
Order
OrderIDPO NumberOrderDate
1000ABC1231/1/2012 01:00 PM
20001122332/1/2012 02:00 AM
3000XYZ9873/1/2012 03:00 PM
4000500004/1/2012 04:00 AM
5000Verbal5/1/2012 05:00 AM
CustomerOrders
CustomerIDOrderIDIsShipped
11000False
13000True
34000False
22000True
55000True
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
Change is inevitable... Change for the better is not.
October 29, 2012 at 9:58 am
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:-):-)--------------------------------------------------------------------------------
October 29, 2012 at 10:32 am
prathibha_aviator (10/29/2012)
Thanks for the reply I tried using this and got some errorMsg 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 29, 2012 at 11:03 am
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:-):-)--------------------------------------------------------------------------------
October 29, 2012 at 11:11 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 29, 2012 at 11:24 am
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:-):-)--------------------------------------------------------------------------------
October 29, 2012 at 11:41 am
You're welcome. Glad you figured it out and thanks for letting us know. 😀
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply