
October 1st, 2008, 05:15 PM
| | | Stored procedure
Can someone help me get the right data out...
I want to compare last weeks Table Diary9-22 to this weeks Table
Diary9-29 to see if the same PAN (field name) are in both records
How would I write this? | 
October 1st, 2008, 05:25 PM
| | | Re: Stored procedure
Below are a couple methods (the last one requires SQL Server 2005/2008).
Those will give you all PAN values that match in both tables.
Not sure why weekly data is stored in separate tables, a better approach
is to use a single table with date (or week number/year) column.
SELECT PAN
FROM [Diary9-22] AS A
WHERE EXISTS (SELECT *
FROM [Diary9-29] AS B
WHERE B.PAN = A.PAN);
SELECT A.PAN
FROM [Diary9-22] AS A
JOIN [Diary9-29] AS B
ON A.PAN = B.PAN;
SELECT PAN
FROM [Diary9-22]
INTERSECT
SELECT PAN
FROM [Diary9-29];
--
Plamen Ratchev http://www.SQLStudio.com | 
October 1st, 2008, 05:45 PM
| | | Re: Stored procedure
On Wed, 1 Oct 2008 09:05:53 -0700 (PDT), JJ297 <nc297@yahoo.com>
wrote: Quote:
>Can someone help me get the right data out...
>
>I want to compare last weeks Table Diary9-22 to this weeks Table
>Diary9-29 to see if the same PAN (field name) are in both records
>
>How would I write this?
| Is PAN a unique column? Or may a value appear more than once?
If it is unique then this will show only the values that are in only
one of the tables.
SELECT A.PAN, B.PAM
FROM [Diary9-22] AS A
FULL OUTER
JOIN [Diary9-29] AS B
ON A.PAN = B.PAN
WHERE A.PAN IS NULL
OR B.PAN IS NULL;
If PAN is not unique you could simply add a DISTINCT to that query,
but depending on performance of that you might want to try applying
DISTINCT to each table before the join process. That could be done in
derived tables, or (in SQL Server 2005 or later) in Common Table
Expressions.
WITH
A AS
(
SELECT DISTINCT PAN
FROM [Diary9-22]
),
B AS
(
SELECT DISTINCT PAN
FROM [Diary9-29]
)
SELECT A.PAN, B.PAM
FROM A
FULL OUTER
JOIN B
ON A.PAN = B.PAN
WHERE A.PAN IS NULL
OR B.PAN IS NULL;
Roy Harvey
Beacon Falls, CT | 
October 1st, 2008, 07:05 PM
| | | Re: Stored procedure
On Oct 1, 12:41*pm, "Roy Harvey (SQL Server MVP)"
<roy_har...@snet.netwrote: Quote:
On Wed, 1 Oct 2008 09:05:53 -0700 (PDT), JJ297 <nc...@yahoo.com>
wrote:
> Quote: |
Can someone help me get the right data out...
| > Quote:
I want to compare last weeks Table Diary9-22 to this weeks Table
Diary9-29 to see if the same PAN (field name) are in both records
| >>
Is PAN a unique column? *Or may a value appear more than once?
>
If it is unique then this will show only the values that are in only
one of the tables.
>
SELECT A.PAN, B.PAM
* FROM [Diary9-22] AS A
* FULL OUTER
* JOIN [Diary9-29] AS B
* * ON A.PAN = B.PAN
*WHERE A.PAN IS NULL
* * OR B.PAN IS NULL;
>
If PAN is not unique you could simply add a DISTINCT to that query,
but depending on performance of that you might want to try applying
DISTINCT to each table before the join process. *That could be done in
derived tables, or (in SQL Server 2005 or later) in Common Table
Expressions.
>
WITH
A AS
(
SELECT DISTINCT PAN
* FROM [Diary9-22]
),
B AS
(
SELECT DISTINCT PAN
* FROM [Diary9-29]
)
SELECT A.PAN, B.PAM
* FROM A
* FULL OUTER
* JOIN B
* * ON A.PAN = B.PAN
*WHERE A.PAN IS NULL
* * OR B.PAN IS NULL;
>
Roy Harvey
Beacon Falls, CT
| I am actually going to put this in SSIS so I want the duplicates to go
to a pending table and the other ones to go to the Cleared Table. I
figured I will get the SQL Statement working first in SQL then I can
get the info out of SSIS. Thanks! | 
October 1st, 2008, 07:15 PM
| | | Re: Stored procedure
On Oct 1, 1:58*pm, JJ297 <nc...@yahoo.comwrote: Quote:
On Oct 1, 12:41*pm, "Roy Harvey (SQL Server MVP)"
>
>
>
>
>
<roy_har...@snet.netwrote: Quote:
On Wed, 1 Oct 2008 09:05:53 -0700 (PDT), JJ297 <nc...@yahoo.com>
wrote:
| > Quote: Quote: |
>Can someone help me get the right data out...
| | > Quote: Quote:
>I want to compare last weeks Table Diary9-22 to this weeks Table
>Diary9-29 to see if the same PAN (field name) are in both records
| | >> Quote: |
Is PAN a unique column? *Or may a value appear more than once?
| > Quote:
If it is unique then this will show only the values that are in only
one of the tables.
| > Quote:
SELECT A.PAN, B.PAM
* FROM [Diary9-22] AS A
* FULL OUTER
* JOIN [Diary9-29] AS B
* * ON A.PAN = B.PAN
*WHERE A.PAN IS NULL
* * OR B.PAN IS NULL;
| > Quote:
If PAN is not unique you could simply add a DISTINCT to that query,
but depending on performance of that you might want to try applying
DISTINCT to each table before the join process. *That could be done in
derived tables, or (in SQL Server 2005 or later) in Common Table
Expressions.
| > Quote:
WITH
A AS
(
SELECT DISTINCT PAN
* FROM [Diary9-22]
),
B AS
(
SELECT DISTINCT PAN
* FROM [Diary9-29]
)
SELECT A.PAN, B.PAM
* FROM A
* FULL OUTER
* JOIN B
* * ON A.PAN = B.PAN
*WHERE A.PAN IS NULL
* * OR B.PAN IS NULL;
| > Quote:
Roy Harvey
Beacon Falls, CT
| >
I am actually going to put this in SSIS so I want the duplicates to go
to a pending table and the other ones to go to the Cleared Table. *I
figured I will get the SQL Statement working first in SQL then I can
get the info out of SSIS. *Thanks!- Hide quoted text -
>
- Show quoted text -
| Okay doesn't look like it is giving me the correct info. The PAN
number is listed one time in each table. I want to combine both
tables and then take out the duplicates and put into a table called
pending. The one's that are only mentioned one time should then go
into a cleared table. Is this possible? | 
October 1st, 2008, 07:25 PM
| | | Re: Stored procedure
To get all duplicates from both tables, you can run a query like this:
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
UNION ALL
SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) 1;
To get the single occurrences is very similar:
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
UNION ALL
SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) = 1;
--
Plamen Ratchev http://www.SQLStudio.com | 
October 1st, 2008, 07:35 PM
| | | Re: Stored procedure
On Oct 1, 2:22*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote: Quote:
To get all duplicates from both tables, you can run a query like this:
>
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
* * * *UNION ALL
* * * *SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) 1;
>
To get the single occurrences is very similar:
>
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
* * * *UNION ALL
* * * *SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) = 1;
>
--
Plamen Ratchevhttp://www.SQLStudio.com
| Thanks that worked! | 
October 1st, 2008, 08:35 PM
| | | Re: Stored procedure
On Oct 1, 2:29*pm, JJ297 <nc...@yahoo.comwrote: Quote:
On Oct 1, 2:22*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote:
>
>
>
>
> Quote: |
To get all duplicates from both tables, you can run a query like this:
| > Quote:
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
* * * *UNION ALL
* * * *SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) 1;
| > Quote: |
To get the single occurrences is very similar:
| > Quote:
SELECT PAN
FROM (SELECT PAN FROM [Diary9-22]
* * * *UNION ALL
* * * *SELECT PAN FROM [Diary9-29]) AS T
GROUP BY PAN
HAVING COUNT(*) = 1;
| > Quote:
--
Plamen Ratchevhttp://www.SQLStudio.com
| >
Thanks that worked!- Hide quoted text -
>
- Show quoted text -
| How do I add this to my stored procedure to get duplicates out
DATEDIFF("dd",Out_DryDte1,GETDATE()) 60 - 12
This isn't working:
SELECT PAN, Out_DryDte1
FROM (SELECT PAN FROM [NewDiary9_22]
UNION ALL
SELECT PAN FROM [NewDiary9_29]) AS T
GROUP BY PAN
HAVING COUNT(*) 1 and (Out_DryDte1,Getdate()) 60 -12
I want to get all of those records who are over 60 days - 12 days from
today's date. Hope that makes sense | 
October 1st, 2008, 08:45 PM
| | | Re: Stored procedure
Try this:
SELECT PAN
FROM (SELECT PAN, Out_DryDte1 FROM [NewDiary9_22]
UNION ALL
SELECT PAN, Out_DryDte1 FROM [NewDiary9_29]) AS T
WHERE Out_DryDte1 < DATEADD(DAY,
DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) -
(60 - 12), 0)
GROUP BY PAN
HAVING COUNT(*) 1
--
Plamen Ratchev http://www.SQLStudio.com | 
October 2nd, 2008, 02:55 PM
| | | Re: Stored procedure
On Oct 1, 12:21*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote: Quote:
Below are a couple methods (the last one requires SQL Server 2005/2008).
Those will give you all PAN values that match in both tables.
>
Not sure why weekly data is stored in separate tables, a better approach
is to use a single table with date (or week number/year) column.
>
SELECT PAN
FROM [Diary9-22] AS A
WHERE EXISTS (SELECT *
* * * * * * * *FROM [Diary9-29] AS B
* * * * * * * *WHERE B.PAN = A.PAN);
>
SELECT A.PAN
FROM [Diary9-22] AS A
JOIN [Diary9-29] AS B
* *ON A.PAN = B.PAN;
>
SELECT PAN
FROM [Diary9-22]
INTERSECT
SELECT PAN
FROM [Diary9-29];
>
--
Plamen Ratchevhttp://www.SQLStudio.com
| Okay thanks will try it now! | 
October 2nd, 2008, 02:55 PM
| | | Re: Stored procedure
On Oct 1, 12:21*pm, Plamen Ratchev <Pla...@SQLStudio.comwrote: Quote:
Below are a couple methods (the last one requires SQL Server 2005/2008).
Those will give you all PAN values that match in both tables.
>
Not sure why weekly data is stored in separate tables, a better approach
is to use a single table with date (or week number/year) column.
>
SELECT PAN
FROM [Diary9-22] AS A
WHERE EXISTS (SELECT *
* * * * * * * *FROM [Diary9-29] AS B
* * * * * * * *WHERE B.PAN = A.PAN);
>
SELECT A.PAN
FROM [Diary9-22] AS A
JOIN [Diary9-29] AS B
* *ON A.PAN = B.PAN;
>
SELECT PAN
FROM [Diary9-22]
INTERSECT
SELECT PAN
FROM [Diary9-29];
>
--
Plamen Ratchevhttp://www.SQLStudio.com
| Oh weekly data is stored because they want to keep a copy of the
weekly files. So I need to join the tables to get the pending and
cleared files. Really I can just look for the cleared records (the
one's that are in Diary9-22 and not in Diary 9-29)
Thanks for the procedures. | 
October 2nd, 2008, 03:25 PM
| | | Re: Stored procedure
You can still keep all weekly data in one table, just need an extra
column to store the week start date. That will make the system a lot
more flexible (and you do not have to create a new table every week).
Right now you have to change your queries every week to reflect the
table name change.
--
Plamen Ratchev http://www.SQLStudio.com | 
October 2nd, 2008, 04:25 PM
| | | Re: Stored procedure
On Oct 2, 10:22*am, Plamen Ratchev <Pla...@SQLStudio.comwrote: Quote:
You can still keep all weekly data in one table, just need an extra
column to store the week start date. That will make the system a lot
more flexible (and you do not have to create a new table every week).
Right now you have to change your queries every week to reflect the
table name change.
>
--
Plamen Ratchevhttp://www.SQLStudio.com
| Yes I would have to change the queries each week. I will set it up
the way you suggested. Thanks again for your help! | 
October 3rd, 2008, 03:15 AM
| | | Re: Stored procedure
"A problem well stated is a problem half solved." -- Charles F.
Kettering
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.
Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html
Your narrative seems to describe a file (records and fields!!) and
tables that mimic weekly tapes from a 1950's style magnetic system.
Your table names even look like classic IBM tape labels based on a
date which violate the basics of RDBMS. We had a "YYDDD" format for
decades before RDBMS. There are some Y2K problems with this,
obviously. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|