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 / Reports / Printing / February 2008

Tip: Looking for answers? Try searching our database.

Corstabquery or normal query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Situmorang - 18 Feb 2008 11:11 GMT
Hello,

Maybe I have a langguage problem in expressing my difficulties, but here is
what I wanted

Normal report based on the normal query:

Year 2005
No.     Position     Name
1, Elder            Mr. A
2. clerck            Mr. B
3.  Deacon         Mr. C
Year 2006
1, Elder            Mr. D
2. clerck            Mr. E
3.  Deacon         Mr. F
Year 2007...same way downward

I wanted it that the year is Sideway"

No.     Position     Name    Year 2005   Year 2006        Year 2007
1, Elder                            Mr. A          Mr. D               .......
2. clerck                           Mr. B           Mr. E              
.......
3.  Deacon                        Mr. C           Mr. F             .........

In other words no grouping in rowheading, and only grouping by year for
column heading, and should be able goes side way whenever we have next year
and on.

We need your help

Thanks

Signature

H. Frank Situmorang

John Spencer - 18 Feb 2008 12:55 GMT
What does your data look like?  What tables?  What Fields in the tables?

Do you have one table with fields like - fldYear, fldPosition, and fldName?

Assuming that is the structure, I would first build a query to get the data
and a ranking order for the data (qOrdered).  WARNING: Untested SQL
statements follow - they may have syntax errors or they may give undesired
results.

SELECT Ta.fldYear, Ta.fldPosition, Ta.fldName,
Count(Tb.fldPosition) as RankOrder
FROM [Your Table] as Ta LEFT JOIN [Your Table] as Tb
ON Ta.fldYear=Tb.FldYear AND Ta.fldPosition = Tb.fldPosition
AND Ta.fldName < Tb.fldName
GROUP BY Ta.fldYear, Ta.fldPosition, Ta.fldName

Now I would use the saved query as the source of a crosstab query.

TRANSFORM First(FldName) as theName
SELECT fldPosition, fldRankOrder
FROM qOrdered
GROUP BY  fldPosition, fldRankOrder
PIVOT fldYear

Hope this helps.
Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> Hello,
>
[quoted text clipped - 33 lines]
>
> Thanks
Duane Hookom - 18 Feb 2008 21:30 GMT
John's idea should work. However, do you ever have a year with more than one
person in a position? If so, the crosstab would only return one of the names.

Signature

Duane Hookom
Microsoft Access MVP

> What does your data look like?  What tables?  What Fields in the tables?
>
[quoted text clipped - 58 lines]
> >
> > Thanks
Frank Situmorang - 19 Feb 2008 03:43 GMT
Yes, Duane we have of course, like deacons, we have many deacons in our
church, also church elders, we have many elders, only 1st elder is one person.

So if I could elaborate again that the report now that already works is like
below, but I want it side way per year, because the election committee have
the broad picure of who is more often serving as chruch elder for example, we
can see it from year to year. We can make it in excel manually, but since the
data base that I am building now could be multi functions, so I want to make
something like cross tab query but I have a problem,

Year 2005
No.     Position     Name
1, Elder            Mr. A
2. clerck            Mr. B
3.  Deacon      1.   Mr. C
                      2, Mr. P
                      3. Mr. Q
                      4. Mr. R

Could you please help me on this, my database is alreay 70% complete except
for tutorial, merge

Thanks in advance

Signature

H. Frank Situmorang

> John's idea should work. However, do you ever have a year with more than one
> person in a position? If so, the crosstab would only return one of the names.
[quoted text clipped - 61 lines]
> > >
> > > Thanks
Duane Hookom - 19 Feb 2008 04:17 GMT
You can create the crosstab similar to the suggestion by John. The Value can
be the results of a concatenation using the function that can be found at
http://www.rogersaccesslibrary.com/OtherLibraries.asp#Hookom,Duane. Check out
the Generic Function To Concatenate Child Records.
Signature

Duane Hookom
Microsoft Access MVP

> Yes, Duane we have of course, like deacons, we have many deacons in our
> church, also church elders, we have many elders, only 1st elder is one person.
[quoted text clipped - 85 lines]
> > > >
> > > > Thanks
John Spencer - 19 Feb 2008 13:45 GMT
Duane,
I thought by adding the Ranking that I would get multiple lines for each
position in the Crosstab query.  OR did I miss something with that.

Year Position Name RankOrder
2007 Deacon  Bob   1
2007 Deacon  Tom   2
2007 Deacon Zeara  3
2008 Deacon Bob  1
2008 Deacon Zeara 2

Then  crosstab would return
Position  Rank 2007  2008
Deacon    1     Bob   Bob
Deacon    2     Tom   Zeara
Decaon    3    Zeara  (Null)

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> John's idea should work. However, do you ever have a year with more than
> one
[quoted text clipped - 67 lines]
>> >
>> > Thanks
Frank Situmorang - 25 Feb 2008 02:14 GMT
John,

I want to use your suggestion, but can you explain again on how can we make
the query you said ( SQL) and then how can we make the TRANSFOR  and Pivot. I
appreciate your help on how can we build that query. Normally I can use grid
query and also someone told me in this thread how to make SQL but we must in
in the form first and then in the data record source, we can build the SQL.

Thanks in advance
Signature

H. Frank Situmorang

> Duane,
> I thought by adding the Ranking that I would get multiple lines for each
[quoted text clipped - 84 lines]
> >> >
> >> > Thanks
John Spencer - 25 Feb 2008 14:03 GMT
SELECT Ta.fldYear, Ta.fldPosition, Ta.fldName,
Count(Tb.fldPosition) as RankOrder
FROM [Your Table] as Ta LEFT JOIN [Your Table] as Tb
ON Ta.fldYear=Tb.FldYear AND Ta.fldPosition = Tb.fldPosition
AND Ta.fldName < Tb.fldName
GROUP BY Ta.fldYear, Ta.fldPosition, Ta.fldName

Open a new query
-- Add your table to the query two times (I'll call those two - Table 1 and
Table 2)
-- Drag from the Year field in table 1 to the year field in table 2
-- Drag from the position field in table 1 to the position field in table 2
-- Drag from the Name field in table 1 to the name field in table 2
(That should set up three join lines) between the tables
-- Add the fields year, position, and name from table 1 to the grid
-- Add the position field from table 2 to the grid
-- SELECT View: Totals from the menu
-- Change GROUP BY to Count under table 2 position field
-- SELECT View: SQL View from the menu
In the text that is now visible,
-- Change INNER JOIN to LEFT JOIN
-- Change table1.Name field = Table2.Name Field to
   table1.Name field <  Table2.Name Field

Try to run the query.  If it runs, then save it and use that as the source
of a crosstab query.  IF you have problems with the crosstab query, post
back with any error messages, so someone can help you.

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> John,
>
[quoted text clipped - 103 lines]
>> >> >
>> >> > Thanks
Duane Hookom - 25 Feb 2008 16:07 GMT
John,
I was initially skeptical about being able to create a crosstab based off
your first "totals" query since my usual attempts to do this used a subquery
to create the RankOrder column. I tried your solution against the employees
table in Northwind and it worked. My first query:
=== qselRankTitle ===============
SELECT Employees.Title, Employees.LastName, Count(Employees_1.EmployeeID) AS
RankOrder
FROM Employees LEFT JOIN Employees AS Employees_1 ON Employees.Title =
Employees_1.Title
WHERE (((Employees.EmployeeID)<=[Employees_1].[EmployeeID]))
GROUP BY Employees.Title, Employees.LastName
ORDER BY Employees.Title;
==============================
Then my crosstab
=== qxtbEmployeeTitle ==============
TRANSFORM First(qselRankTitle.LastName) AS FirstOfLastName
SELECT qselRankTitle.Title
FROM qselRankTitle
GROUP BY qselRankTitle.Title
PIVOT qselRankTitle.RankOrder;
===============================

This placed the "names" into individual columns while the solution that I
suggested concatenates the names into a single column.

Thanks for the alternative solution!
Signature

Duane Hookom
Microsoft Access MVP

> SELECT Ta.fldYear, Ta.fldPosition, Ta.fldName,
> Count(Tb.fldPosition) as RankOrder
[quoted text clipped - 131 lines]
> >> >> >
> >> >> > Thanks
Frank Situmorang - 26 Feb 2008 04:25 GMT
John, when I followed step by step procedures like you suggested, I still do
not know how to make it in rank order and how to make a transform....

This is my SQL when I followed your suggestion.
SELECT PelayanJemaat.TahunPel, PelayanJemaat.BidangPelayanan,
PelayanJemaat.NamaPel, Count(PelayanJemaat2.BidangPelayanan) AS
CountOfBidangPelayanan
FROM PelayanJemaat LEFT JOIN PelayanJemaat2 ON
(PelayanJemaat.TahunPel=PelayanJemaat2.TahunPel) AND
(PelayanJemaat.BidangPelayanan=PelayanJemaat2.BidangPelayanan) AND
(PelayanJemaat.NamaPel<PelayanJemaat2.NamaPel)
GROUP BY PelayanJemaat.TahunPel, PelayanJemaat.BidangPelayanan,
PelayanJemaat.NamaPel;

Thanks for anybody's idea to solve this problem
Signature

H. Frank Situmorang

> SELECT Ta.fldYear, Ta.fldPosition, Ta.fldName,
> Count(Tb.fldPosition) as RankOrder
[quoted text clipped - 131 lines]
> >> >> >
> >> >> > Thanks
Duane Hookom - 26 Feb 2008 04:40 GMT
You missed some of John's suggestion. The PelayanJemaat table should be added
to the query twice.

Try something like:
SELECT PelayanJemaat.TahunPel, PelayanJemaat.BidangPelayanan,
PelayanJemaat.NamaPel, Count(B.BidangPelayanan) AS
CountOfBidangPelayanan
FROM PelayanJemaat LEFT JOIN PelayanJemaat B ON
(PelayanJemaat.TahunPel=B.TahunPel) AND
(PelayanJemaat.BidangPelayanan=B.BidangPelayanan) AND
(PelayanJemaat.NamaPel<B.NamaPel)
GROUP BY PelayanJemaat.TahunPel, PelayanJemaat.BidangPelayanan,
PelayanJemaat.NamaPel;

Signature

Duane Hookom
Microsoft Access MVP

> John, when I followed step by step procedures like you suggested, I still do
> not know how to make it in rank order and how to make a transform....
[quoted text clipped - 147 lines]
> > >> >> >
> > >> >> > Thanks
Frank Situmorang - 26 Feb 2008 05:15 GMT
Thanks Duane, the query now works now, but how can we make it into
crosstabquery so that it shows name under the year ( TahunPel/Year of service)

I appreciate your help. this is the only thing left, and writing something
like simple user manual, then we can go live.

Signature

H. Frank Situmorang

> You missed some of John's suggestion. The PelayanJemaat table should be added
> to the query twice.
[quoted text clipped - 161 lines]
> > > >> >> >
> > > >> >> > Thanks
Duane Hookom - 26 Feb 2008 13:37 GMT
Frank,
Go back to John's first post or my example of the crosstab and apply the
solution to your recent query. Use the CountOfBidangPelayanan as the the
Column Heading. The value would be the first of your name field.

Signature

Duane Hookom
Microsoft Access MVP

> Thanks Duane, the query now works now, but how can we make it into
> crosstabquery so that it shows name under the year ( TahunPel/Year of service)
[quoted text clipped - 167 lines]
> > > > >> >> >
> > > > >> >> > Thanks
Frank Situmorang - 27 Feb 2008 07:26 GMT
Ok John and Duane,

The query works now to have the year sideway, but the sequance of the
position is not like what I expected.

We need to that in my church officers table, I have the Position sequece in
order to present as follows:

Seq.No     Posision       Name
1.         First Elder        Mr. A
2.          Elders             Mr. B
                                  Mr. C
                                  Mr. D
3.           Dean of Deacon  Mr.E
4.           Deacons          Mr. F
                                    Mr. G
                                    Mr. H.

How can we make it the position is in sequence like that

Thanks in advance

Signature

H. Frank Situmorang

> You missed some of John's suggestion. The PelayanJemaat table should be added
> to the query twice.
[quoted text clipped - 161 lines]
> > > >> >> >
> > > >> >> > Thanks
John Spencer - 27 Feb 2008 14:13 GMT
If you mean you want the positions to appear in specific order then you will
need to sort by the column that contains Seq No.  Add an ORDER BY clause to
your query.

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> Ok John and Duane,
>
[quoted text clipped - 209 lines]
>> > > >> >> >
>> > > >> >> > Thanks
Frank Situmorang - 28 Feb 2008 03:26 GMT
Thanks John for your advice, The sequence already shows in sequeece but I do
not understand why each position only shows one record/one line

This is my SQL for Query:
SELECT PelayanJemaat.TahunPel, PelayanJemaat.NamaPel, PelayanJemaat.Nurut,
PelayanJemaat.BidangPelayanan, Count(PelayanJemaat_1.BidangPelayanan) AS
CountOfBidangPelayanan
FROM PelayanJemaat LEFT JOIN PelayanJemaat AS PelayanJemaat_1 ON
(PelayanJemaat.BidangPelayanan=PelayanJemaat_1.BidangPelayanan) AND
(PelayanJemaat.NamaPel<PelayanJemaat_1.NamaPel) AND
(PelayanJemaat.Nurut=PelayanJemaat_1.Nurut) AND
(PelayanJemaat.TahunPel=PelayanJemaat_1.TahunPel)
GROUP BY PelayanJemaat.TahunPel, PelayanJemaat.NamaPel, PelayanJemaat.Nurut,
PelayanJemaat.BidangPelayanan
ORDER BY PelayanJemaat.Nurut;

AND THIS IS MY SQL FOR CROSSTABQUERY:
TRANSFORM First(PelayanJemaatQrybyYear.NamaPel) AS FirstOfNamaPel
SELECT PelayanJemaatQrybyYear.Nurut, PelayanJemaatQrybyYear.BidangPelayanan,
First(PelayanJemaatQrybyYear.NamaPel) AS [Total Of NamaPel]
FROM PelayanJemaatQrybyYear
GROUP BY PelayanJemaatQrybyYear.Nurut, PelayanJemaatQrybyYear.BidangPelayanan
PIVOT PelayanJemaatQrybyYear.TahunPel;

Thanks for your help

Signature

H. Frank Situmorang

> If you mean you want the positions to appear in specific order then you will
> need to sort by the column that contains Seq No.  Add an ORDER BY clause to
[quoted text clipped - 213 lines]
> >> > > >> >> >
> >> > > >> >> > Thanks
Duane Hookom - 28 Feb 2008 03:43 GMT
It's a bit difficult to understand your SQL since most of us don't understand
your field names. I think your issue might be that you didn't follow my
instruction a while back to "Use the CountOfBidangPelayanan as the the Column
Heading." I don't know what your column heading field "TahunPel" stores.
Signature

Duane Hookom
Microsoft Access MVP

> Thanks John for your advice, The sequence already shows in sequeece but I do
> not understand why each position only shows one record/one line
[quoted text clipped - 215 lines]
> > >> > > >> >> > 3.  Deacon         Mr. F
> > >> > > >> >> > Year 2007...same way downward
Frank Situmorang - 28 Feb 2008 04:10 GMT
Sorry Duane, Tahunpel is year of service or we can say Year, I just do not
now which one is columnheading because it didn't show column heading like in
the grids, but is it Pivot what you mean column heading?, I have tried
bidangpelayanan or in english means fieldposition as the columnheading, but
it will not show the year ub the column result.

Do I miss something?, sorry for the language problem, if I put full in
English, the people in Indonesia will not understand when I explain it to
them.

We appreciate if you could explain again.

Signature

H. Frank Situmorang

> It's a bit difficult to understand your SQL since most of us don't understand
> your field names. I think your issue might be that you didn't follow my
[quoted text clipped - 209 lines]
> > > >> > > >> >> >
> > > >> > > >> >> > Normal report based on the normal query:
Duane Hookom - 28 Feb 2008 04:20 GMT
I think you want the Year and the Position as Row Headings, the CountOf... as
the Column Heading, and first of the name field as the value.
Signature

Duane Hookom
Microsoft Access MVP

> Sorry Duane, Tahunpel is year of service or we can say Year, I just do not
> now which one is columnheading because it didn't show column heading like in
[quoted text clipped - 213 lines]
> > > > >> > > >> >> Hope this helps.
> > > > >> > > >> >> --
Frank Situmorang - 28 Feb 2008 04:54 GMT
Duane, I have tried that, but the result is, the year is in the row, and in
the colum heading is number 1 number 2....... What I want is like in the
outlay that I said in my first thread.

It seems that I want to give up on this approach, maybe I could try the
approach you said on other threads, in the website:
http://www.tek-tips.com/faqs.cfm?fid=5466.

Is it maybe because  in my table I have also field the member's ID?, which I
do not take it into account as it is for other purpose in the future?

Thanks for your helps

Signature

H. Frank Situmorang

> I think you want the Year and the Position as Row Headings, the CountOf... as
> the Column Heading, and first of the name field as the value.
[quoted text clipped - 207 lines]
> > > > > >> > > >> >> Now I would use the saved query as the source of a crosstab
> > > > > >> > > >> >> query.
Duane Hookom - 28 Feb 2008 05:16 GMT
Looking back at your original posting, if you want the year as part of the
column name, use The Year and Count of fields combined as the Column Heading.
Signature

Duane Hookom
Microsoft Access MVP

> Duane, I have tried that, but the result is, the year is in the row, and in
> the colum heading is number 1 number 2....... What I want is like in the
[quoted text clipped - 202 lines]
> > > > > > >> > > >> >>
> > > > > > >> > > >> >> Assuming that is the structure, I would first build a query to
Frank Situmorang - 28 Feb 2008 06:05 GMT
Thanks very much Duane and John it works now. May God bless you for all your
help. I will use this corsstab query in the reports.

Again, with many thanks
Signature

H. Frank Situmorang

> Looking back at your original posting, if you want the year as part of the
> column name, use The Year and Count of fields combined as the Column Heading.
[quoted text clipped - 195 lines]
> > > > > > > >> > > >> > of the
> > > > > > > >> > > >> > names.
John Spencer - 28 Feb 2008 12:47 GMT
I think you need to include the ranking field (CountOfBidangPelayanan) in
the grouping clause to generate multiple lines.

TRANSFORM First(PelayanJemaatQrybyYear.NamaPel) AS FirstOfNamaPel
SELECT PelayanJemaatQrybyYear.Nurut
, PelayanJemaatQrybyYear.BidangPelayanan
, First(PelayanJemaatQrybyYear.NamaPel) AS [Total Of NamaPel]
FROM PelayanJemaatQrybyYear
GROUP BY PelayanJemaatQrybyYear.Nurut
, PelayanJemaatQrybyYear.BidangPelayanan

, CountOfBidangPelayanan

PIVOT PelayanJemaatQrybyYear.TahunPel;

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> Thanks John for your advice, The sequence already shows in sequeece but I
> do
[quoted text clipped - 25 lines]
>
> Thanks for your help
Frank Situmorang - 29 Feb 2008 03:08 GMT
Thanks John, it works now like what I expected, the only problem now, how can
I make it on the report the dynamic Year's column heading. When I tried to
design a report based on that crosstab query, I saw the year already 2007,
2008, the sample that I made.

On the separated thread I will already asked it but Duane give the sample in
the webiste: http://www.tek-tips.com/faqs.cfm?fid=5466.
which is not easy to apply when the model of this crosstab query.

Thanks very much for your help
Greetings from Jakarta, Indonesia
Signature

H. Frank Situmorang

> I think you need to include the ranking field (CountOfBidangPelayanan) in
> the grouping clause to generate multiple lines.
[quoted text clipped - 40 lines]
> >
> > Thanks for your help
 
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.