Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

need help..cant understand how to approach Expand / Collapse
Author
Message
Posted Sunday, October 28, 2012 10:19 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, 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--------------------------------------------------------------------------------
Post #1378093
Posted Sunday, October 28, 2012 10:45 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, January 31, 2014 4:06 AM
Points: 83, 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

Post #1378096
Posted Sunday, October 28, 2012 10:46 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:53 PM
Points: 36,795, Visits: 31,257
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1378097
Posted Monday, October 29, 2012 9:58 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, 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--------------------------------------------------------------------------------
Post #1378323
Posted Monday, October 29, 2012 10:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 13,139, Visits: 11,980
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 Moden's 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)
Post #1378343
Posted Monday, October 29, 2012 11:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, 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--------------------------------------------------------------------------------
Post #1378362
Posted Monday, October 29, 2012 11:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 13,139, Visits: 11,980
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 Moden's 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)
Post #1378369
Posted Monday, October 29, 2012 11:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 13, 2014 1:48 PM
Points: 104, 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--------------------------------------------------------------------------------
Post #1378382
Posted Monday, October 29, 2012 11:41 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 13,139, Visits: 11,980
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 Moden's 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)
Post #1378388
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse