Archive for the 'subquery' Category

Calculation in SQL Microsoft SQL Server?

I have a small Video rental database and I am supposed to display in percentage the number of videos that are rented out.
So I have the formula
(TotalVideosCheckedOut / TotalVideos)*100
That was the easy part. My problem is I am not sure how I get SQL to do that. I get the totalVideos from the inventory [...]

SQL server help?

I have to turn this into a subquery:
SELECT c.CustomerID, o.OrderDate
FROM orders o RIGHT OUTER JOIN Customers c
ON o.CustomerID = c.CustomerID
WHERE o.OrderDate IS NULL;
But do not know how? I don’t understand why this is not working:
SELECT c.CustomerID, o.OrderDate FROM orders o RIGHT OUTER JOIN Customers c
ON o.CustomerID = c.CustomerID
WHERE EXISTS
( SELECT c.CustomerID, o.OrderDate FROM orders o [...]

sql – to select 5 records of each category?

I have a table which has video_id and cat_id, I would like to select 5 viedeos of each categories. How do I make this query work,
I got this error for my query with sub query, IN and LIMIT
"This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery’ " Please let me know if there [...]

MYSQL and PHP why are my records displaying twice?

View the page and source at:
http://nlmphotography.net/MAIN/news/news.php
[CODE]<?php
header("Content-type: text/xml");
$host = "xxxx";
$user = "xxxx";
$pass = "xxxx";
$database = "xxxx";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT DISTINCT year_id, date FROM news ORDER BY year_id";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml [...]

Pls Help me w/ this SQL statement I wrote, I got stuck..thanks a lot!!?

I need to retrieve Country name and total value of all sales that I have made to that country during 2007 where the total values of sales to that country is under $5,000. Result set should have just one row per country name.
I have a Orders table, OrderDetails (from Northwind db)
I keep getting errors from [...]

SQL 2005–Max of a Max?

I know I should know this, but I’m drawing an absolute blank and don’t have time to figure it out.
A table has a name field, time field, and date field.I need to get the name associated with the max time on the max date and need to do it in one query. This can [...]

SQL 2005–Max of a Max?

I know I should know this, but I’m drawing an absolute blank and don’t have time to figure it out.
A table has a name field, time field, and date field.I need to get the name associated with the max time on the max date and need to do it in one query. This can [...]

Help with MySQL insert query?

I’m having some trouble with an insert using a subquery. I have it half working… my problems start when I try to add information into additional fields that aren’t pulled by the subquery.
What I have right now:
INSERT INTO `phpbb_users`
(user_id, username, user_password, user_regdate, user_email, user_website, user_from, user_avatar)
(SELECT u.id, u.forum_user, u.password, UNIX_TIMESTAMP(u.date_created), u.email, p.web, p.hometown, u.avatar [...]

SQL Sub-queries?

I need to do a correlated subquery to calculate sums.
I have a table similar to this
Group | Number
A | 100
B | 23
A | [...]

PL/SQL Lovers Only Please!!!?

Okay for you SQL/Oracle lovers…I need your help in answering a few questions….I really hope you can help.
1. What is a self join? And why would you use it?
- From what I know, correct me if I am wrong…
A self-join is a query in which a table is joined (compared) to itself. Self-joins are [...]