Must have a value in each sectionSQL Query - Ensure a row exists for each value in ()Find outlier values in several tables of dataConditionally joining tablesAggregate Query returning multiple recordsExtract rows based on multiple previous rows' values in SQL ServerCheck sql table for values in another tableHow to display character values for numerical data for many columnsCreating matrix for all dates and all users with counts and categoriesSelect users who have records equal to y but not xHow would you display a pivot table generated by a stored procedure that uses dynamic SQL as an SSRS report if the final number of columns is unknown?
What's the point of writing that I know will never be used or read?
Duplicate and slide edge (rip from boundary)
How do ultra-stable oscillators for spacecraft work?
When was "Fredo" an insult to Italian-Americans?
What should I do with the stock I own if I anticipate there will be a recession?
Have there ever been other TV shows or Films that told a similiar story to the new 90210 show?
Doesn't the speed of light limit imply the same electron can be annihilated twice?
How can I find an old paper when the usual methods fail?
Why do so many people play out of turn on the last lead?
What is the fastest way to level past 95 in Diablo II?
Is the Microsoft recommendation to use C# properties applicable to game development?
Are there liquid fueled rocket boosters having coaxial fuel/oxidizer tanks?
A+ rating still unsecure by Google Chrome's opinion
What is the purpose/function of this power inductor in parallel?
Short comic about alien explorers visiting an abandoned world with giant statues that turn out to be alive but move very slowly
If Marbury v Madison was overturned, would this eliminate Judicial Review in the United States?
What exactly happened to the 18 crew members who were reported as "missing" in "Q Who"?
Airline power sockets shut down when I plug my computer in. How can I avoid that?
Is Fourier series a sampled version of Fourier transform?
What was the intention with the Commodore 128?
What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?
Would molten tin solidify and coat an organic horn?
What are the advantages of this gold finger shape?
Unconventional examples of mathematical modelling
Must have a value in each section
SQL Query - Ensure a row exists for each value in ()Find outlier values in several tables of dataConditionally joining tablesAggregate Query returning multiple recordsExtract rows based on multiple previous rows' values in SQL ServerCheck sql table for values in another tableHow to display character values for numerical data for many columnsCreating matrix for all dates and all users with counts and categoriesSelect users who have records equal to y but not xHow would you display a pivot table generated by a stored procedure that uses dynamic SQL as an SSRS report if the final number of columns is unknown?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Find records where value for supplied categories does not exist
So I have a requirement to produce a query that references data entered by users into a user form. The form is a tick box type and if no value is selected then a tick should be entered that says 'None of the above'. The form is divided into 4 different sections each needing a tick in relevant boxes or if no box is relevant then each section should have a tick in the 'None of the above' box for that section.
Now i am looking at the tables in the background and trying to construct a query that highlights records that do not have a tick in one of the boxes in one or more sections, not even the 'None of the above' box.
essentially we are looking to make sure that the form (each form has its own id) has been populated completely.
For the purposes of this site assume that my data is all in a single table:
Any idea how i can construct this query?
In the example above i would want to identify customer 789 as they are missing a value for section 3
this is a simplified version of what i am trying to do but hopefully will be clear
tsql
add a comment |
Find records where value for supplied categories does not exist
So I have a requirement to produce a query that references data entered by users into a user form. The form is a tick box type and if no value is selected then a tick should be entered that says 'None of the above'. The form is divided into 4 different sections each needing a tick in relevant boxes or if no box is relevant then each section should have a tick in the 'None of the above' box for that section.
Now i am looking at the tables in the background and trying to construct a query that highlights records that do not have a tick in one of the boxes in one or more sections, not even the 'None of the above' box.
essentially we are looking to make sure that the form (each form has its own id) has been populated completely.
For the purposes of this site assume that my data is all in a single table:
Any idea how i can construct this query?
In the example above i would want to identify customer 789 as they are missing a value for section 3
this is a simplified version of what i am trying to do but hopefully will be clear
tsql
add a comment |
Find records where value for supplied categories does not exist
So I have a requirement to produce a query that references data entered by users into a user form. The form is a tick box type and if no value is selected then a tick should be entered that says 'None of the above'. The form is divided into 4 different sections each needing a tick in relevant boxes or if no box is relevant then each section should have a tick in the 'None of the above' box for that section.
Now i am looking at the tables in the background and trying to construct a query that highlights records that do not have a tick in one of the boxes in one or more sections, not even the 'None of the above' box.
essentially we are looking to make sure that the form (each form has its own id) has been populated completely.
For the purposes of this site assume that my data is all in a single table:
Any idea how i can construct this query?
In the example above i would want to identify customer 789 as they are missing a value for section 3
this is a simplified version of what i am trying to do but hopefully will be clear
tsql
Find records where value for supplied categories does not exist
So I have a requirement to produce a query that references data entered by users into a user form. The form is a tick box type and if no value is selected then a tick should be entered that says 'None of the above'. The form is divided into 4 different sections each needing a tick in relevant boxes or if no box is relevant then each section should have a tick in the 'None of the above' box for that section.
Now i am looking at the tables in the background and trying to construct a query that highlights records that do not have a tick in one of the boxes in one or more sections, not even the 'None of the above' box.
essentially we are looking to make sure that the form (each form has its own id) has been populated completely.
For the purposes of this site assume that my data is all in a single table:
Any idea how i can construct this query?
In the example above i would want to identify customer 789 as they are missing a value for section 3
this is a simplified version of what i am trying to do but hopefully will be clear
tsql
tsql
asked Mar 27 at 12:33
distressedinnhsdistressedinnhs
144 bronze badges
144 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Ideally your input form would have validation that would not allow this. However, I realize that may not be under your control. This will work for the simplified example you provided. Hopefully you can modified it to your needs.
The syntax may vary depending on your database platform. This is on SQL Server.
CREATE TABLE #Section (ID INT)
INSERT INTO #Section (ID) VALUES (1)
INSERT INTO #Section (ID) VALUES (2)
INSERT INTO #Section (ID) VALUES (3)
INSERT INTO #Section (ID) VALUES (4)
CREATE TABLE #CustomerResponse (CustomerID INT, FormID INT, SectionID INT, value VARCHAR(25))
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 2, 'none of the above')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 4, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 2, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 3, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 4, '1')
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 1 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 1
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 2 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 2
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 3 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 3
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 4 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 4
WHERE cr2.CustomerID IS null
I do not like this solution, but it does work. Maybe someone will come along with something better.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55377308%2fmust-have-a-value-in-each-section%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
Ideally your input form would have validation that would not allow this. However, I realize that may not be under your control. This will work for the simplified example you provided. Hopefully you can modified it to your needs.
The syntax may vary depending on your database platform. This is on SQL Server.
CREATE TABLE #Section (ID INT)
INSERT INTO #Section (ID) VALUES (1)
INSERT INTO #Section (ID) VALUES (2)
INSERT INTO #Section (ID) VALUES (3)
INSERT INTO #Section (ID) VALUES (4)
CREATE TABLE #CustomerResponse (CustomerID INT, FormID INT, SectionID INT, value VARCHAR(25))
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 2, 'none of the above')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 4, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 2, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 3, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 4, '1')
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 1 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 1
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 2 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 2
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 3 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 3
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 4 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 4
WHERE cr2.CustomerID IS null
I do not like this solution, but it does work. Maybe someone will come along with something better.
add a comment |
Ideally your input form would have validation that would not allow this. However, I realize that may not be under your control. This will work for the simplified example you provided. Hopefully you can modified it to your needs.
The syntax may vary depending on your database platform. This is on SQL Server.
CREATE TABLE #Section (ID INT)
INSERT INTO #Section (ID) VALUES (1)
INSERT INTO #Section (ID) VALUES (2)
INSERT INTO #Section (ID) VALUES (3)
INSERT INTO #Section (ID) VALUES (4)
CREATE TABLE #CustomerResponse (CustomerID INT, FormID INT, SectionID INT, value VARCHAR(25))
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 2, 'none of the above')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 4, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 2, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 3, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 4, '1')
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 1 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 1
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 2 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 2
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 3 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 3
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 4 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 4
WHERE cr2.CustomerID IS null
I do not like this solution, but it does work. Maybe someone will come along with something better.
add a comment |
Ideally your input form would have validation that would not allow this. However, I realize that may not be under your control. This will work for the simplified example you provided. Hopefully you can modified it to your needs.
The syntax may vary depending on your database platform. This is on SQL Server.
CREATE TABLE #Section (ID INT)
INSERT INTO #Section (ID) VALUES (1)
INSERT INTO #Section (ID) VALUES (2)
INSERT INTO #Section (ID) VALUES (3)
INSERT INTO #Section (ID) VALUES (4)
CREATE TABLE #CustomerResponse (CustomerID INT, FormID INT, SectionID INT, value VARCHAR(25))
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 2, 'none of the above')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 4, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 2, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 3, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 4, '1')
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 1 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 1
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 2 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 2
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 3 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 3
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 4 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 4
WHERE cr2.CustomerID IS null
I do not like this solution, but it does work. Maybe someone will come along with something better.
Ideally your input form would have validation that would not allow this. However, I realize that may not be under your control. This will work for the simplified example you provided. Hopefully you can modified it to your needs.
The syntax may vary depending on your database platform. This is on SQL Server.
CREATE TABLE #Section (ID INT)
INSERT INTO #Section (ID) VALUES (1)
INSERT INTO #Section (ID) VALUES (2)
INSERT INTO #Section (ID) VALUES (3)
INSERT INTO #Section (ID) VALUES (4)
CREATE TABLE #CustomerResponse (CustomerID INT, FormID INT, SectionID INT, value VARCHAR(25))
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 2, 'none of the above')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (789, 1, 4, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 1, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 2, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 3, '1')
INSERT INTO #CustomerResponse (CustomerID, FormID, SectionID, value) VALUES (454, 2, 4, '1')
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 1 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 1
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 2 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 2
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 3 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 3
WHERE cr2.CustomerID IS null
UNION
SELECT DISTINCT cr1.CustomerID, cr1.FormID, 4 AS MissingSection FROM #CustomerResponse cr1
LEFT JOIN #CustomerResponse cr2 ON cr1.CustomerID = cr2.CustomerId AND cr2.SectionID = 4
WHERE cr2.CustomerID IS null
I do not like this solution, but it does work. Maybe someone will come along with something better.
answered Mar 27 at 15:04
IsaacIsaac
1,0411 gold badge12 silver badges18 bronze badges
1,0411 gold badge12 silver badges18 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55377308%2fmust-have-a-value-in-each-section%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