Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Am having a small problem trying to compare two table. The comparism work fine when i choose two colums BuT if i choose more then two it gives me a different solution
Let say i have these tables:
C#
T1
[Products]  [Area]   [Price]   [Finance]
Apple        20        500        BA
Apple        20        200        LA
Apple        20        NULL       BA
Orange       10        50         L6
Kiwi         31        250        ZS
Kiwi         31        150        BA
Mango        14        25         F


C#
T2
[Products]  [Area]   [Price]   [Finance]
Cherry       02        221        LB
Cherry       02        551        LB
Cherry       02        75         LA
Apple        20        500        BA
Blueberry    15        125        ZS
Blueberry    15        135        F
Kiwi         31        150        BA
Kiwi         31        250        ZS
Banana       50        255        L6
Banana       50        355        L6
Banana       50        121        BA
Mango        14         25        F


Now i wanna compare and get the results of data which are in T2 BUT not in T1
Something like:
C#
SELECT Products,Price,Finance FROM T2
EXCEPT
SELECT Products,Price,Finance FROM T1

Alternative
SQL
SELECT DISTINCT Products,Price,Finance FROM T2
EXCEPT
SELECT DISTINCT Products,Price,Finance FROM T1


I wanna have a result like this:

C#
[Products]        [Price]   [Finance]
Cherry              221        LB
Cherry              551        LB
Cherry              75         LA
Blueberry           125        ZS
Blueberry           135        F
Banana              255        L6
Banana              355        L6
Banana              121        BA


Can somebody please help?
Posted
Updated 30-Oct-14 4:36am
v3
Comments
Maciej Los 30-Oct-14 16:29pm    
I have no idea what you mean b saying "compare ... if i choose more than 2 columns"...
mikybrain1 30-Oct-14 16:42pm    
As i described in my question. i have two tables and wanna know whats new in T2. i wanna get something like the result table.
Would be helpful if u can help
Maciej Los 30-Oct-14 16:44pm    
Is there any PK[^]?
mikybrain1 30-Oct-14 16:51pm    
Unfortunately no PK
I need the new products in T2 and it coresponding prices and finances
Maciej Los 30-Oct-14 17:16pm    
Please, see my answer ;)

I think you could use something like

SQL
select key from t2 where key not in (select key from t1)


now, you'll note Ive used 'key' .. iirc, when I first started sql and had to do something like this, I think I basically concatenated the column values together to form a single string... so, I'd try something like (assuming they are string columns or you can stringify them)

SQL
select products+price+finance as key from t2 where key not in (select products+price+finance from t1);


nb if your sql variant doesn't support '+' for string concatenation, you might need to try 'concat' function

you may be able to do the same thing without concatenating the fields together - I cant try it out right now sorry
 
Share this answer
 
Comments
mikybrain1 30-Oct-14 9:27am    
hi! It wasn' helpful
[no name] 30-Oct-14 11:35am    
Why not? Do you recognized the meaning of "key"?
mikybrain1 30-Oct-14 10:22am    
Any suggestions? Please
First of all, your query looks OK.

Here is what i tested:
SQL
DECLARE @T1 TABLE ([Products] VARCHAR(30), [Area] VARCHAR(3), [Price] INT, [Finance] VARCHAR(5))

INSERT INTO @T1([Products], [Area], [Price], [Finance])
VALUES('Apple', '20', 500, 'BA'),
('Apple', '20', 200, 'LA'),
('Apple', '20', NULL, 'BA'),
('Orange', '10', 50, 'L6'),
('Kiwi', '31', 250, 'ZS'),
('Kiwi', '31', 150, 'BA'),
('Mango', '14', 25, 'F')

DECLARE @T2 TABLE ([Products] VARCHAR(30), [Area] VARCHAR(3), [Price] INT, [Finance] VARCHAR(5))
INSERT INTO @T2 ([Products], [Area], [Price], [Finance])
VALUES('Cherry', '02', 221, 'LB'),
('Cherry', '02', 551, 'LB'),
('Cherry', '02', 75, 'LA'),
('Apple', '20', 500, 'BA'),
('Blueberry', '15', 125, 'ZS'),
('Blueberry', '15', 135, 'F'),
('Kiwi', '31', 150, 'BA'),
('Kiwi', '31', 250, 'ZS'),
('Banana', '50', 255, 'L6'),
('Banana', '50', 355, 'L6'),
('Banana', '50', 121, 'BA'),
('Mango', '14', 25, 'F')

SELECT *
FROM (
	SELECT *
	FROM @T2  
	EXCEPT
	SELECT *
	FROM @T1 
) AS T
ORDER BY T.Products DESC


Result:
Products	Area	Price	Finance
Cherry		02		75		LA
Cherry		02		221		LB
Cherry		02		551		LB
Blueberry	15		125		ZS
Blueberry	15		135		F
Banana		50		121		BA
Banana		50		255		L6
Banana		50		355		L6


Returns exactly what you want.
 
Share this answer
 
Comments
mikybrain1 30-Oct-14 17:25pm    
ok thanks dude am gonna check it out.
Garth J Lancaster 30-Oct-14 19:03pm    
d'oh - I didn't think to check if his variant of SQL actually had an 'except' clause

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900