Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / June 2007

Tip: Looking for answers? Try searching our database.

Help with SELECT

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vovan - 13 Jun 2007 21:47 GMT
I have a table with Accounts, let's call it Account.
It has the following fields:
AccountID
AccountName
ParentAccountID

Data in the table:
AccountID                        AccountName
ParentAccountID
1                                        Checking
0
2                                        Savings
0
3                                        Expenses
0
4                                        Fixed Assets
0
5                                        Travel
3
6                                        Computers
4
7                                        Equipment
4

I need to write SELECT which will return data in the following format:

AccountID                        AccountName
ParentAccountID
1                                        Checking
0
2                                        Savings
0
3                                        Expenses
0
5                                               Travel
3
4                                        Fixed Assets
0
6                                                Computers
4
7                                                Equipment
4

How do I do that?
Thank you

vovan
vovan - 13 Jun 2007 21:56 GMT
Unfortunately the format I used to write my question was changed by outlook
express and now it's hard to see the columns.
There are 3 columns AccountID, AccountName, ParentAccountID and 7 rows with
values

vovan

>I have a table with Accounts, let's call it Account.
> It has the following fields:
[quoted text clipped - 27 lines]
>
> vovan
Ko Zaw - 14 Jun 2007 06:00 GMT
> Unfortunately the format I used to write my question was changed by outlook
> express and now it's hard to see the columns.
[quoted text clipped - 34 lines]
>
> > vovan

Let me guess.

SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY Format([Account]![ParentAccountID],"00") & Format([Account]!
[AccountID],"00");

Ko Zaw
Gary Walter - 14 Jun 2007 13:23 GMT
>> Unfortunately the format I used to write my question was changed by
>> outlook
[quoted text clipped - 43 lines]
> ORDER BY Format([Account]![ParentAccountID],"00") & Format([Account]!
> [AccountID],"00");

Hi Ko Zaw,

I could be wrong, but I think using your format
vovan would want

SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY
IIF([ParentAccountID=0,
Format([AccountID],"00"),
Format([ParentAccountID],"00") & Format([AccountID],"00");
Gary Walter - 14 Jun 2007 13:30 GMT
sorry, previous response missing an ending ")"

SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY
IIF([ParentAccountID=0,
Format([AccountID],"00"),
Format([ParentAccountID],"00") & Format([AccountID],"00"));

>>> Unfortunately the format I used to write my question was changed by
>>> outlook
[quoted text clipped - 55 lines]
> Format([AccountID],"00"),
> Format([ParentAccountID],"00") & Format([AccountID],"00");
vovan - 14 Jun 2007 14:22 GMT
Thank you
It's not what I wanted again.
If you have QuickBooks then look at its Accounting List. That list
represents values in the order I want and all values are stored in a single
table

vovan

> sorry, previous response missing an ending ")"
>
[quoted text clipped - 65 lines]
>> Format([AccountID],"00"),
>> Format([ParentAccountID],"00") & Format([AccountID],"00");
Gary Walter - 14 Jun 2007 15:30 GMT
Hi vovan,

I'm sorry I don't have QuickBooks.

I really thought that the query would
sort properly *given the sample data
you showed us.*

So...there are 2 possibilities...

1) your AccountID's can be greater than 99?

if that's the case, then change format to accomodate
larger AccountID's

this should handle any Long AccountID you throw at it:

SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY
IIF([ParentAccountID=0,
Format([AccountID],"0000000000"),
Format([ParentAccountID],"0000000000")
& "/" & Format([AccountID],"0000000000"));

in fact, I would make the order by into a calculated field
and double-check result (and maybe copy here) if wrong.
It should string sort as you wanted.

unless...

2) You maybe have "nested sets,"
    i.e., an acct can have a parent
           and that parent has a parent other 0

for example...

AccountID AccountName   ParentAccountID
1                   Checking                   0
2                   Savings                     0
3                   Expenses                   0
4                   Fixed Assets            0
5                  Travel                         3
6                  Computers                 4
7                  Equipment                 4
8                  someacct                   7

where "someacct" has parent 7,
which has parent 4,
which *then* has parent 0.

If that's the case, then you probably will
have to create a sort table.

One method for this was illustrated by Rob Volk:
http://www.sqlteam.com/item.asp?ItemID=8866

Please respond back if that is what you need
and you need further assistance.

good luck,

gary

> It's not what I wanted again.
> If you have QuickBooks then look at its Accounting List. That list
[quoted text clipped - 72 lines]
>>> Format([AccountID],"00"),
>>> Format([ParentAccountID],"00") & Format([AccountID],"00");
vovan - 14 Jun 2007 16:48 GMT
Another way to understand what I want it to assume the data in expanded
TreeView:
ParentRecord
   ChildRecord
   AnotherChildRecord
       GrandChildRecord
AnotherParentRecordWithNoChildren
AnotherParentRecord
   ChildRecord
AnotherParentRecord
   ChildRecord
AnotherParentRecordWithNoChildren
AnotherParentRecordWithNoChildren

and so on

vovan

> Hi vovan,
>
[quoted text clipped - 136 lines]
>>>> Format([AccountID],"00"),
>>>> Format([ParentAccountID],"00") & Format([AccountID],"00");
Gary Walter - 14 Jun 2007 17:45 GMT
So you have an "adjacency model" (as in #2 of reply)
and SQL solution usually involves non-trivial translation
to "nested sets"

Spend the day searching Google Groups on

"Joe Celko Tree Structures nested sets"

or did you look at Volk solution?

create a table (say "tblOrg")

ID                     Text(10) pk
Parent               Text(10)   (allow Null, i.e., Required = No)
AccountName  Text(255)
Depth               Long  (allow Null)
Lineage            Text(255) (allow Null)

Create an append query (say "qryGetData")
that gets AccountID, ParentAccountID, and AccountName from your
Account table and fills tblOrg, but converts ID and
Parent to "0 justified" text in the process.

ID: Right("0000000000" & Account.AccountID, 10)
Parent: Right("0000000000" & Account.ParentAccountID, 10)

Then create 2 queries:

qryAddRoot:

INSERT INTO tblOrg (ID, Parent, Position, Depth,Lineage)
VALUES ('0000000000',Null,0,0,'/')

qryOnePass:

UPDATE tblOrg AS T INNER JOIN tblOrg AS P
ON T.Parent = P.ID
SET T.Depth = [P].[Depth]+1,
T.Lineage = [P].[Lineage] & [T].[Parent] & '/'
WHERE
(((P.Depth)>=0)
AND
((P.Lineage) Is Not Null)
AND
((T.Depth) Is Null));

When you want to run your report

'*** aircode***
Dim db As DAO.Database

Set db = CurrentDb

'clear tblOrg
db.Execute "DELETE * FROM tblOrg", dbFailOnError

'execute your append query
db.Execute "qryGetData", dbFailOnError

'add root
db.Execute "qryAddRoot", dbFailOnError

'make passes until all have Depth and Lineage
Do While Dcount("*","tblOrg","[Depth] Is Null") > 0
   db.Execute "qryOnePass", dbFailOnError
Loop

db.Close

'then open report sorted by Depth and Lineage.
'if you need more fields from Account for report
'recordsource, join tblOrgs to Account

'*** end aircode***

The Text(10) should allow you to go to a depth
of at least 22.

Like I said, it is non-trivial, but I have used
successfully on several ocassions...

good luck,

gary

> Another way to understand what I want it to assume the data in expanded
> TreeView:
[quoted text clipped - 155 lines]
>>>>> Format([AccountID],"00"),
>>>>> Format([ParentAccountID],"00") & Format([AccountID],"00");
Gary Walter - 14 Jun 2007 18:58 GMT
sorry, erroneously cut-and-pasted from an earlier post...

qryAddRoot should be:

INSERT INTO tblOrg (ID, Parent, AccountName, Depth,Lineage)
VALUES ('0000000000',Null,0,0,'/')

> So you have an "adjacency model" (as in #2 of reply)
> and SQL solution usually involves non-trivial translation
[quoted text clipped - 242 lines]
>>>>>> Format([AccountID],"00"),
>>>>>> Format([ParentAccountID],"00") & Format([AccountID],"00");
Bamar - 15 Jun 2007 04:28 GMT
> sorry, erroneously cut-and-pasted from an earlier post...
>
[quoted text clipped - 253 lines]
> >>>>>> Format([AccountID],"00"),
> >>>>>> Format([ParentAccountID],"00") & Format([AccountID],"00");

Hi every body!,

All are going with more and more complicated. Gone out the range of my
understanding.
Perhaps, vovan just want sorting with parentAccountID and then
accountID in background, and hide the sorting column.
This should be just the sql, run check at your end. If vovan's maximum
AccountID and parentAccountID is 99, format should 00, or is 999,
format should be 000. Thalt's all.

SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY Format([Account]![ParentAccountID],"00") & Format([Account]!
[AccountID],"00");

dear vovan, pleased respond your result.
_________________________________
Gary Walter - 15 Jun 2007 07:47 GMT
lex parsimoniae

Occam's razor --
All things being equal, the simplest solution
tends to be the best one.

Bamar's razor --
All things that are too complicated to understand easily
must be wrong.

> Hi every body!,
>
[quoted text clipped - 13 lines]
> dear vovan, pleased respond your result.
> _________________________________
Kozaw - 15 Jun 2007 09:41 GMT
> lex parsimoniae
>
[quoted text clipped - 23 lines]
> > dear vovan, pleased respond your result.
> > _________________________________

Sorry. Don't misunderstand on me.
Should Simple coding with same result be the goal?
___________________________________________
Gary Walter - 15 Jun 2007 13:25 GMT
>> > _________________________________
>
> Sorry. Don't misunderstand on me.
> Should Simple coding with same result be the goal?
> ___________________________________________

I apologise for being a little snarky, but I
do not believe you will get "same result."

I believe vovan should have given us some
sample data like:

AccountID AccountName   ParentAccountID
1                   Checking                   0
2                   Savings                     0
3                   Expenses                   0
4                   Fixed Assets            0
5                  Travel                         3
6                  Computers                 4
7                  Equipment                 4
8                  Printers                      7
9                  Fax                             7
10                Laser                          8
11                InkJet                         8
12                Rent                           3
13                MainOffice              12
14                BranchOffice           12
15               Copiers                       7

Save the above to a table Account
and run your query to see how it works.

This is what I think vovan wants (I could
be wrong):

{accts are indented to show nesting}

AccountID AccountName ParentAccountID
1                Checking                0
2                Savings                   0
3                Expenses                0
5                     Travel                3
12                    Rent                  3
13                       MainOffice    12
14                       BranchOffice 12
4                Fixed Assets            0
6                    Computers           4
7                    Equipment            4
8                          Printers           7
10                             Laser         8
11                              InkJet       8
9                          Fax                7
15                        Copiers          7

If we knew *for sure* there was only
this deep level of nesting, we could just
use 4 instances of table to sort, i.e.,

{formatting assumes AccountID<=9999}

SELECT
Parent.AccountID,
Parent.AccountName,
Parent.ParentAccountID
FROM
((Account AS Parent
LEFT JOIN
Account AS Child
ON
Parent.ParentAccountID = Child.AccountID)
LEFT JOIN
Account AS GrandChild
ON
Child.ParentAccountID = GrandChild.AccountID)
LEFT JOIN
Account AS GreatGrandChild
ON
GrandChild.ParentAccountID = GreatGrandChild.AccountID
ORDER BY
Format([GreatGrandChild].[ParentAccountID],"0000")
&
Format([GrandChild].[ParentAccountID],"0000")
&
Format([Child].[ParentAccountID],"0000")
&
Format([Parent].[ParentAccountID],"0000")
&
Format([Parent].[AccountID],"0000");

What if there is one more level of nesting?
Just add another "GreatGreatGrandChild"
instance of the table?

Usually it best not to assume level of nesting
and go for a more general approach.

Here be more step-by-step so maybe you can
follow along and understand Volk's method
better (I'm going to assume you have a test db
with the Account table and data above -- if not,
then how do you learn?)

create a table (say "tblOrg")

ID                     Text(10) pk
Parent               Text(10)   (allow Null, i.e., Required = No)
AccountName  Text(255)
Depth               Long  (allow Null)
Lineage            Text(255) (allow Null)

to easily create it, just run following query:

CREATE TABLE tblOrg
(ID TEXT(10) NOT NULL CONSTRAINT PK_ID PRIMARY KEY,
Parent TEXT(10),
AccountName TEXT(255),
Depth Long,
Lineage TEXT(255));

Create an append query (say "qryGetData")
that gets AccountID, ParentAccountID, and AccountName from your
Account table and fills tblOrg, but converts ID and
Parent to "0 justified" text in the process.

qryGetData:

INSERT INTO tblOrg ( ID, AccountName, Parent )
SELECT
Format([AccountID],"0000000000") AS ID,
Account.AccountName,
Format([ParentAccountID],"0000000000") AS Parent
FROM Account;

tblOrg should then look like:

ID                Parent         AccountName Depth Lineage
0000000001 0000000000 Checking
0000000002 0000000000 Savings
0000000003 0000000000 Expenses
0000000004 0000000000 Fixed Assets
0000000005 0000000003 Travel
0000000006 0000000004 Computers
0000000007 0000000004 Equipment
0000000008 0000000007 Printers
0000000009 0000000007 Fax
0000000010 0000000008 Laser
0000000011 0000000008 InkJet
0000000012 0000000003 Rent
0000000013 0000000012 MainOffice
0000000014 0000000012 BranchOffice
0000000015 0000000007 Copiers

then create query to add "root"

qryAddRoot:

INSERT INTO tblOrg (ID, Parent, AccountName, Depth,Lineage)
VALUES ('0000000000', Null, 'ROOT',0,'/')

tblOrg should then look like:

ID                   Parent         AccountName Depth Lineage
0000000000                      ROOT             0        /
0000000001 0000000000 Checking
0000000002 0000000000 Savings
0000000003 0000000000 Expenses
0000000004 0000000000 Fixed Assets
0000000005 0000000003 Travel
0000000006 0000000004 Computers
0000000007 0000000004 Equipment
0000000008 0000000007 Printers
0000000009 0000000007 Fax
0000000010 0000000008 Laser
0000000011 0000000008 InkJet
0000000012 0000000003 Rent
0000000013 0000000012 MainOffice
0000000014 0000000012 BranchOffice
0000000015 0000000007 Copiers

then create following query...

qryOnePass:

UPDATE tblOrg AS T INNER JOIN tblOrg AS P
ON T.Parent = P.ID
SET T.Depth = [P].[Depth]+1,
T.Lineage = [P].[Lineage] & [T].[Parent] & '/'
WHERE
(((P.Depth)>=0)
AND
((P.Lineage) Is Not Null)
AND
((T.Depth) Is Null));

the first time you run it, it will update 4 rows:

ID                   Parent         AccountName Depth Lineage
0000000000                      ROOT             0        /
0000000001 0000000000 Checking          1       /0000000000/
0000000002 0000000000 Savings             1       /0000000000/
0000000003 0000000000 Expenses          1       /0000000000/
0000000004 0000000000 Fixed Assets     1       /0000000000/
0000000005 0000000003 Travel
0000000006 0000000004 Computers
0000000007 0000000004 Equipment
0000000008 0000000007 Printers
0000000009 0000000007 Fax
0000000010 0000000008 Laser
0000000011 0000000008 InkJet
0000000012 0000000003 Rent
0000000013 0000000012 MainOffice
0000000014 0000000012 BranchOffice
0000000015 0000000007 Copiers

the next time you run it, it will update 4 more rows:

ID                   Parent         AccountName Depth Lineage
0000000000                      ROOT          0     /
0000000001 0000000000 Checking      1     /0000000000/
0000000002 0000000000 Savings         1     /0000000000/
0000000003 0000000000 Expense        1     /0000000000/
0000000004 0000000000 Fixed Assets 1     /0000000000/
0000000005 0000000003 Travel           2     /0000000000/0000000003/
0000000006 0000000004 Computers    2     /0000000000/0000000004/
0000000007 0000000004 Equipment     2     /0000000000/0000000004/
0000000008 0000000007 Printers
0000000009 0000000007 Fax
0000000010 0000000008 Laser
0000000011 0000000008 InkJet
0000000012 0000000003 Rent             2     /0000000000/0000000003/
0000000013 0000000012 MainOffice
0000000014 0000000012 BranchOffice
0000000015 0000000007 Copiers

if you continue to run query until no more rows
are updated, the last 3 fields should look like:

AccountName Depth Lineage
ROOT            0 /
Checking        1 /0000000000/
Savings           1 /0000000000/
Expenses        1 /0000000000/
Fixed Assets   1 /0000000000/
Travel            2 /0000000000/0000000003/
Computers    2 /0000000000/0000000004/
Equipment    2 /0000000000/0000000004/
Printers       3 /0000000000/0000000004/0000000007/
Fax            3 /0000000000/0000000004/0000000007/
Laser         4 /0000000000/0000000004/0000000007/0000000008/
InkJet        4 /0000000000/0000000004/0000000007/0000000008/
Rent              2 /0000000000/0000000003/
MainOffice    3 /0000000000/0000000003/0000000012/
BranchOffice 3 /0000000000/0000000003/0000000012/
Copiers         3 /0000000000/0000000004/0000000007/

qryIndentedList:

SELECT
Space([T].[Depth]*4) & [AccountName] AS Acct
FROM tblOrg AS T
ORDER BY
[Lineage] & [ID];

gives:

ROOT
   Checking
   Savings
   Expenses
       Travel
       Rent
           MainOffice
           BranchOffice
   Fixed Assets
       Computers
       Equipment
           Printers
               Laser
               InkJet
           Fax
           Copiers

or what I thought vovan wanted:

SELECT
CLng([ID]) AS AccountID,
tblOrg.AccountName,
CLng([Parent]) AS ParentAccountID
FROM tblOrg
WHERE
tblOrg.AccountName <>'ROOT'
ORDER BY [Lineage] & [ID];

gives:

{indenting added in post}

AccountID AccountName ParentAccountID
1                Checking                0
2                Savings                   0
3                Expenses                0
5                     Travel                3
12                    Rent                  3
13                       MainOffice    12
14                       BranchOffice 12
4                Fixed Assets            0
6                    Computers           4
7                    Equipment            4
8                          Printers           7
10                             Laser         8
11                              InkJet       8
9                          Fax                7
15                        Copiers          7

this method also lends itself to looking
at data in other ways, for example

"subordinates"

what are the subordinate accounts of
Expenses ("0000000003")?

SELECT
CLng([ID]) AS AccountID,
tblOrg.AccountName,
CLng([Parent]) AS ParentAccountID,
tblOrg.Depth
FROM tblOrg
WHERE
tblOrg.Lineage Like '*0000000003*'
ORDER BY [Lineage] & [ID];

Hopefully you can better understand now?

Remember there are other principles besides Occam's Razor...

2 forms of Karl Menger's Law Against Miserliness

"Entities must not be reduced to the point of inadequacy"

and

"It is vain to do with fewer what requires more"

or Kant's "counter-razor":

"The variety of beings should not rashly be diminished."

good luck,

gary
Kozaw - 18 Jun 2007 06:38 GMT
> >> > _________________________________
>
[quoted text clipped - 348 lines]
>
> read more ?

Hi ! I don't know why my last post wasn't shown in time.
And will repost again.

Dear Gary Walter,

Yes, Your sorting might be right.
I simulated it on my end.
Based on your query, I would like to amend as follow.
I created a query named "AccountName_Mixed" with following sql.

SELECT Parent.AccountID, IIf([AcName_4]<>"",
[AcName_4],IIf([AcName_3]<>"",[AcName_3],IIf([AcName_2]<>"",[AcName_2],
[AcName_1]))) AS AccountName, Parent.ParentAccountID, [Parent]!
[AccountName] AS AcName_1, Parent.ParentAccountID AS AcLevel1, IIf(Not
IsNull([AcLevel2]),"    " & [Parent]![AccountName],"") AS AcName_2,
Child.ParentAccountID AS AcLevel2, IIf(Not IsNull([AcLevel3]),"      "
& [Parent]![AccountName],"") AS AcName_3, GrandChild.ParentAccountID
AS AcLevel3, IIf(Not IsNull([AcLevel4]),"        " & [Parent]!
[AccountName],"") AS AcName_4, GreatGrandChild.ParentAccountID AS
AcLevel4
FROM ((Account AS Parent LEFT JOIN Account AS Child ON
Parent.ParentAccountID = Child.AccountID) LEFT JOIN Account AS
GrandChild ON Child.ParentAccountID = GrandChild.AccountID) LEFT JOIN
Account AS GreatGrandChild ON GrandChild.ParentAccountID =
GreatGrandChild.AccountID
ORDER BY Format([GreatGrandChild].[ParentAccountID],"0000") &
Format([GrandChild].[ParentAccountID],"0000") & Format([Child].
[ParentAccountID],"0000") & Format([Parent].[ParentAccountID],"0000")
& Format([Parent].[AccountID],"0000");

And then created another query "AccountName_Indented" with following
sql.

SELECT AccountName_Mixed.AccountID, AccountName_Mixed.AccountName,
AccountName_Mixed.ParentAccountID
FROM AccountName_Mixed;

Would you mind to simulate at your end, for comparing results.
It doesn't need action queries. And will go well as far as IIF gone
crazy.

Thanks for your query and links. It means a lot to me.
Rgds,
Kozaw
Gary Walter - 18 Jun 2007 10:32 GMT
Yes, Your sorting might be right.
I simulated it on my end.
Based on your query, I would like to amend as follow.
I created a query named "AccountName_Mixed" with following sql.

SELECT Parent.AccountID, IIf([AcName_4]<>"",
[AcName_4],IIf([AcName_3]<>"",[AcName_3],IIf([AcName_2]<>"",[AcName_2],
[AcName_1]))) AS AccountName, Parent.ParentAccountID, [Parent]!
[AccountName] AS AcName_1, Parent.ParentAccountID AS AcLevel1, IIf(Not
IsNull([AcLevel2]),"    " & [Parent]![AccountName],"") AS AcName_2,
Child.ParentAccountID AS AcLevel2, IIf(Not IsNull([AcLevel3]),"      "
& [Parent]![AccountName],"") AS AcName_3, GrandChild.ParentAccountID
AS AcLevel3, IIf(Not IsNull([AcLevel4]),"        " & [Parent]!
[AccountName],"") AS AcName_4, GreatGrandChild.ParentAccountID AS
AcLevel4
FROM ((Account AS Parent LEFT JOIN Account AS Child ON
Parent.ParentAccountID = Child.AccountID) LEFT JOIN Account AS
GrandChild ON Child.ParentAccountID = GrandChild.AccountID) LEFT JOIN
Account AS GreatGrandChild ON GrandChild.ParentAccountID =
GreatGrandChild.AccountID
ORDER BY Format([GreatGrandChild].[ParentAccountID],"0000") &
Format([GrandChild].[ParentAccountID],"0000") & Format([Child].
[ParentAccountID],"0000") & Format([Parent].[ParentAccountID],"0000")
& Format([Parent].[AccountID],"0000");

And then created another query "AccountName_Indented" with following
sql.

SELECT AccountName_Mixed.AccountID, AccountName_Mixed.AccountName,
AccountName_Mixed.ParentAccountID
FROM AccountName_Mixed;

Would you mind to simulate at your end, for comparing results.
It doesn't need action queries. And will go well as far as IIF gone
crazy.

Thanks for your query and links. It means a lot to me.
Rgds,
Kozaw

------ response --------

I don't know how you can assume the adjacency table
will be only this deep. What is to keep someone from
saying they are going to branch out the laser printers
into HP and Lexmark (as a silly example)?

AccountID AccountName   ParentAccountID
1                   Checking                   0
2                   Savings                     0
3                   Expenses                   0
4                   Fixed Assets            0
5                  Travel                         3
6                  Computers                 4
7                  Equipment                 4
8                  Printers                      7
9                  Fax                             7
10                Laser                          8
11                InkJet                         8
12                Rent                           3
13                MainOffice              12
14                BranchOffice           12
15               Copiers                       7
16                HP                           10
17                Lexmark                   10

When you find yourself upon a dead horse,
the best advice I can give you is to dismount...

good luck,

gary
aaac@aaac.org - 25 Jun 2007 05:04 GMT
if you need help with SELECT then you shoudl use an Access Data Project

On Jun 15, 6:25 pm, "Gary Walter" <g...@wrotein.msg> wrote:
> >> > _________________________________
>
[quoted text clipped - 348 lines]
>
> read more »

Hi ! I don't know why my last post wasn't shown in time.
And will repost again.

Dear Gary Walter,

Yes, Your sorting might be right.
I simulated it on my end.
Based on your query, I would like to amend as follow.
I created a query named "AccountName_Mixed" with following sql.

SELECT Parent.AccountID, IIf([AcName_4]<>"",
[AcName_4],IIf([AcName_3]<>"",[AcName_3],IIf([AcName_2]<>"",[AcName_2],
[AcName_1]))) AS AccountName, Parent.ParentAccountID, [Parent]!
[AccountName] AS AcName_1, Parent.ParentAccountID AS AcLevel1, IIf(Not
IsNull([AcLevel2]),"    " & [Parent]![AccountName],"") AS AcName_2,
Child.ParentAccountID AS AcLevel2, IIf(Not IsNull([AcLevel3]),"      "
& [Parent]![AccountName],"") AS AcName_3, GrandChild.ParentAccountID
AS AcLevel3, IIf(Not IsNull([AcLevel4]),"        " & [Parent]!
[AccountName],"") AS AcName_4, GreatGrandChild.ParentAccountID AS
AcLevel4
FROM ((Account AS Parent LEFT JOIN Account AS Child ON
Parent.ParentAccountID = Child.AccountID) LEFT JOIN Account AS
GrandChild ON Child.ParentAccountID = GrandChild.AccountID) LEFT JOIN
Account AS GreatGrandChild ON GrandChild.ParentAccountID =
GreatGrandChild.AccountID
ORDER BY Format([GreatGrandChild].[ParentAccountID],"0000") &
Format([GrandChild].[ParentAccountID],"0000") & Format([Child].
[ParentAccountID],"0000") & Format([Parent].[ParentAccountID],"0000")
& Format([Parent].[AccountID],"0000");

And then created another query "AccountName_Indented" with following
sql.

SELECT AccountName_Mixed.AccountID, AccountName_Mixed.AccountName,
AccountName_Mixed.ParentAccountID
FROM AccountName_Mixed;

Would you mind to simulate at your end, for comparing results.
It doesn't need action queries. And will go well as far as IIF gone
crazy.

Thanks for your query and links. It means a lot to me.
Rgds,
Kozaw
KARL DEWEY - 13 Jun 2007 22:07 GMT
Try this --
SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY Account.AccountName;

Signature

KARL DEWEY
Build a little - Test a little

> I have a table with Accounts, let's call it Account.
> It has the following fields:
[quoted text clipped - 43 lines]
>
> vovan
vovan - 13 Jun 2007 22:25 GMT
No, it doesn't work the way I wanted.
I do not need just to order records by AccountName. If you noticed I need
them to be listed in order Parent then all its children, then next parent
with its children. There may be needed self join, or something else.

Anyway, thank you
vovan

> Try this --
> SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
[quoted text clipped - 48 lines]
>>
>> vovan
KARL DEWEY - 13 Jun 2007 23:01 GMT
I try again ---
SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
FROM Account
ORDER BY Account.ParentAccountID, Account.AccountID;

Signature

KARL DEWEY
Build a little - Test a little

> No, it doesn't work the way I wanted.
> I do not need just to order records by AccountName. If you noticed I need
[quoted text clipped - 56 lines]
> >>
> >> vovan
vovan - 13 Jun 2007 23:29 GMT
No, thanks.

vovan

>I try again ---
> SELECT Account.AccountID, Account.AccountName, Account.ParentAccountID
[quoted text clipped - 61 lines]
>> >>
>> >> vovan
KARL DEWEY - 13 Jun 2007 23:55 GMT
I am lost as what your order is to be.  Your post has this --
1 Checking 0
2 Savings 0
3 Expenses 0
5 Travel 3
4 Fixed Assets 0   Seems out of order -- why is this correct?
6 Computers 4
7 Equipment 4

Can you explain some more - maybe with a different set of data.

Signature

KARL DEWEY
Build a little - Test a little

> No, thanks.
>
[quoted text clipped - 65 lines]
> >> >>
> >> >> vovan
vovan - 14 Jun 2007 13:43 GMT
Everything is correct here:
1 Checking 0
2 Savings 0
3 Expenses 0
5 Travel 3
4 Fixed Assets 0   Seems out of order -- why is this correct?
This is because (5 Travel 3) row is a child of the (3 Expense 0) row. It's a
subcategory of Expenses.
The row (4 Fixed Assets 0) comes after because it's a new category after
Expenses category. Remaining 2 rows are subcategories of Fixed Assets
category.
6 Computers 4
7 Equipment 4

>I am lost as what your order is to be.  Your post has this --
> 1 Checking 0
[quoted text clipped - 80 lines]
>> >> >>
>> >> >> vovan
Michel Walsh - 14 Jun 2007 16:16 GMT
Just one level of hierarchy? if so

SELECT *
FROM account
ORDER BY iif(parentAccountID=0, accountID, parentAccountID), a.accountID

should do.

Hoping it may help,
Vanderghast, Access MVP

> Everything is correct here:
> 1 Checking 0
[quoted text clipped - 94 lines]
>>> >> >>
>>> >> >> vovan
KARL DEWEY - 14 Jun 2007 16:41 GMT
I had to use two tables to make it work.
   Account ---
ParentAccountID    AccountID    AccountName
0    1    Checking
0    2    Savings
0    3    Expenses
0    4    Fixed Assets
3    5    Travel

   Account_Sub ---
ParentAccountID    AccountID    AccountName
4    6    Computers
4    7    Equipment

SELECT Account.ParentAccountID, Account.AccountID, Account.AccountName,
Account_Sub.AccountName
FROM Account LEFT JOIN Account_Sub ON Account.AccountID =
Account_Sub.ParentAccountID
ORDER BY Account.ParentAccountID, Account.AccountID;

Signature

KARL DEWEY
Build a little - Test a little

> Everything is correct here:
> 1 Checking 0
[quoted text clipped - 94 lines]
> >> >> >>
> >> >> >> vovan
Gary Walter - 14 Jun 2007 00:06 GMT
I have a table with Accounts, let's call it Account.
It has the following fields:
AccountID
AccountName
ParentAccountID

Data in the table:
AccountID AccountName   ParentAccountID  SortBy
1                   Checking                   0              /01/
2                   Savings                     0              /02/
3                   Expenses                   0             / 03/
4                   Fixed Assets            0              / 04/
5                  Travel                         3          / 03/05/
6                  Computers                 4          / 04/06/
7                  Equipment                 4          / 04/07/

I need to write SELECT which will return data in the following format:

AccountID   AccountName  ParentAccountID
1                       Checking               0
2                        Savings                0
3                       Expenses               0
5                       Travel                    3
4                       Fixed Assets         0
6                        Computers            4
7                       Equipment             4

////////////////////////////////
I added a SortBy field to your table data that
should sort as you want (if I understand correctly)

this can be a calculated field column in your query

Field:   SortBy: IIF(ParentAccountID=0,"/" & Right("00" & [AccountID],2) &
"/", "/" & Right("00" & [ParentAccountID],2) & "/" &  Right("00" &
[AccountID],2) & "/"
Table:
Sort:    Ascending
Show: <checked>
Criteria:
Or:
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.