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


SQL Question


SQL Question

Author
Message
Yasir Masood
Yasir Masood
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 83
Hello,

I need your help in the following scenario.

I have customer information in two tables, ProductA_Customer and ProductB_Customer.
I am trying to create a report that would include all Customers from ProductA_Customer
and customers from ProductB_Customer that have no record in ProductA_Customer.

The result needs to be formatted in the following table

Cust_No_A
Cust_Name_A
Cust_Address_A
Cust_City_A
Cust_State_A
Cust_Zip_A
Cust_No_B
Cust_Name_B
Cust_Address_B
Cust_City_B
Cust_State_B
Cust_Zip_B

We are using SQL Server 2005. I would appreciare your help

Thanks
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3103 Visits: 2766
It will be good for us to solve your problem if you provide some more details like table structure, sample data and resultant output....

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Yasir Masood
Yasir Masood
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 83
Here is the table structure,

CREATE TABLE ProductA_Customer
(
Cust_No_A char(13) NOT NULL,
Cust_Name_A varchar(60) NULL,
Cust_Address_A varchar(60) NULL,
Cust_City_A varchar(20) NULL,
Cust_State_A char(2) NULL,
Cust_Zip_A char(10) NULL
)

CREATE UNIQUE CLUSTERED INDEX XPKProductA_Customer ON ProductA_Customer
(
Cust_No_A
)


CREATE TABLE ProductB_Customer
(
Cust_No_B char(13) NOT NULL,
Cust_Name_B varchar(60) NULL,
Cust_Address_B varchar(60) NULL,
Cust_City_B varchar(20) NULL,
Cust_State_B char(2) NULL,
Cust_Zip_B char(10) NULL
)

CREATE UNIQUE CLUSTERED INDEX XPKProductB_Customer ON ProductB_Customer
(
Cust_No_B
)

The report needs to be in the following format,

Cust_No_A
Cust_Name_A
Cust_Address_A
Cust_City_A
Cust_State_A
Cust_Zip_A
Cust_No_B
Cust_Name_B
Cust_Address_B
Cust_City_B
Cust_State_B
Cust_Zip_B

Thanks
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3103 Visits: 2766
You need to follow this article for posting any question:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
R. Brush
R. Brush
SSC-Enthusiastic
SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)SSC-Enthusiastic (195 reputation)

Group: General Forum Members
Points: 195 Visits: 352
-----------------------------
-- The following returns what you asked for (but may not be what you want):
-----------------------------
SELECT Cust_No_A
,Cust_Name_A
,Cust_Address_A
,Cust_City_A
,Cust_State_A
,Cust_Zip_A
,NULL AS Cust_No_B
,NULL AS Cust_Name_B
,NULL AS Cust_Address_B
,NULL AS Cust_City_B
,NULL AS Cust_State_B
,NULL AS Cust_Zip_B
FROM ProductA_Customer

UNION ALL
SELECT Cust_No_A
,Cust_Name_A
,Cust_Address_A
,Cust_City_A
,Cust_State_A
,Cust_Zip_A
,Cust_No_B
,Cust_Name_B
,Cust_Address_B
,Cust_City_B
,Cust_State_B
,Cust_Zip_B
FROM ProductB_Customer
LEFT JOIN ProductA_Customer
ON Cust_No_B = Cust_No_A
WHERE Cust_No_A IS NULL

-----------------------------
--The following would be a more typical recordset to return:
-----------------------------
WITH Cust_Unique(Cust_No, Cust_Name, Cust_Address, Cust_City, Cust_State, Cust_Zip, Table_Source)
AS (
SELECT Cust_No_A
,Cust_Name_A
,Cust_Address_A
,Cust_City_A
,Cust_State_A
,Cust_Zip_A
,'A'
FROM ProductA_Customer

UNION ALL
SELECT Cust_No_B
,Cust_Name_B
,Cust_Address_B
,Cust_City_B
,Cust_State_B
,Cust_Zip_B
,'B'
FROM ProductB_Customer
LEFT JOIN ProductA_Customer
ON Cust_No_B = Cust_No_A
WHERE Cust_No_A IS NULL
)

SELECT *
FROM Cust_Unique
ORDER BY Cust_No
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