Query to Return Missing Data
I have two tables with almost exact structure, but one table always seems to not be updated appropriately. To show the issue, I have created two test tables that illustrate.
Declare @Test1 Table (card4 int, d1 date, amt float)
Declare @Test2 Table (card4 int, d1 date, amt float)
Insert Into @Test1 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/01/2015', '44.12'),
('1111', '01/01/2015', '55.10'),
('2222', '01/01/2015', '23.12')
Insert Into @Test2 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/02/2015', '11.11'),
('4356', '01/01/2015', '44.12')
Select * from @Test1
Select * from @Test2
From the sample data @Test1 is missing the amt
11.11 and I am not sure how to write a query that will pull all values from @Test1
and any values that are missing from @Test2
This is the data set that I want returned
card4 d1 amt
1111 1/1/2015 55.1
2222 1/1/2015 23.12
4356 1/1/2015 12.24
4356 1/1/2015 44.12
4356 1/2/2015 11.11
What would syntax be to achieve this?
sql-server sql-server-2008-r2 t-sql
bumped to the homepage by Community♦ 22 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have two tables with almost exact structure, but one table always seems to not be updated appropriately. To show the issue, I have created two test tables that illustrate.
Declare @Test1 Table (card4 int, d1 date, amt float)
Declare @Test2 Table (card4 int, d1 date, amt float)
Insert Into @Test1 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/01/2015', '44.12'),
('1111', '01/01/2015', '55.10'),
('2222', '01/01/2015', '23.12')
Insert Into @Test2 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/02/2015', '11.11'),
('4356', '01/01/2015', '44.12')
Select * from @Test1
Select * from @Test2
From the sample data @Test1 is missing the amt
11.11 and I am not sure how to write a query that will pull all values from @Test1
and any values that are missing from @Test2
This is the data set that I want returned
card4 d1 amt
1111 1/1/2015 55.1
2222 1/1/2015 23.12
4356 1/1/2015 12.24
4356 1/1/2015 44.12
4356 1/2/2015 11.11
What would syntax be to achieve this?
sql-server sql-server-2008-r2 t-sql
bumped to the homepage by Community♦ 22 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
4
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52
add a comment |
I have two tables with almost exact structure, but one table always seems to not be updated appropriately. To show the issue, I have created two test tables that illustrate.
Declare @Test1 Table (card4 int, d1 date, amt float)
Declare @Test2 Table (card4 int, d1 date, amt float)
Insert Into @Test1 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/01/2015', '44.12'),
('1111', '01/01/2015', '55.10'),
('2222', '01/01/2015', '23.12')
Insert Into @Test2 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/02/2015', '11.11'),
('4356', '01/01/2015', '44.12')
Select * from @Test1
Select * from @Test2
From the sample data @Test1 is missing the amt
11.11 and I am not sure how to write a query that will pull all values from @Test1
and any values that are missing from @Test2
This is the data set that I want returned
card4 d1 amt
1111 1/1/2015 55.1
2222 1/1/2015 23.12
4356 1/1/2015 12.24
4356 1/1/2015 44.12
4356 1/2/2015 11.11
What would syntax be to achieve this?
sql-server sql-server-2008-r2 t-sql
I have two tables with almost exact structure, but one table always seems to not be updated appropriately. To show the issue, I have created two test tables that illustrate.
Declare @Test1 Table (card4 int, d1 date, amt float)
Declare @Test2 Table (card4 int, d1 date, amt float)
Insert Into @Test1 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/01/2015', '44.12'),
('1111', '01/01/2015', '55.10'),
('2222', '01/01/2015', '23.12')
Insert Into @Test2 VALUES
('4356', '01/01/2015', '12.24'),
('4356', '01/02/2015', '11.11'),
('4356', '01/01/2015', '44.12')
Select * from @Test1
Select * from @Test2
From the sample data @Test1 is missing the amt
11.11 and I am not sure how to write a query that will pull all values from @Test1
and any values that are missing from @Test2
This is the data set that I want returned
card4 d1 amt
1111 1/1/2015 55.1
2222 1/1/2015 23.12
4356 1/1/2015 12.24
4356 1/1/2015 44.12
4356 1/2/2015 11.11
What would syntax be to achieve this?
sql-server sql-server-2008-r2 t-sql
sql-server sql-server-2008-r2 t-sql
asked Nov 22 '16 at 19:43
BellHopByDayAmetuerCoderByNighBellHopByDayAmetuerCoderByNigh
430416
430416
bumped to the homepage by Community♦ 22 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 22 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
4
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52
add a comment |
4
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52
4
4
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52
add a comment |
1 Answer
1
active
oldest
votes
You can use SQL's EXCEPT clause.
https://msdn.microsoft.com/en-us/library/ms188055.aspx
Values in the first table (the left query) that are not in the second (the right query) will be shown. You can change the table order to achieve the opposite.
select * from @Test1
except
select * from @Test2
card4 d1 amt
----------- ---------- ----------------------
1111 2015-01-01 55.1
2222 2015-01-01 23.12
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f156060%2fquery-to-return-missing-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use SQL's EXCEPT clause.
https://msdn.microsoft.com/en-us/library/ms188055.aspx
Values in the first table (the left query) that are not in the second (the right query) will be shown. You can change the table order to achieve the opposite.
select * from @Test1
except
select * from @Test2
card4 d1 amt
----------- ---------- ----------------------
1111 2015-01-01 55.1
2222 2015-01-01 23.12
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
add a comment |
You can use SQL's EXCEPT clause.
https://msdn.microsoft.com/en-us/library/ms188055.aspx
Values in the first table (the left query) that are not in the second (the right query) will be shown. You can change the table order to achieve the opposite.
select * from @Test1
except
select * from @Test2
card4 d1 amt
----------- ---------- ----------------------
1111 2015-01-01 55.1
2222 2015-01-01 23.12
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
add a comment |
You can use SQL's EXCEPT clause.
https://msdn.microsoft.com/en-us/library/ms188055.aspx
Values in the first table (the left query) that are not in the second (the right query) will be shown. You can change the table order to achieve the opposite.
select * from @Test1
except
select * from @Test2
card4 d1 amt
----------- ---------- ----------------------
1111 2015-01-01 55.1
2222 2015-01-01 23.12
You can use SQL's EXCEPT clause.
https://msdn.microsoft.com/en-us/library/ms188055.aspx
Values in the first table (the left query) that are not in the second (the right query) will be shown. You can change the table order to achieve the opposite.
select * from @Test1
except
select * from @Test2
card4 d1 amt
----------- ---------- ----------------------
1111 2015-01-01 55.1
2222 2015-01-01 23.12
answered Nov 22 '16 at 19:50
datagoddatagod
5,94843053
5,94843053
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
add a comment |
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
Maybe, I worded my ? incorrectly, I want all non duplicate results to be returned. This seems to return data that is in one table and not the other.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:52
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
I mis-understood the question. I'll leave this here for a bit then vote to delete it.
– datagod
Nov 22 '16 at 20:03
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f156060%2fquery-to-return-missing-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
You can use UNION between your two select statements to answer the question you asked. However, certain issues you mention (near identical tables, missing entries) point to other underlying problems.
– Forrest
Nov 22 '16 at 19:53
@Forrest - Yes there are underlying problems. These were 2 Spreadsheets that were received and imported into SQL that should have been identical...but were not.
– BellHopByDayAmetuerCoderByNigh
Nov 22 '16 at 19:54
Excel problems are myriad. This could be a simple problem with the cell formatting in a certain row. Good luck! The UNION will solve this question's problem, as @Forrest suggested.
– Randolph West
Nov 22 '16 at 20:52