SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 selecting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

134 Posts

Posted - 09/18/2012 :  05:16:58  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 09/18/2012 :  05:28:14  Show Profile  Visit webfred's Homepage  Reply with Quote
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

134 Posts

Posted - 09/18/2012 :  10:09:53  Show Profile  Reply with Quote
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

India
47189 Posts

Posted - 09/18/2012 :  10:48:02  Show Profile  Reply with Quote
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

134 Posts

Posted - 09/18/2012 :  10:51:31  Show Profile  Reply with Quote
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

India
47189 Posts

Posted - 09/18/2012 :  11:23:23  Show Profile  Reply with Quote
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
Posting Yak Master

Denmark
100 Posts

Posted - 09/18/2012 :  15:00:09  Show Profile  Reply with Quote
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

134 Posts

Posted - 09/20/2012 :  06:10:22  Show Profile  Reply with Quote
Appreciate it guys.

Thanks a lot for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 09/20/2012 :  11:20:27  Show Profile  Reply with Quote
did it work finally?

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.28 seconds. Powered By: Snitz Forums 2000