473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select Subquery Filtering out problem

Spazasaurus
I am having trouble. I am not sure if it is not possible or not, but don't know any alternatives.

I am converting my site from PHP and MYSQL to ASP.NET and MSSQL. In my current site. I did a query and then did another query on each record while looping through those results to get what I am trying to do in one query now. I would simply do that same thing but I am not sure how while using Visual Studio and I know that it was bad code, so thought I would fix it.

Here is my current setup:

event table definition in MySQL
+-----------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+----------------+
| ev_id | smallint(6) | | PRI | NULL | auto_increment |
| st_id | smallint(6) | YES | | NULL | |
| re_id | smallint(6) | YES | | NULL | |
| ev_name | varchar(50) | | | | |
| ev_description | text | YES | | NULL | |
| ev_date | date | YES | | NULL | |
| ev_time | time | YES | | NULL | |
| lo_id | smallint(6) | YES | | NULL | |
| lc_id | smallint(6) | YES | | NULL | |
| ev_duration | varchar(10) | YES | | NULL | |
| ev_adultsattended | smallint(4) | YES | | NULL | |
| ev_youthattended | smallint(4) | YES | | NULL | |
| ev_staffattended | smallint(4) | YES | | NULL | |
| ev_residentsattended | smallint(4) | YES | | NULL | |
| ev_volunteersattended | smallint(4) | YES | | NULL | |
+-----------------------+-------------+------+-----+---------+----------------+

eventrating table definition in MySQL
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| ev_id | smallint(6) | | PRI | 0 | |
| st_id | smallint(6) | | PRI | 0 | |
| rlc_id | smallint(6) | YES | | NULL | |
| er_comment | text | YES | | NULL | |
| er_famrating | char(1) | YES | | NULL | |
| er_staffrating | char(1) | YES | | NULL | |
| er_resrating | char(1) | YES | | NULL | |
| er_volrating | char(1) | YES | | NULL | |
| er_repeat | varchar(5) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+

PHP queries to get the data and place in a dropdown box
*** I am using a formcreator class that I built so the code will look a little different, but should be easy enough to follow ***

[PHP]$query = "select DISTINCT e.ev_id, e.ev_name, e.ev_date, s.st_username, s.st_id from event e, staff s, resource r where (e.ev_date >= '$start_date' and e.ev_date <= '$end_date' and s.st_id = e.st_id and r.re_id = e.re_id) ";//and (er.st_id != 1)";// and ".$user_id." <> ANY (select er.st_id from eventrating er) order by e.ev_name";
$result = mysql_query($query);
$select_event_array = array ();
$select_event_count = 0;
if (mysql_num_rows($result))
{
$select_event_array[$select_event_count] = array("label" => "", "value" => "^");
$select_event_count++;
while ($row = mysql_fetch_array($result))
{
$query2 = "select * from eventrating er where er.ev_id = ".$row[0]." and er.st_id = $user_id";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2) == 0)
{
$select_event_array[$select_event_count] = array("label" => $row[1], "value" => $row[0]);
$select_event_count++;
}
}
}
else
{
$select_event_array[$select_event_count] = array("label" => "No Events", "value" => "^");
}
if ($select_event_count < 2)
{
$select_event_array[$select_event_count - 1] = array("label" => "No Events", "value" => "^");
}[/PHP]

So I am trying to do this in mssql. I have converted the tables over and here are the defenitions that I am currently using.
events table
ev_id | smallint <- primary
st_id | smallint
re_id | smallint
ev_name | varchar(50)
ev_description | varchar(489)
ev_date | datetime
ev_time | varchar(12)
lo_id | smallint
lc_id | smallint
ev_duration | varchar(10)
ev_adultsattended | smallint
ev_youtattented | smallint
ev_staffattended | smallint
ev_residentsattended | smallint
ev_volunteersattended | smallint

eventrating table
ev_id | smallint <- primary
st_id | smallint <- primary
rlc_id | smallint
er_comment | varchar(291)
er_famrating | varchar(1)
er_famrating | varchar(1)
er_staffrating | varchar(1)
er_resrating | varchar(1)
er_volrating | varchar(1)
er_repeat | varchar(5)

The staff id in the event table is for the staff member responsible for the event, while the staff id in the eventrating table is for the staff member who is rating the event. There can be many ratings on each event by staff members, but each staff member can only rate each event once.

So, I am try to show all events within a certain date range that have not been rated by the current staff member logged in.

I may not have my database normalized very well and would take suggestions toward getting it in shape as well. I have a lot of limitations based on the fact that I am going to be importing the current data into the new database and don't want to have to convert data as well as much as possible.

Sorry if I have shared too much!

Thanks!
Jan 5 '07 #1
2 1596
almaz
168 Expert 100+
Why do you use resource table in your first query? Can the events.st_id field have NULL values?
I removed resource table as it looks like not used, and assume that events.st_id cannot have NULL values.
The following script should do the right thing:
Expand|Select|Wrap|Line Numbers
  1. select  e.ev_id, e.ev_name, e.ev_date, s.st_username, s.st_id
  2. from    events e inner join staff s on e.st_id = s.st_id
  3. where   e.ev_date between @start_date and @end_date 
  4.     and e.ev_id not in (select ev_id from eventrating where er.st_id = @user_id)
Jan 10 '07 #2
First off, congratulations on being brilliant!

Thank you so much!

Yes, you are right. I didn't need the resource table linked in there at all and am at a miss as to why I had it in there in the first place. Perhaps I should see if my original code is working as I expected it to.

I did get an error with the code you gave, but it was only because referred to an alias (er.st_id) where ther wasn't one, so that was easy to fix.

This is what I ended up with for those who may need it.

Expand|Select|Wrap|Line Numbers
  1. SELECT     e.ev_id, e.ev_name, e.ev_date, s.st_username, s.st_id
  2. FROM         event AS e INNER JOIN
  3.                       staff AS s ON e.st_id = s.st_id
  4. WHERE     (e.ev_date BETWEEN @start_date AND @end_date) AND (e.ev_id NOT IN
  5.                           (SELECT     ev_id
  6.                             FROM          eventrating
  7.                             WHERE      (st_id = @user_id)))
Thank you again almaz. I was actually really close, but I was trying ot link the not in on the event id and the staff id, which would return only the one that had been rated by the staff member or nothing at all if neither were true. Somtimes the brain wanders!
Jan 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
23
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
4
by: Kris Rudin | last post by:
I am displaying a table of information on a web page, using an asp:table that I populate dynamically. On this page I give the user the options to group the rows by certain fields, and/or filter the...
1
by: darkforcesjedi | last post by:
I have a query with a subquery in it. The Subquery JOINS inspection history with another table. The subquery data looks like: Subquery `Q`: ------ ------- 1486 --- 3/10/2006 ---...
5
by: No bother | last post by:
I am using 5.0.26-NT on Windows 2000. I have need to use a reference in the outer from clause in a subquery in the select clause. Consider the following example: Select (select b.baitID from...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.