corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » SQL Query Issue


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author SQL Query Issue
Neo
Member

Registered: 20th Feb 07
Location: Essex
User status: Offline
25th Aug 11 at 11:43   View User's Profile U2U Member Reply With Quote

Client of mine has an old catering system and no one on site can write queries so foolishly without even looking at it i've said i'll give it a crack, however the system is so locked down and archaic I have to use the built in report designer/wizard/piece of crap. The database is DBISAM and requires DBISAM statements (sub selects are out of the question, as well as many other things !) and the database is written using .dat files.

The parts of the query that i'm having issues with is


SELECT PLU.Name, PLU.PLUNUMBER, SUM(Memberdetail.QUANTITY) AS Quantity
FROM "Membertran.dat" Membertran
INNER JOIN "Memberdetail.dat" Memberdetail ON
(Memberdetail.TRANSACTIONNUMBER=Membertran.TRANSACTIONNUMBER)
INNER JOIN "PLU.dat" PLU ON
(PLU.PLUNUMBER=Memberdetail.PLUNUMBER)
WHERE (Membertran.PERSONID='WHATEVER')
GROUP BY PLU.NAME, PLU.PLUNUMBER


Ignore spelling i'm copying from another computer. I've left out the stuff we don't need

Now the requirement is that the query returns

ALL PLU.Name and PLU.NUMBER records regardless of WHERE expressions
The results of the "quantity" SUM that only apply to PERSONID and to return a 0 if no value is in the database (ISNULL() doesn't work)

What is happening is that when the query is run, it only returns values for the PLU.NAME and PLU.NUMBER where the WHERE clause is true (as you would expect)

Unfortunately I have about 10 of these that the client then wants on one end report, however initially I want to ensure that its possible to do.

Any ideas ?
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
25th Aug 11 at 11:51   View User's Profile U2U Member Reply With Quote

Not sure I fully understand what you are trying to achieve but if there's a table that could potentially have no matches then to still return results you would need a LEFT JOIN instead of an INNER JOIN. Then where there's no match you will get NULL, so your ISNULL will work.
Dom
Member

Registered: 13th Sep 03
User status: Offline
25th Aug 11 at 11:56   View User's Profile U2U Member Reply With Quote

As you said, you need to remove the WHERE condition if you're wanting to fire out every record. As for return a zero instead of NULL use an IFNULL condition on the SELECT, something like
code:
IFNULL(SUM(Memberdetail.QUANTITY) = 'NULL', 0, SUM(Memberdetail.QUANTITY))


As for the 10 queries, can you not run them individually and then dump the data to the single report? Or is it one query only and you're not allowed sub SELECTS?

[Edited on 25-08-2011 by Dom]
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
25th Aug 11 at 12:00   View User's Profile U2U Member Reply With Quote

What about a union? If you can't do sub selects?

Essentially from what I'm understanding, you want all records back, but only to show the calculated quantity on the particular record being searched for.

Therefore, select all BUT the particular record, without calculating quantity.
Then select the single record with quantity calculated and then union it together.

e.g.

SELECT PLU.Name, PLU.PLUNUMBER, 0 AS Quantity
FROM "Membertran.dat" Membertran
INNER JOIN "Memberdetail.dat" Memberdetail ON
(Memberdetail.TRANSACTIONNUMBER=Membertran.TRANSACTIONNUMBER)
INNER JOIN "PLU.dat" PLU ON
(PLU.PLUNUMBER=Memberdetail.PLUNUMBER)
WHERE (Membertran.PERSONID<>'WHATEVER')
UNION
SELECT PLU.Name, PLU.PLUNUMBER, IFNULL(SUM(Memberdetail.QUANTITY) = 'NULL', 0, SUM(Memberdetail.QUANTITY)) AS Quantity
FROM "Membertran.dat" Membertran
INNER JOIN "Memberdetail.dat" Memberdetail ON
(Memberdetail.TRANSACTIONNUMBER=Membertran.TRANSACTIONNUMBER)
INNER JOIN "PLU.dat" PLU ON
(PLU.PLUNUMBER=Memberdetail.PLUNUMBER)
WHERE (Membertran.PERSONID='WHATEVER')
GROUP BY PLU.NAME, PLU.PLUNUMBER

[Edited on 25-08-2011 by Paul_J]
Neo
Member

Registered: 20th Feb 07
Location: Essex
User status: Offline
25th Aug 11 at 12:02   View User's Profile U2U Member Reply With Quote

ISNULL, in any context, returns an error of "DBISAM engine error - Unknown function "ISNULL" found"

I've tried LEFT JOIN, however this doesn't change the returned results regardless of how simplistic the query is

Initially you HAVE to create the query using its wizard that writes the initial SQL, and there is a drop down box to select the joins, if you change it from INNER to anything else it says "This join is not supported in CHURCHASSISTANT"

The hell is CHURCHASSISTANT

Thanks for your input though James, like i said this is some strange system !

Neo
Member

Registered: 20th Feb 07
Location: Essex
User status: Offline
25th Aug 11 at 12:06   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Paul_J
What about a union? If you can't do sub selects?

Essentially from what I'm understanding, you want all records back, but only to show the calculated quantity on the particular record being searched for.

Therefore, select all BUT the particular record, without calculating quantity.
Then select the single record with quantity calculated and then union it together.

e.g.

SELECT PLU.Name, PLU.PLUNUMBER, 0 AS Quantity
FROM "Membertran.dat" Membertran
INNER JOIN "Memberdetail.dat" Memberdetail ON
(Memberdetail.TRANSACTIONNUMBER=Membertran.TRANSACTIONNUMBER)
INNER JOIN "PLU.dat" PLU ON
(PLU.PLUNUMBER=Memberdetail.PLUNUMBER)
WHERE (Membertran.PERSONID<>'WHATEVER')
UNION
SELECT PLU.Name, PLU.PLUNUMBER, IFNULL(SUM(Memberdetail.QUANTITY) = 'NULL', 0, SUM(Memberdetail.QUANTITY)) AS Quantity
FROM "Membertran.dat" Membertran
INNER JOIN "Memberdetail.dat" Memberdetail ON
(Memberdetail.TRANSACTIONNUMBER=Membertran.TRANSACTIONNUMBER)
INNER JOIN "PLU.dat" PLU ON
(PLU.PLUNUMBER=Memberdetail.PLUNUMBER)
WHERE (Membertran.PERSONID='WHATEVER')


Hadn't thought of that Paul, will have a look and see what happens...

Dom - Its essentially the same query however against different PERSONID's, luckily the system allows turns the queries results into a "pipe", then use those pipes to create a final report
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
25th Aug 11 at 12:08   View User's Profile U2U Member Reply With Quote

remember the group on the Sum (I had to edit my post as I didn't copy it from your post properly first of all)
Dom
Member

Registered: 13th Sep 03
User status: Offline
25th Aug 11 at 12:20   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Neo
The hell is CHURCHASSISTANT



Could be http://www.churchassist.com?


As said ISNULL isn't supported, hence why you use "='NULL'" etc Thinking about it i don't think DBISAM supports IFNULL either, but you could use a plain IF condition.

And UNION is a good shout by Paul.
Neo
Member

Registered: 20th Feb 07
Location: Essex
User status: Offline
25th Aug 11 at 12:23   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Dom
quote:
Originally posted by Neo
The hell is CHURCHASSISTANT



Could be http://www.churchassist.com?


As said ISNULL isn't supported, hence why you use "='NULL'" etc Thinking about it i don't think DBISAM supports IFNULL either, but you could use a plain IF condition.

And UNION is a good shout by Paul.


No it's not that, first thing I did was google it.

IFNULL isn't supported your tight

UNION seems to be working, I've got a small issue a bit further on but it seems to be doing the right thing, however it has to do that alongside everything else so just trying to alter it to work. Many thanks Paul I owe you a beer (or BK if I ever make it to another guildford meet!)

I hate SQL at the best of times
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
25th Aug 11 at 12:32   View User's Profile U2U Member Reply With Quote

what about using a case instead of IFNULL / ISNULL?

case when SUM(Memberdetail.QUANTITY) = NULL then 0 else SUM(Memberdetail.QUANTITY) end

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
SQL - pronounciation Sam Geek Day 31 1786
8th Jan 10 at 17:41
by Tom G
 
(my)SQL dudes Dom Geek Day 9 632
3rd May 07 at 23:50
by Dom
 
VB6 problem - strings Ian Geek Day 19 503
12th Mar 08 at 23:30
by Ian
 
Php help? (mysql_num_rows) Whittie Geek Day 17 1507
15th Jan 10 at 14:38
by Dom
 
(My)SQL Gurus - Column Alias and Where Clause issue Dom Geek Day 2 184
30th Mar 11 at 20:52
by Dom
 

Corsa Sport » Message Board » Off Day » Geek Day » SQL Query Issue 28 database queries in 0.0113211 seconds