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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 selecting

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-18 : 05:16:58
hi guys,

i did the following query:

USE db
SELECT f.number, r.number INTO f_r FROM f, r
WHERE f.number = r.number

Its working, know i want to use UNION function, crossing the data with another table, in order to know which rows are missing in my new table (f_r):

SELECT number FROM f_r
UNION
SELECT number FROM r


IS not working properly, the output that i have in this query is the full rows from 'r' ...

Any idea?

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-18 : 05:28:14
IT IS working properly!
Your inserted data into f_r is coming from an inner join and that means only rows that exist in both f AND R are inserted.

So your SELECT UNION can't give any other result...


Too old to Rock'n'Roll too young to die.
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-18 : 10:09:53
Understand what you mean.

I have the same issue with:



SELECT * INTO FILE_R_WO_MOST_CURRENT_DATE

FROM(


SELECT F.Sif_F,F.NUMBER, F.IDAT FROM F_F AS F
WHERE F.Sif_F='R' AND F.IDAT = (SELECT MAX(IDAT) FROM F_F AS C WHERE F.NUMBER = C.NUMBER)
GROUP BY F.IDAT, F.NUMBER, F.Sif_F

UNION

SELECT A.Sif_F, A.NUMBER_R, A.IDAT FROM F_R AS A
WHERE A.IDAT = F.IDAT


) AS TEMP

The first part of the query is working but whenever i want to cross the tables to get the info that is not the same, I have all the rows...

What can i do?

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-18 : 10:48:02
you should be using EXCEPT instead of UNION to get rows in one table which are not in other table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-18 : 10:51:31
Is not working,

Just blanks:

SELECT F.Sif_F,F.NUMBER, F.IDAT FROM F_F AS F
WHERE F.Sif_F='R' AND F.IDAT = (SELECT MAX(IDAT) FROM F_F AS C WHERE F.NUMBER = C.NUMBER)
GROUP BY F.IDAT, F.NUMBER, F.Sif_F

EXCEPT

SELECT A.Sif_F, A.NUMBER_R, A.IDAT FROM F_R AS A
WHERE A.IDAT = F.IDAT
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-18 : 11:23:23
post some data and explain why its not working

otherwise we cant make out reason without seeing your data or knowing your tables

see guidelines on how to post data below


http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-09-18 : 15:00:09
Just as visakh16 suggested, use except.
The except filters out all records from the second select statement.
I have a feeling that it might work, if you switch the first and second select statement, like this:

SELECT A.Sif_F, A.NUMBER_R, A.IDAT FROM F_R AS A
WHERE A.IDAT = F.IDAT

EXCEPT

SELECT F.Sif_F,F.NUMBER, F.IDAT FROM F_F AS F
WHERE F.Sif_F='R' AND F.IDAT = (SELECT MAX(IDAT) FROM F_F AS C WHERE F.NUMBER = C.NUMBER)
GROUP BY F.IDAT, F.NUMBER, F.Sif_F

Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-20 : 06:10:22
Appreciate it guys.

Thanks a lot for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-20 : 11:20:27
did it work finally?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -