How can I make a SQL query that returns null if there is no record or return the value if there is?










0















I am trying to do a query on three different tables.




  1. The variable table



    • The variable table carries information about what "area", "rounds" and
      "days" the variable belongs to. The variable table also holds a pk column.
      The pk is used to determine which variable a record belongs to.



  2. The area table



    • The area table carries information about the "name" of the area as well as
      the "role" the area belongs to. A user is assigned a role and then has
      access to specific areas.



  3. The record table



    • The record table carries information about the record that was entered. It
      contains the "value", "alarmed", and "alarmType" columns. You can search
      for a record based on the variable, round and day.


I am trying to query all of the variables in a certain round and day for a user.
I want to display all the variables whether or not there is a record found. Currently I have a query that only returns the variables that have records, but not the ones that don't.



If there is no record then thevalue, alarmed, and alarmType column should be null.



This is the query that I have so far constructed:



SELECT DISTINCT variable.name, area.name AS "areaName", variable.pk, CAST(record.value AS TEXT) AS "value", record.alarmed, record.alarmType 
FROM variable, area, record
WHERE variable.round LIKE '%,1,%'
AND variable.day LIKE '%,3,%'
AND variable.readOnly = 0
AND variable.area IN (SELECT pk
FROM area
WHERE role = (SELECT role
FROM user
WHERE userName LIKE 'Leo'))
AND variable.area = area.pk
AND record.value = (SELECT CASE WHEN COUNT() < 1 THEN NULL
ELSE CAST(value AS TEXT) END
FROM record
WHERE round = 1
AND day = "11-14-2018"
AND variable = variable.pk)
ORDER BY variable.area, variable.position ASC;


Currently it returns something like this:



What this query returns



There are a lot more variables and I want to know how to display them even if there are no records.










share|improve this question
























  • Sounds like you might need a JOIN type of query.

    – Twisty
    Nov 15 '18 at 18:24















0















I am trying to do a query on three different tables.




  1. The variable table



    • The variable table carries information about what "area", "rounds" and
      "days" the variable belongs to. The variable table also holds a pk column.
      The pk is used to determine which variable a record belongs to.



  2. The area table



    • The area table carries information about the "name" of the area as well as
      the "role" the area belongs to. A user is assigned a role and then has
      access to specific areas.



  3. The record table



    • The record table carries information about the record that was entered. It
      contains the "value", "alarmed", and "alarmType" columns. You can search
      for a record based on the variable, round and day.


I am trying to query all of the variables in a certain round and day for a user.
I want to display all the variables whether or not there is a record found. Currently I have a query that only returns the variables that have records, but not the ones that don't.



If there is no record then thevalue, alarmed, and alarmType column should be null.



This is the query that I have so far constructed:



SELECT DISTINCT variable.name, area.name AS "areaName", variable.pk, CAST(record.value AS TEXT) AS "value", record.alarmed, record.alarmType 
FROM variable, area, record
WHERE variable.round LIKE '%,1,%'
AND variable.day LIKE '%,3,%'
AND variable.readOnly = 0
AND variable.area IN (SELECT pk
FROM area
WHERE role = (SELECT role
FROM user
WHERE userName LIKE 'Leo'))
AND variable.area = area.pk
AND record.value = (SELECT CASE WHEN COUNT() < 1 THEN NULL
ELSE CAST(value AS TEXT) END
FROM record
WHERE round = 1
AND day = "11-14-2018"
AND variable = variable.pk)
ORDER BY variable.area, variable.position ASC;


Currently it returns something like this:



What this query returns



There are a lot more variables and I want to know how to display them even if there are no records.










share|improve this question
























  • Sounds like you might need a JOIN type of query.

    – Twisty
    Nov 15 '18 at 18:24













0












0








0








I am trying to do a query on three different tables.




  1. The variable table



    • The variable table carries information about what "area", "rounds" and
      "days" the variable belongs to. The variable table also holds a pk column.
      The pk is used to determine which variable a record belongs to.



  2. The area table



    • The area table carries information about the "name" of the area as well as
      the "role" the area belongs to. A user is assigned a role and then has
      access to specific areas.



  3. The record table



    • The record table carries information about the record that was entered. It
      contains the "value", "alarmed", and "alarmType" columns. You can search
      for a record based on the variable, round and day.


I am trying to query all of the variables in a certain round and day for a user.
I want to display all the variables whether or not there is a record found. Currently I have a query that only returns the variables that have records, but not the ones that don't.



If there is no record then thevalue, alarmed, and alarmType column should be null.



This is the query that I have so far constructed:



SELECT DISTINCT variable.name, area.name AS "areaName", variable.pk, CAST(record.value AS TEXT) AS "value", record.alarmed, record.alarmType 
FROM variable, area, record
WHERE variable.round LIKE '%,1,%'
AND variable.day LIKE '%,3,%'
AND variable.readOnly = 0
AND variable.area IN (SELECT pk
FROM area
WHERE role = (SELECT role
FROM user
WHERE userName LIKE 'Leo'))
AND variable.area = area.pk
AND record.value = (SELECT CASE WHEN COUNT() < 1 THEN NULL
ELSE CAST(value AS TEXT) END
FROM record
WHERE round = 1
AND day = "11-14-2018"
AND variable = variable.pk)
ORDER BY variable.area, variable.position ASC;


Currently it returns something like this:



What this query returns



There are a lot more variables and I want to know how to display them even if there are no records.










share|improve this question
















I am trying to do a query on three different tables.




  1. The variable table



    • The variable table carries information about what "area", "rounds" and
      "days" the variable belongs to. The variable table also holds a pk column.
      The pk is used to determine which variable a record belongs to.



  2. The area table



    • The area table carries information about the "name" of the area as well as
      the "role" the area belongs to. A user is assigned a role and then has
      access to specific areas.



  3. The record table



    • The record table carries information about the record that was entered. It
      contains the "value", "alarmed", and "alarmType" columns. You can search
      for a record based on the variable, round and day.


I am trying to query all of the variables in a certain round and day for a user.
I want to display all the variables whether or not there is a record found. Currently I have a query that only returns the variables that have records, but not the ones that don't.



If there is no record then thevalue, alarmed, and alarmType column should be null.



This is the query that I have so far constructed:



SELECT DISTINCT variable.name, area.name AS "areaName", variable.pk, CAST(record.value AS TEXT) AS "value", record.alarmed, record.alarmType 
FROM variable, area, record
WHERE variable.round LIKE '%,1,%'
AND variable.day LIKE '%,3,%'
AND variable.readOnly = 0
AND variable.area IN (SELECT pk
FROM area
WHERE role = (SELECT role
FROM user
WHERE userName LIKE 'Leo'))
AND variable.area = area.pk
AND record.value = (SELECT CASE WHEN COUNT() < 1 THEN NULL
ELSE CAST(value AS TEXT) END
FROM record
WHERE round = 1
AND day = "11-14-2018"
AND variable = variable.pk)
ORDER BY variable.area, variable.position ASC;


Currently it returns something like this:



What this query returns



There are a lot more variables and I want to know how to display them even if there are no records.







sql sqlite android-sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:24









dwir182

1,445619




1,445619










asked Nov 15 '18 at 18:20









ChrisChris

4018




4018












  • Sounds like you might need a JOIN type of query.

    – Twisty
    Nov 15 '18 at 18:24

















  • Sounds like you might need a JOIN type of query.

    – Twisty
    Nov 15 '18 at 18:24
















Sounds like you might need a JOIN type of query.

– Twisty
Nov 15 '18 at 18:24





Sounds like you might need a JOIN type of query.

– Twisty
Nov 15 '18 at 18:24












1 Answer
1






active

oldest

votes


















2














I think I see what you're trying to do. The key is using joins (specifically OUTER joins) instead of trying to mash all the tables together and then find similarities. There are also LEFT, RIGHT and INNER flavors (read more about these here and here), depending on what you consider the "complete" or "master" data set - the starting point of your query.



Here's how I understand your relationships (let me know if I have this wrong):



record.variable --> variable.pk
variable.area --> area.pk
area.role --> user.role


In your case, you stated that you need all records from the variable table, so I would start with this:



SELECT v.*
FROM variable v;


Then, you might find all the AREA records related to a particular USER. Use an INNER join to find only records that exist on BOTH sides of the join:



SELECT a.*, u.*
FROM area a
INNER JOIN user u -- Define the table to join
ON a.role = u.role -- Which columns contain keys to match on
WHERE u.userName = 'Leo';


The WHERE filter applies to the user table, but because we are ONLY asking for records from the area table that have a match with user, then that limits the results from the area table.



The next step is to join these two extracts together using another INNER join, again, to find the intersection - matches that exist on BOTH sides of the join(s):



SELECT v.*, a.*, u.*
FROM variable v -- New starting point
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo';


Now, we find all the records for a certain day by adding WHERE clauses:



SELECT v.*, a.*, u.*
FROM variable v
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo'
AND v.round = 1 -- Add filters for "round"
AND v.day = '11-14-2018'; -- and "day" columns


Next, we use a LEFT join to give us all the records from the table on the "left" plus any matches we find on the "right" side (the "record" table) or NULL if no match is made:



SELECT v.name
,a.name as "areaName"
,CAST(r.value as TEXT) as "value"
,r.alarmed
,r.alarmType
FROM variable v
INNER JOIN area a
ON v.area = a.pk
INNER JOIN user u
ON a.role = u.role
LEFT JOIN record r -- LEFT is important here
ON v.pk = r.variable
WHERE u.userName = 'Leo'
AND v.round = 1
AND v.day = '11-14-2018'
ORDER BY v.area, v.position;


The result from INNER joins (variable + area + user) becomes the "left" side of this join, and the record becomes the "right" side. Using the LEFT join declares that we want ALL records from the left, whether they have a match on the right or not.



I don't have a dataset to test this with, so please excuse any errors I've made.



Hopefully, this illustrates how joins would be used to both eliminate rows and add data (columns) the result, without having to make individual queries or resort to sub-queries (using IN or EXISTS).






share|improve this answer

























  • Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

    – Chris
    Nov 15 '18 at 19:35











  • @Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

    – kerry
    Nov 16 '18 at 1:32











  • What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

    – dwir182
    Nov 16 '18 at 1:40












  • Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

    – Chris
    Nov 16 '18 at 13:48










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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325664%2fhow-can-i-make-a-sql-query-that-returns-null-if-there-is-no-record-or-return-the%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









2














I think I see what you're trying to do. The key is using joins (specifically OUTER joins) instead of trying to mash all the tables together and then find similarities. There are also LEFT, RIGHT and INNER flavors (read more about these here and here), depending on what you consider the "complete" or "master" data set - the starting point of your query.



Here's how I understand your relationships (let me know if I have this wrong):



record.variable --> variable.pk
variable.area --> area.pk
area.role --> user.role


In your case, you stated that you need all records from the variable table, so I would start with this:



SELECT v.*
FROM variable v;


Then, you might find all the AREA records related to a particular USER. Use an INNER join to find only records that exist on BOTH sides of the join:



SELECT a.*, u.*
FROM area a
INNER JOIN user u -- Define the table to join
ON a.role = u.role -- Which columns contain keys to match on
WHERE u.userName = 'Leo';


The WHERE filter applies to the user table, but because we are ONLY asking for records from the area table that have a match with user, then that limits the results from the area table.



The next step is to join these two extracts together using another INNER join, again, to find the intersection - matches that exist on BOTH sides of the join(s):



SELECT v.*, a.*, u.*
FROM variable v -- New starting point
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo';


Now, we find all the records for a certain day by adding WHERE clauses:



SELECT v.*, a.*, u.*
FROM variable v
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo'
AND v.round = 1 -- Add filters for "round"
AND v.day = '11-14-2018'; -- and "day" columns


Next, we use a LEFT join to give us all the records from the table on the "left" plus any matches we find on the "right" side (the "record" table) or NULL if no match is made:



SELECT v.name
,a.name as "areaName"
,CAST(r.value as TEXT) as "value"
,r.alarmed
,r.alarmType
FROM variable v
INNER JOIN area a
ON v.area = a.pk
INNER JOIN user u
ON a.role = u.role
LEFT JOIN record r -- LEFT is important here
ON v.pk = r.variable
WHERE u.userName = 'Leo'
AND v.round = 1
AND v.day = '11-14-2018'
ORDER BY v.area, v.position;


The result from INNER joins (variable + area + user) becomes the "left" side of this join, and the record becomes the "right" side. Using the LEFT join declares that we want ALL records from the left, whether they have a match on the right or not.



I don't have a dataset to test this with, so please excuse any errors I've made.



Hopefully, this illustrates how joins would be used to both eliminate rows and add data (columns) the result, without having to make individual queries or resort to sub-queries (using IN or EXISTS).






share|improve this answer

























  • Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

    – Chris
    Nov 15 '18 at 19:35











  • @Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

    – kerry
    Nov 16 '18 at 1:32











  • What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

    – dwir182
    Nov 16 '18 at 1:40












  • Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

    – Chris
    Nov 16 '18 at 13:48















2














I think I see what you're trying to do. The key is using joins (specifically OUTER joins) instead of trying to mash all the tables together and then find similarities. There are also LEFT, RIGHT and INNER flavors (read more about these here and here), depending on what you consider the "complete" or "master" data set - the starting point of your query.



Here's how I understand your relationships (let me know if I have this wrong):



record.variable --> variable.pk
variable.area --> area.pk
area.role --> user.role


In your case, you stated that you need all records from the variable table, so I would start with this:



SELECT v.*
FROM variable v;


Then, you might find all the AREA records related to a particular USER. Use an INNER join to find only records that exist on BOTH sides of the join:



SELECT a.*, u.*
FROM area a
INNER JOIN user u -- Define the table to join
ON a.role = u.role -- Which columns contain keys to match on
WHERE u.userName = 'Leo';


The WHERE filter applies to the user table, but because we are ONLY asking for records from the area table that have a match with user, then that limits the results from the area table.



The next step is to join these two extracts together using another INNER join, again, to find the intersection - matches that exist on BOTH sides of the join(s):



SELECT v.*, a.*, u.*
FROM variable v -- New starting point
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo';


Now, we find all the records for a certain day by adding WHERE clauses:



SELECT v.*, a.*, u.*
FROM variable v
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo'
AND v.round = 1 -- Add filters for "round"
AND v.day = '11-14-2018'; -- and "day" columns


Next, we use a LEFT join to give us all the records from the table on the "left" plus any matches we find on the "right" side (the "record" table) or NULL if no match is made:



SELECT v.name
,a.name as "areaName"
,CAST(r.value as TEXT) as "value"
,r.alarmed
,r.alarmType
FROM variable v
INNER JOIN area a
ON v.area = a.pk
INNER JOIN user u
ON a.role = u.role
LEFT JOIN record r -- LEFT is important here
ON v.pk = r.variable
WHERE u.userName = 'Leo'
AND v.round = 1
AND v.day = '11-14-2018'
ORDER BY v.area, v.position;


The result from INNER joins (variable + area + user) becomes the "left" side of this join, and the record becomes the "right" side. Using the LEFT join declares that we want ALL records from the left, whether they have a match on the right or not.



I don't have a dataset to test this with, so please excuse any errors I've made.



Hopefully, this illustrates how joins would be used to both eliminate rows and add data (columns) the result, without having to make individual queries or resort to sub-queries (using IN or EXISTS).






share|improve this answer

























  • Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

    – Chris
    Nov 15 '18 at 19:35











  • @Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

    – kerry
    Nov 16 '18 at 1:32











  • What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

    – dwir182
    Nov 16 '18 at 1:40












  • Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

    – Chris
    Nov 16 '18 at 13:48













2












2








2







I think I see what you're trying to do. The key is using joins (specifically OUTER joins) instead of trying to mash all the tables together and then find similarities. There are also LEFT, RIGHT and INNER flavors (read more about these here and here), depending on what you consider the "complete" or "master" data set - the starting point of your query.



Here's how I understand your relationships (let me know if I have this wrong):



record.variable --> variable.pk
variable.area --> area.pk
area.role --> user.role


In your case, you stated that you need all records from the variable table, so I would start with this:



SELECT v.*
FROM variable v;


Then, you might find all the AREA records related to a particular USER. Use an INNER join to find only records that exist on BOTH sides of the join:



SELECT a.*, u.*
FROM area a
INNER JOIN user u -- Define the table to join
ON a.role = u.role -- Which columns contain keys to match on
WHERE u.userName = 'Leo';


The WHERE filter applies to the user table, but because we are ONLY asking for records from the area table that have a match with user, then that limits the results from the area table.



The next step is to join these two extracts together using another INNER join, again, to find the intersection - matches that exist on BOTH sides of the join(s):



SELECT v.*, a.*, u.*
FROM variable v -- New starting point
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo';


Now, we find all the records for a certain day by adding WHERE clauses:



SELECT v.*, a.*, u.*
FROM variable v
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo'
AND v.round = 1 -- Add filters for "round"
AND v.day = '11-14-2018'; -- and "day" columns


Next, we use a LEFT join to give us all the records from the table on the "left" plus any matches we find on the "right" side (the "record" table) or NULL if no match is made:



SELECT v.name
,a.name as "areaName"
,CAST(r.value as TEXT) as "value"
,r.alarmed
,r.alarmType
FROM variable v
INNER JOIN area a
ON v.area = a.pk
INNER JOIN user u
ON a.role = u.role
LEFT JOIN record r -- LEFT is important here
ON v.pk = r.variable
WHERE u.userName = 'Leo'
AND v.round = 1
AND v.day = '11-14-2018'
ORDER BY v.area, v.position;


The result from INNER joins (variable + area + user) becomes the "left" side of this join, and the record becomes the "right" side. Using the LEFT join declares that we want ALL records from the left, whether they have a match on the right or not.



I don't have a dataset to test this with, so please excuse any errors I've made.



Hopefully, this illustrates how joins would be used to both eliminate rows and add data (columns) the result, without having to make individual queries or resort to sub-queries (using IN or EXISTS).






share|improve this answer















I think I see what you're trying to do. The key is using joins (specifically OUTER joins) instead of trying to mash all the tables together and then find similarities. There are also LEFT, RIGHT and INNER flavors (read more about these here and here), depending on what you consider the "complete" or "master" data set - the starting point of your query.



Here's how I understand your relationships (let me know if I have this wrong):



record.variable --> variable.pk
variable.area --> area.pk
area.role --> user.role


In your case, you stated that you need all records from the variable table, so I would start with this:



SELECT v.*
FROM variable v;


Then, you might find all the AREA records related to a particular USER. Use an INNER join to find only records that exist on BOTH sides of the join:



SELECT a.*, u.*
FROM area a
INNER JOIN user u -- Define the table to join
ON a.role = u.role -- Which columns contain keys to match on
WHERE u.userName = 'Leo';


The WHERE filter applies to the user table, but because we are ONLY asking for records from the area table that have a match with user, then that limits the results from the area table.



The next step is to join these two extracts together using another INNER join, again, to find the intersection - matches that exist on BOTH sides of the join(s):



SELECT v.*, a.*, u.*
FROM variable v -- New starting point
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo';


Now, we find all the records for a certain day by adding WHERE clauses:



SELECT v.*, a.*, u.*
FROM variable v
INNER JOIN area a
ON a.pk = v.area
INNER JOIN user u
ON a.role = u.role
WHERE u.userName = 'Leo'
AND v.round = 1 -- Add filters for "round"
AND v.day = '11-14-2018'; -- and "day" columns


Next, we use a LEFT join to give us all the records from the table on the "left" plus any matches we find on the "right" side (the "record" table) or NULL if no match is made:



SELECT v.name
,a.name as "areaName"
,CAST(r.value as TEXT) as "value"
,r.alarmed
,r.alarmType
FROM variable v
INNER JOIN area a
ON v.area = a.pk
INNER JOIN user u
ON a.role = u.role
LEFT JOIN record r -- LEFT is important here
ON v.pk = r.variable
WHERE u.userName = 'Leo'
AND v.round = 1
AND v.day = '11-14-2018'
ORDER BY v.area, v.position;


The result from INNER joins (variable + area + user) becomes the "left" side of this join, and the record becomes the "right" side. Using the LEFT join declares that we want ALL records from the left, whether they have a match on the right or not.



I don't have a dataset to test this with, so please excuse any errors I've made.



Hopefully, this illustrates how joins would be used to both eliminate rows and add data (columns) the result, without having to make individual queries or resort to sub-queries (using IN or EXISTS).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 1:30

























answered Nov 15 '18 at 19:13









kerrykerry

33228




33228












  • Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

    – Chris
    Nov 15 '18 at 19:35











  • @Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

    – kerry
    Nov 16 '18 at 1:32











  • What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

    – dwir182
    Nov 16 '18 at 1:40












  • Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

    – Chris
    Nov 16 '18 at 13:48

















  • Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

    – Chris
    Nov 15 '18 at 19:35











  • @Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

    – kerry
    Nov 16 '18 at 1:32











  • What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

    – dwir182
    Nov 16 '18 at 1:40












  • Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

    – Chris
    Nov 16 '18 at 13:48
















Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

– Chris
Nov 15 '18 at 19:35





Hello, thank you for your input epluribusunix. I am getting the same results as the query above. I know this can be done as an iteration once we found the variable pk's. I was thinking there are 154 variables. Instead of making 154 queries, we could get all the results in one. For some rounds there may be around 400 variables. What is practical?

– Chris
Nov 15 '18 at 19:35













@Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

– kerry
Nov 16 '18 at 1:32





@Chris, I would definitely try to stay away from making multiple queries. That's a LOT of overhead for something that should be relatively straightforward. I have edited my answer, so please review the data model at the top and let me know what I've missed.

– kerry
Nov 16 '18 at 1:32













What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

– dwir182
Nov 16 '18 at 1:40






What do you mean 154 variable? is it 154 table with same name variable? And this answer are good approach rather you use subqery..

– dwir182
Nov 16 '18 at 1:40














Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

– Chris
Nov 16 '18 at 13:48





Hello, there are 154 variables in this specific round. Variables can have at most one record per round on a specific date. I want to display all the variables with their record. If there is no account of a record I want it to be null.

– Chris
Nov 16 '18 at 13:48



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325664%2fhow-can-i-make-a-sql-query-that-returns-null-if-there-is-no-record-or-return-the%23new-answer', 'question_page');

);

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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands