Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-13 : 14:45:44
|
| I've created a small database in addition to the one we already have at work. My new database looks at our customer base whom not necessarily registered with us and track some of the information that we don't want to have on our main database. Now I need to create a report and have a hard time coming up with if then, or case when then statment for it. My new database, table1 Guests, has following columns: Guest_ID int PK, Customer_ID float FK, FirstName char, LastName char and Phone nchar ... where current customers get assigned Guest_ID, but no other personal info inserted in there, and the prospective customer or guests have all info, except Customer_ID, but it might be added in future, when they register.table2 GuestsTrips: Guest_ID fk, customer_ID fk, trip_Id int PK, otherinfo1, otherinfo2, otherinfo3 (both customers and guests have all fields filled out)select g.Guest_ID, gt.Customer_ID, gt.Trip_ID, gt.otherinfo1, gt.otherinfo2, gt.otherinfo3FirstName=case (gt.Customer_ID)when null then (select g.FirstName from guests g where g.Guest_ID=gt.Guest_ID)when >'0' then (select p.FirstName from Customers c where gt.Customer_ID=c.Customer_ID)else 'missing info' endfrom GuestsTrips gt (nolock)join Guests g (nolock) on g.guest_ID=gt.guest_IDjoin Customers c (nolock) on gt.Customer_ID=c.Customer_IDI will need to get LastName and Phone information the same way as FirstName. Can anyone help me make it work and if possible to simplify it? I get all sort of error and I have messed with it for 2 days with no results. Help please, I'm really desperate. Will check how your sugestions work on monday, but will check forum on weekends to answer any questions. Thank you all in advance! |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-08-13 : 15:17:38
|
| I'm looking at the query and it doesn't make any sense to me. You are inner joining on CustomerID, then you check to see if it is NULL and return different values based on that case. But, how can CustomerID ever be NULL?Before i start asking questions about data types (comparing an ID to a string), maybe some sample data and expected output would be in order. Here is a link that can help you prepare said data for consumpion:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-13 : 22:23:40
|
| QUESTION:How to fill in the blanks of the report from different tables? Table Guests has some info, and table Customers has similar info. If a guest has a customerID, i need my query pull FirstName, LastName and phone from Customers table, if a guest does not have a customerID (ie, has not registered with our company and only has minimal personal info available on our temp database) i need FirstName, LastName and phone to be pulled from Guests table. DDL INFO:1) Create table GUESTSGuest_ID PK, int, not nullCustomer_ID FK, float, nullFirstName varchar(50), nullLastName varchar(50), nullPhone nchar(10), null2) Create Table TripInfoTrip_ID PK, int, not nullPlayer_ID FK, float, nullGuest_ID FK, int, not nullOtherInfo1 datetime, not nullOtherInfo2 varchar(50)OtherInfo3 varchar(50)3) Create Table CUSTOMERS (this on is from original database, the other two above are from my creation)Customer_ID PK, int, not nullFirstName varchar(20), not nullLastName varchar(20), not nullPhone varchar(21), nullselect g.Guest_ID, t.Trip_ID, t.Customer_ID, FirstName = case(t.Player_ID)when null then (select g.FirstName from GUESTS where g.Guest_ID=t.Guest_ID)when ='0' then (select g.FirstName from GUESTS where g.Guest_ID=t.Guest_ID)else (select c.FirstName from customers s where c.Customer_ID=t.Customer_ID)endFrom guests g (nolock)left join tripinfo t (nolock) on g.guest_id=t.guest_idleft join custoemers c (nolock) on c.customer_ID=t.Customer_IDHope this clarifies some of the confusion. I can usually write simple code and never done anything as involved as this project. Appologies for not having it all in a proper format. I'm not at work and had to type everything up. |
 |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-13 : 22:31:33
|
quote: Originally posted by tkizerCustomer_ID float - really? I wouldn't want approximate data for my customer ids. How about bigint instead?
Tara, the only reason I used float, because I have been told to do so. I'm not good at this type of work as you can see. In the original database, where that Customer_ID is automatically created, it is int, but for my database, I have been told to make it a float. I am not even sure why, and whenever I have question, the answer is usually "google it". |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 00:18:48
|
quote: Originally posted by motilok QUESTION:How to fill in the blanks of the report from different tables? Table Guests has some info, and table Customers has similar info. If a guest has a customerID, i need my query pull FirstName, LastName and phone from Customers table, if a guest does not have a customerID (ie, has not registered with our company and only has minimal personal info available on our temp database) i need FirstName, LastName and phone to be pulled from Guests table. DDL INFO:1) Create table GUESTSGuest_ID PK, int, not nullCustomer_ID FK, float, nullFirstName varchar(50), nullLastName varchar(50), nullPhone nchar(10), null2) Create Table TripInfoTrip_ID PK, int, not nullPlayer_ID FK, float, nullGuest_ID FK, int, not nullOtherInfo1 datetime, not nullOtherInfo2 varchar(50)OtherInfo3 varchar(50)3) Create Table CUSTOMERS (this on is from original database, the other two above are from my creation)Customer_ID PK, int, not nullFirstName varchar(20), not nullLastName varchar(20), not nullPhone varchar(21), nullselect g.Guest_ID, t.Trip_ID, t.Customer_ID, FirstName = case(t.Player_ID)when null then (select g.FirstName from GUESTS where g.Guest_ID=t.Guest_ID)when ='0' then (select g.FirstName from GUESTS where g.Guest_ID=t.Guest_ID)else (select c.FirstName from customers s where c.Customer_ID=t.Customer_ID)endFrom guests g (nolock)left join tripinfo t (nolock) on g.guest_id=t.guest_idleft join custoemers c (nolock) on c.customer_ID=t.Customer_IDHope this clarifies some of the confusion. I can usually write simple code and never done anything as involved as this project. Appologies for not having it all in a proper format. I'm not at work and had to type everything up.
for this you needSELECT COALESCE(c.FirstName, g.FirstName) AS FirstName,COALESCE(c.LastName , g.LastName ) AS LastName,COALESCE(c.Phone, g.Phone) AS PhoneFROM Guests gLEFT JOIN Customer cON c.Customer_ID = g.Customer_ID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-08-14 : 13:16:43
|
quote: Originally posted by motilok
quote: Originally posted by tkizerCustomer_ID float - really? I wouldn't want approximate data for my customer ids. How about bigint instead?
Tara, the only reason I used float, because I have been told to do so. I'm not good at this type of work as you can see. In the original database, where that Customer_ID is automatically created, it is int, but for my database, I have been told to make it a float. I am not even sure why, and whenever I have question, the answer is usually "google it".
Use int, do not use float. There isn't anything to google here, float is not the proper data type and you will likely run into problems with it.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
motilok
Starting Member
24 Posts |
Posted - 2010-08-16 : 10:45:12
|
| Thank you, visakh16 and everyone else for your help. I got it working and I learned a few things on the way.. :) Thanks a bunch! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 10:51:10
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|