473,402 Members | 2,064 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,402 software developers and data experts.

sql trouble

bob
Could someone help me get the following SQL statement working?

SELECT
standardgame.gamename,
standardgame.rowteamname,
standardgame.colteamname,
standardgame.dollarvalue,
standardgame.gameid,
standardgame.cutoffdatetime,
standardgame.gametype,
standardgame.gameowner,
(100-COUNT(purchasedsquares.gameid)) AS squaresremaining
FROM standardgame
LEFT OUTER JOIN
purchasedsquares ON standardgame.gameid = purchasedsquares.gameid
where gametype='$gametype' and dollarvalue = '$dollarvalue' and
gameowner = '
GROUP BY standardgame.gameid
order by
CASE squaresremaining WHEN 0 THEN 1 ELSE 0 END ASC,
squaresremaining ASC
The problem is... MySQL doesn't seem to want to let me use
squaresremaining in that case statement since it's not an official
column name. Any idea how I can reference squaresremaining in the case
statement?

Jul 23 '05 #1
2 1356
On 1 Jul 2005 12:47:02 -0700, bo*@coolgroups.com wrote:
Could someone help me get the following SQL statement working?

SELECT
standardgame.gamename,
standardgame.rowteamname,
standardgame.colteamname,
standardgame.dollarvalue,
standardgame.gameid,
standardgame.cutoffdatetime,
standardgame.gametype,
standardgame.gameowner,
(100-COUNT(purchasedsquares.gameid)) AS squaresremaining
FROM standardgame
LEFT OUTER JOIN
purchasedsquares ON standardgame.gameid = purchasedsquares.gameid
where gametype='$gametype' and dollarvalue = '$dollarvalue' and
gameowner = '
GROUP BY standardgame.gameid
order by
CASE squaresremaining WHEN 0 THEN 1 ELSE 0 END ASC,
squaresremaining ASC
The problem is... MySQL doesn't seem to want to let me use
squaresremaining in that case statement since it's not an official
column name. Any idea how I can reference squaresremaining in the case
statement?


Hi Bob,

First, this is a group for MS SQL Server, not for MySQL. The query you
posted won't work in MS SQL for a completely different reason than why
it doesn't work in MySQL (the non-ANSI standard use of GROUP BY with
theoretically unpredictable results).

The logical solution in SQL Server will probably not work on MySQL,
because it contains a non-ANSI compliant construction in the ORDER BY
clause. That's why I'll give you a version that works in SQL Server AND
is within the ANSI standards - you'll have to test it to see if MySQL is
able to handle it. If it isn't, I'd advise you to take this to a MySQL
group instead of here :-)

SELECT s.gamename,
s.rowteamname,
s.colteamname,
s.dollarvalue,
s.gameid,
s.cutoffdatetime,
s.gametype,
s.gameowner,
d.squaresremaining,
CASE d.squaresremaining
WHEN 0 THEN 1
ELSE 0
END AS orderinghelper
FROM standardgame AS s
LEFT OUTER JOIN (SELECT gameid, 100-COUNT(*)
FROM purchasedsquares
GROUP BY gameid) AS d(gameid, squaresremaining)
ON s.gameid = d.gameid
WHERE s.gametype = '$gametype'
AND s.dollarvalue = '$dollarvalue'
AND s.gameowner = '$gameowner' -- Assumption;
-- this part was
-- missing in your post
ORDER BY orderinghelper ASC,
squaresremaining ASC

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
(bo*@coolgroups.com) writes:
SELECT
standardgame.gamename,
standardgame.rowteamname,
standardgame.colteamname,
standardgame.dollarvalue,
standardgame.gameid,
standardgame.cutoffdatetime,
standardgame.gametype,
standardgame.gameowner,
(100-COUNT(purchasedsquares.gameid)) AS squaresremaining
FROM standardgame
LEFT OUTER JOIN
purchasedsquares ON standardgame.gameid = purchasedsquares.gameid
where gametype='$gametype' and dollarvalue = '$dollarvalue' and
gameowner = '
GROUP BY standardgame.gameid
order by
CASE squaresremaining WHEN 0 THEN 1 ELSE 0 END ASC,
squaresremaining ASC
The problem is... MySQL doesn't seem to want to let me use
squaresremaining in that case statement since it's not an official
column name. Any idea how I can reference squaresremaining in the case
statement?


The best way is to wrap the query into a derived table. There is however
a second problem: you cannot group only by gameid. All non-aggregate columns
must be in the GROUP BY clause. A much cleaner solution is to keep just
the core to the derived table, and then join with standardgame again.
This gives us:

SELECT s1.gamename, s1.rowteamname, s1.colteamname, s1.dollarvalue,
s1.gameid, s1.cutoffdatetime, s1.gametype, s1.gameowner,
s2.squaresremaining
FROM standardgame s1
JOIN (SELECT s.gameid, (100 - COUNT(p.gameid)) AS squaresremaining
FROM standardgame s
LEFT JOIN purchasedsquares p ON s.gameid = p.gameid
WHERE gametype='$gametype'
AND dollarvalue = '$dollarvalue'
AND gameowner = '
GROUP BY s.gameid) AS s2
ORDER BY CASE s2.squaresremaining WHEN 0 THEN 1 ELSE 0 END ASC,
s2.squaresremaining ASC
(I've also introduced aliases, to make the query less verbose and
more readable.) Note that there is a syntax error just before GROUP
BY, a unclosed string literal.

The above syntax is fine in MS SQL Server. If you are really using
MySQL, I can't tell whether this syntax is good. In theory it should
be, because it's all ANSI compatible. Hm, wait, the CASE expression
in the ORDER BY may not be; you might have to add a column to the
result set with the expression for that.

In any case, this newsgroup is for MS SQL Server, so if you are using
MySQL, you are likely to get better help elsewhere.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

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

Similar topics

2
by: James | last post by:
Hi I am having some trouble getting a asp page to load. Im a noob to the asp side. I have followed knowledege base Article 301305. I am running 2000 adv, IIS 5.0 I have the following...
4
by: Jacek Dziedzic | last post by:
Hi! First of all, I hope my problem is not too loosely tied to the "standard C++" that is the topic of this group. I have some code that exhibits a strange behaviour: on one computer, where I...
0
by: Paul C | last post by:
Hello, everybody. I am having trouble running some of the VS.NET samples, specifically the CarSelector web app, which is very simple. The symptom is that the web controls (drop down listboxes and...
0
by: Alexandre Jaquet | last post by:
Hi does anybody know how to solve my trouble, when I try to create a MS Office project I always got trouble. I can't create office project when I try to create one vs.net restart ... :s
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
1
by: Jim Bancroft | last post by:
Hi everyone, I'm running into a problem with my ASP.Net application. I've just created a new aspx page which uses some new components of mine that inherit from ServicedComponent and are...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
2
by: JLupear | last post by:
I am having trouble with my code again, I had prepared a question and the code to upload, however I am having trouble posting it, are there limits to the amount of lines you can post? I split it...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.