Minggu, 11 Oktober 2020

Re: [MSAccessProfessionals] How to restrict results in a pivot query

look at your Where Clause.
There are Two tables in there:


fix the Outer Where and add additional Where on the Join:

WHERE (((pets.tblpet.pet_deceased)="N"))


TRANSFORM First(pets.breed_desc) AS FirstOfBreed
SELECT pets.pet_cust_no
FROM (SELECT tblpet.pet_cust_no, tblpet.pet_no, tblpet.pet_deceased, tblbreed.breed_desc AS breed_desc, Count(tblpet_1.pet_cust_no) AS ColHead FROM (tblbreed INNER JOIN tblpet ON tblbreed.breed_no=tblpet.pet_breed_no) INNER JOIN tblpet AS tblpet_1 ON tblpet.pet_cust_no=tblpet_1.pet_cust_no WHERE (((tblpet.pet_no)>=[tblpet_1.pet_no]) And ((tblpet_1.pet_deceased)="N")) GROUP BY tblpet.pet_cust_no, tblpet.pet_no, tblpet.pet_deceased, [tblbreed].[breed_desc] ORDER BY tblpet.pet_no)  AS pets
WHERE (((tblpet.pet_deceased)="N"))
GROUP BY pets.pet_cust_no
PIVOT "breed_desc" & [ColHead] In ("breed_desc1","breed_desc2","breed_desc3","breed_desc4","breed_desc5","breed_desc6");

On Sun, Oct 11, 2020 at 10:06 PM sue <sue@questor-cp.co.uk> wrote:

Hello all

 

About 6 years ago I asked for help on writing a query to gather data from a variable number of records in various tables and put it all into csv file to populate a form. I did finally get it working and recently I needed to modify it to eliminate some information. I haven't entirely succeeded so I hope you can help.

 

The system is for a boarding kennels or cattery. It's a commercial system, developed in (I think) Visual Studio with an Access database, but I am able to develop custom queries for it.

 

The query is collect the information on up to six animals owned by one customer, when the customer books them in for boarding. It encompasses a number of pivot tables which collect each animal's breed, vaccinations, medications, etc, and put the results into separate fields. I altered the pivot tables recently to eliminate animals which were deceased and I thought I'd done that successfully but actually the pivot tables still count the 'blank' record; when the form is populated the entries for the deceased animal are blank. When I tested it I must have selected bookings where the deceased pet was the last of the selected records so it appeared to be working.

 

One of the pivot queries is below; can anyone tell me a way of stopping the query adding the deceased animals to the count, please? I'm sure it's pretty obvious but my mind is also blank!

 

TRANSFORM First(pets.breed_desc) AS FirstOfBreed

SELECT pets.pet_cust_no

FROM (SELECT tblpet.pet_cust_no, tblpet.pet_no, tblpet.pet_deceased, tblbreed.breed_desc AS breed_desc, Count(tblpet_1.pet_cust_no) AS ColHead FROM (tblbreed INNER JOIN tblpet ON tblbreed.breed_no=tblpet.pet_breed_no) INNER JOIN tblpet AS tblpet_1 ON tblpet.pet_cust_no=tblpet_1.pet_cust_no WHERE (((tblpet.pet_no)>=[tblpet_1.pet_no])) GROUP BY tblpet.pet_cust_no, tblpet.pet_no, tblpet.pet_deceased, [tblbreed].[breed_desc] ORDER BY tblpet.pet_no)  AS pets

WHERE (((pets.tblpet.pet_deceased)="N"))

GROUP BY pets.pet_cust_no

PIVOT "breed_desc" & [ColHead] In ("breed_desc1","breed_desc2","breed_desc3","breed_desc4","breed_desc5","breed_desc6");

Thanks in advance

 

Sue



--
Arnelito G. Puzon


_._,_._,_

Groups.io Links:

You receive all messages sent to this group.

View/Reply Online (#115763) | Reply To Group | Reply To Sender | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [sugeng.panjalu.access@blogger.com]

_._,_._,_

Tidak ada komentar:

Posting Komentar