473,414 Members | 1,667 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,414 software developers and data experts.

Sort in Query

Alireza355
Dear all,

I have created an accounting database with a lot of forms, tables, queries, etc. I want to have a query that gives me the following results:

in the main table of information, there is a column named CREDIT and one named DEBIT. there are also some other columns, one of which is called ORDER.

When there are some numbers in the CREDIT column, the DEBIT is NULL, and when there are some numbers in DEBIT, the CREDIT is NULL.

in this ORDER column, there are some numbers, starting from 1 and then 2 and so on. this column shows the order of entering data in the main table. (The items that have been entered first, have smaller ORDER numbers than the ones entered after them.

And finally, there is a NUMBER column, that has one number in it for each day. for example NUMBER 3 is for May 2nd, NUMBER 4 is for May 3rd, and so on.

I want to have a query that gives me the items in that table, sorted firstly with NUMBER, so that the days are in fact sorted. But the tricky thing is that I want the CREDIT items in each day then the DEBIT items, all sorted by the ORDER column.

Anyone has any idea?
Apr 14 '09 #1
12 2247
MikeTheBike
639 Expert 512MB
@Alireza355
Hi

On the basis that both CREDIT and DEBIT cannot BOTH be null then I suggest something like this:-

In the query designer add this to you field list

Sort_DebitCredit: IIF(IsNull(DEBIT),1,2)

And then sort on the NUMBER field first, then second on the filed entered above and last but not least on the ORDER field.

Hope that is what you wanted?

MTB
Apr 14 '09 #2
NeoPa
32,556 Expert Mod 16PB
The first field to sort by would be [NUMBER] of course, and the last [ORDER]. These are straightforward no-brainers.

To handle Credits all coming before the Debits, we have to be a little more creative.

If we assert that ([CREDIT] IS NULL) then the result will always be one of either TRUE or FALSE. As we know that TRUE evaluates numerically to -1 (all 1 bits) and FALSE to 0 (all 0 bits), sorting on this assertion will necessarily separate out the Credits from the Debits, and ensure that Credits are first.
Expand|Select|Wrap|Line Numbers
  1. ORDER BY [NUMBER],
  2.          ([CREDIT] IS NULL),
  3.          [ORDER]
Apr 14 '09 #3
I have tried this, and it looks to work fine, but a few days ago, for the first time ever, I found a mis-sort (there were some credits, some debits, and again some credits in one day).

Can you please tell me what is wrong with this:
Expand|Select|Wrap|Line Numbers
  1. ORDER BY MainTable.Number, MainTable.Credit>0, MainTable.Order
Thanx
Apr 15 '09 #4
I'm sorry, I forgot to take a look back into my database basics.

Because I needed to do some calculations here, I have put some update queries there to replace the nulls with zeros.

That's why I used >0 there.

using Is Not Null seems to be more effective, but my problem is that I can not use my calculations when one of the fields is null

for example: credit - debit results in null if credit or debit is null.

Now what?!?
Apr 15 '09 #5
NeoPa
32,556 Expert Mod 16PB
You're confusing me here. You seem to be saying the nulls have been replaced by zeroes, then you ask me how to handle the nulls :S

First you need to sort out if you are using nulls, zeroes, or a mixture of both.

The fundamental answer to your question though, is to use the Nz() function where necessary. This will replace null values on the fly with zeroes (or any other value you supply).
Apr 15 '09 #6
NeoPa
32,556 Expert Mod 16PB
@Alireza355
As any Null value would cause the assertion to fail just as a zero value would, I cannot say without seeing the data. I would certainly expect that to work.

Essentially, if a null is found in a numeric assertion it resolves to FALSE.
Expand|Select|Wrap|Line Numbers
  1. (Null>0) == (Null<0) == (Null=0) == FALSE
Apr 15 '09 #7
MikeTheBike
639 Expert 512MB
Hi

By adding a nested IIF() to my previous post (which seems to have been ignored - nothing new here then!) you can cater for all posibilities ie.

Expand|Select|Wrap|Line Numbers
  1. ORDER BY [NUMBERS],  IIf(IsNull([DEBIT]),IIf(IsNull([CREDIT]),3,1),2), [ORDER]

At least I think that is what you want??

Or is this method not considered good practice ?

MTB
Apr 16 '09 #8
NeoPa
32,556 Expert Mod 16PB
@MikeTheBike
It wasn't my intention to ignore your post Mike. I was simply putting forward an alternative with an explanation.
@MikeTheBike
I wouldn't presume to criticise your code, but I generally recommend that code not be added which essentially adds nothing (as you're asking).

Your latest code makes perfect sense, as it results in three possible values (even if logically either [CREDIT] or [DEBIT] should be true but never both). We all know that data stored in this (non-normalised) way is prone to allowing illogical values though.

When a field already has only two possible states, it seems redundant to me to wrap a function call around it to produce two different states, before sorting on it. It's not wrong. It's simply redundant. Such a minor point though, that I wouldn't post just for that. As I was posting anyway though, I did it the way I was happier with.
Apr 16 '09 #9
MikeTheBike
639 Expert 512MB
@NeoPa
I have to say I can only agree with all of that.

Cheers
Apr 16 '09 #10
NeoPa
32,556 Expert Mod 16PB
@MTB :)
@Alireza355
I noticed this earlier but forgot to respond. Got caught up in other stuff.

You are absolutely correct. My code should have read :
Expand|Select|Wrap|Line Numbers
  1. ORDER BY [NUMBER],
  2.          ([CREDIT] IS NOT NULL),
  3.          [ORDER]
Ali, I suspect Mike had the right idea guessing that the reason you had mis-sorts was due to some incorrect values in your data. This would be an illustration of the GIGO law, and can only occur because you have separate fields for [CREDIT] and [DEBIT]. This allows the possibility of an item being both a credit and a debit. Illogical, but only possible because your record design is incorrect.

If you had a single field that contained different values depending on whether the record is a credit or a debit, then not only would this problem go away, but the more logical structure would also mean you would never have needed to post this question in the first place.

I hope this makes sense.
Apr 16 '09 #11
Thanks everyone for your kind support.

About the possibility of a record, having a number in both CREDIT and DEBIT fields, I have made some strict supervising codes and rules in my main form that the user enters data in. IMPOSSIBLE


And the main thing I was missing was using "nz" for my calculations.

Then I used the good and perfectly-working code:
Expand|Select|Wrap|Line Numbers
  1. ORDER BY [NUMBER],
  2.          ([CREDIT] IS NOT NULL),
  3.          [ORDER]
Which is now working perfect.

Thank you all for your kind support and your perfect clues.
Apr 18 '09 #12
NeoPa
32,556 Expert Mod 16PB
@Alireza355
This may well be true, but if I had a pound for every time someone thought that until I showed them the records that didn't fit, then I'd be a wealthy man (well, a little richer than I am at least).

Seriously, proper design of the data structure is so much easier in the long run. In this case, the protection via the main form is very important, so you should be reasonably safe.
@Alireza355
Very pleased to hear it Ali.

Thank you for taking the time to respond and thank the participants.
Apr 18 '09 #13

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

Similar topics

1
by: web developer | last post by:
hi i got a query that takes about 14 mins here it is select BDProduct.ProductCode,BDProduct.ProductName,SALTerritory.TerritoryID...
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
14
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. ...
5
by: Terri | last post by:
The following query will give me the top ten states with the most callers. SELECT TOP 10 Count(Callers.CallerID) AS CountOfCallerID, Callers.State FROM Callers GROUP BY Callers.State ORDER BY...
4
by: Nhmiller | last post by:
This is directly from Access' Help: "About designing a query When you open a query in Design view, or open a form, report, or datasheet and show the Advanced Filter/Sort window (Advanced...
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
7
by: Arnold | last post by:
Greetings Gurus, In a mainform's header, I have a combobox named comboStudents. The rowsource for this combobox is: SELECT -999 As StudentID, "<Add New Student>" As FullName, "aaa" As...
6
by: Chris Zoper | last post by:
Hello, I have a form that shows a lot of records based on a passthrough query to a SQL Server database. I noticed that the Filter and the Sort property of the form do not properly work, often...
15
by: bcochofel | last post by:
Hi, I want to use a variable to sort elements. That var his passed with query string (I'm using Perl CGI to generate XML). Here's a sample of my output:...
3
by: printline | last post by:
Hello All I have a table with some information in the rows on orders placed by my customers. Each column in the table represents some data on the product in the row. What i want to do is make...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
0
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...

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.