473,624 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL - identifying missing matches in two tables

Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be
able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qi d` = `responses`.`qi d`
AND `responses`.`ye arid` = 1 AND `responses`.`ui d` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

This is really bugging me. I can seem to make sense of it in my head, but
not able to translate it to SQL
Hope someone can help,

Cheers,
Greg
Jul 19 '05 #1
2 1836
"The Wanderer" <no************ ***@spamdial.pi pex.com> wrote in message
news:bg******** **@newsg2.svr.p ol.co.uk...
Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qi d` = `responses`.`qi d`
AND `responses`.`ye arid` = 1 AND `responses`.`ui d` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

[snip]

I've come a little further with
SELECT *
FROM questions AS q
LEFT JOIN responses AS r1 ON q.qid = r1.qid
JOIN responses AS r2 ON r1.uid = 1 AND r1.uid = r2.uid AND r1.qid = r2.qid
AND r1.yearid = 1 AND r1.yearid = r2.masid
WHERE r1.value IS NULL OR r1.value = '-1'

but because of the join it only returns those values which have -1 in them,
not those with NULL.
I feel like I'm so close, maybe I just need one extra word or condition and
it'll all fall into place.

Greg

Jul 19 '05 #2
"The Wanderer" <no************ ***@spamdial.pi pex.com> wrote in message
news:bg******** **@newsg2.svr.p ol.co.uk...
Hi Everyone,

I'm trying to perform a query on some responses I have stored. I want to be able to add up a score (using SUM() ) but first checking that all the
questions have been answered.

I've tried
SELECT *
FROM `questions`
LEFT JOIN `responses`
ON `questions`.`qi d` = `responses`.`qi d`
AND `responses`.`ye arid` = 1 AND `responses`.`ui d` = 1
WHERE `value` = NULL OR '-1'
ORDER BY value DESC

but it only returns all the rows even if value is '1' those question

a entry for a question is created with a `value` value of -1 when it is
accessed.

In short, I need to be able to identify those question which either don't
have an entry in `responses` for them or have an entry set to `-1`

RESPONSES
+-------+---------+
| Field | Type |
+-------+---------+
| uid | int(11) |
| yearid | int(11) |
| qid | int(11) |
| value | int(1) |
+-------+---------+

QUESTIONS
+----------+--------------+
| Field | Type |
+----------+--------------+
| qid | int(11) |
| qtext | varchar(100) |
+----------+--------------+

[snip]

I've come a little further with
SELECT *
FROM questions AS q
LEFT JOIN responses AS r1 ON q.qid = r1.qid
JOIN responses AS r2 ON r1.uid = 1 AND r1.uid = r2.uid AND r1.qid = r2.qid
AND r1.yearid = 1 AND r1.yearid = r2.masid
WHERE r1.value IS NULL OR r1.value = '-1'

but because of the join it only returns those values which have -1 in them,
not those with NULL.
I feel like I'm so close, maybe I just need one extra word or condition and
it'll all fall into place.

Greg

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1532
by: Dan Bishop | last post by:
I installed Python on the HP 3000 at work today. The interpreter itself appears to be working fine, but "import math", "import datetime", etc. fail with "ImportError: No module named ". (However, os and system can be imported successfully.) I used help('modules') to print a list of available modules and there were only 14 (__builtin__, _codecs, _sre, _symtable, errno, exceptions, gc, imp, marshal, posix, signal, sys, xxsubtype,...
0
688
by: Duncan Smith | last post by:
Hello, I'm not very experienced in SQL and I need some advice. I have a comma separarated values file containing around 20 million records and about 20 fields. There are many missing values (blank). I am only directly interested in 2 fields (1 & 4), but need to use two other fields (2 & 3) for imputing values for Field 1 (if missing). If Field 4 is missing I must delete / ignore the record. The (Python) types are, Field1 (string) ...
3
1910
by: david_0 | last post by:
I have inherited support for a database with many used tables. There isn't any documentation on what is used or dead. I was hoping to run traces and capture the objectid but the property doesn't work that way. Any good ideas would be appreciated.
3
2543
by: wxbuff | last post by:
I have a report based on our product names that consists of two parts. Both insert data into a temporary table. 1. A single grouped set of results based on all products 2. Multiple tables based on individual product names. I am getting data by calling the same stored procedure multiple times... for the single set of data I use "product like '%'" To get the data for individual products, I am using a cursor to parse
2
1993
by: gssstuff | last post by:
I have a piece of code I use to compare two identically structured tables. There are 15+ sets of tables I am comparing. I am looking to see what has changed between the "old" and "new" versions of the table. Any changes get reported to an audit file which contains the index, field name and field value of the records that have changed. I have the changes nailed for modifications, but cannot seem to get a handle on adds/deletes. Due to...
7
2466
by: bill.brennum | last post by:
Hi, Have a number of Access Databases that I inherited and want to zip a few of them. My concern is that other active Microsoft Applications may be linking to the database or its tables. Is there a method of searching that will allow me to identify those MS Access files that are linked to. I am not really concerned with itemizing the which files link to which, but just that a link exists. Thanks in advance for any help.
3
2123
by: Christopher Weaver | last post by:
I want to set a value in a specific field in the current row of a DataSet. This seems like the most basic thing to do but I can't find the syntax for identifying the current row. IOW, I can do this: SomeRow = 'value'; But how do I set SomeRow to the row that the user is currently viewing?
17
3011
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). I have a dataset as follows (1 week to keep it short): Employee 1 - Date 1 Employee 1 - Date 2
2
2134
by: pb648174 | last post by:
I frequently have the problem where I have a list of items to delete in a temp table, such as ProjectId Description ------------- ---------------- 1 test1 2 test4 3 test3 4 test2
1
4357
by: =?Utf-8?B?VGFz?= | last post by:
Hi, first of all, I'm using: Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600) Microsoft .NET Framework Version 2.0.50727 Installed Edition: C# Express. I have added a DataSet to my Form manually and also manually populated it
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.