Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Per Day 1

Status
Not open for further replies.

EnergyTed

Programmer
Jan 27, 2005
68
0
0
GB
Hi

I am very frustrated and need some help/advice!

I have a ownership table with the following fields -

Company Name
Ownership Start Date
Item

When a company creates a new item a row is created in the table.
When a company transfers an item a row is created in the table with a new Ownership Start Date.

I am trying to calculate a Count Per Day as per the below example -

Date, Company1, Company2, Etc
01/01/2011, 10, 9, 3


A transfer takes place from Company1 to Company2

Date, Company1, Company2, Etc
02/01/2011, 9, 10, 3

Please let me know if you require any further information?


 
Hi Duane

The 10, 9, 3 and examples of the running count on that particular date that I am trying to achieve.

An example of the actual data would be

Company Name, Ownership Start Date, Item
CompA, 01/01/2011, Item1
CompA, 01/01/2011, Item2
CompA, 01/01/2011, Item3
CompA, 01/01/2011, Item4
CompA, 01/01/2011, Item5
CompA, 01/01/2011, Item6
CompA, 01/01/2011, Item7
CompA, 01/01/2011, Item8
CompA, 01/01/2011, Item9
CompA, 01/01/2011, Item10
CompB, 01/01/2011, Item11
CompB, 01/01/2011, Item12
CompB, 01/01/2011, Item13
CompB, 01/01/2011, Item14
CompB, 01/01/2011, Item15
CompB, 01/01/2011, Item16
CompB, 01/01/2011, Item17
CompB, 01/01/2011, Item18
CompC, 01/01/2011, Item19
CompC, 01/01/2011, Item20
CompC, 01/01/2011, Item21
CompB, 02/01/2011, Item1

The table is called Ownership.

Cheers
Ted
 
Apparently you want a count of records by date and company. This looks like a basic crosstab query with the [Ownership Start Date] as a Row Heading, [Company Name] as the Column Heading, and Count(item) as the Value.

Duane
Hook'D on Access
MS Access MVP
 
No, because of the way the table is stuctured, the results from a crosstab query only show ever increasing numbers, whereas in reality one Company has increased and another has decreased their Item count? When CompB takes ownership of Item1, the running count for that date should reflect that, i.e. CompA 9, CompB10.

Ted

 
Based on your data, you can create the following queries:

qselOwnershipStartEnd
Code:
SELECT Ownership.[Company Name], Ownership.[Ownership Start Date], Ownership.Item, Nz(Ownership_1.[Ownership Start Date] - 1, date()) AS OwnEndDate
FROM Ownership LEFT JOIN Ownership AS Ownership_1 ON (Ownership.Item = Ownership_1.Item) AND (Ownership.[Ownership Start Date] < Ownership_1.[Ownership Start Date]) AND (Ownership.[Company Name] <> Ownership_1.[Company Name]);

qgrpOwnershipDates
Code:
SELECT Ownership.[Ownership Start Date] AS OwnDate
FROM Ownership
GROUP BY Ownership.[Ownership Start Date];

qxtbOwnershipDates
Code:
TRANSFORM Count(qselOwnershipStartEnd.Item) AS CountOfItem
SELECT qgrpOwnershipDates.OwnDate
FROM qselOwnershipStartEnd, qgrpOwnershipDates
WHERE (((qgrpOwnershipDates.OwnDate) Between [Ownership Start Date] And [OwnEndDate]))
GROUP BY qgrpOwnershipDates.OwnDate
PIVOT qselOwnershipStartEnd.[Company Name];

[tt]
OwnDate CompA CompB CompC
1/1/2011 10 8 3
1/2/2011 9 9 3
[/tt]

I'm not sure how this will work with multiple ownership changes. The first query may need to be changed to use DMin().

Duane
Hook'D on Access
MS Access MVP
 
Hi Duane

I haven't had time to test the queries but on first look they seem to do exactly what I am looking for.

I will report back when I have had time to test them, especially the possible change to the Dmin() as I know ownership has changed multiple times and this will need to be tested.

Thanks for your time and patience.


Ted.
 
Hi Duane

I finally have some time to experiment and test the queries above.

I am not sure where the Dmin() function should be exchanged too allow for multiple ownerships changes?

Is it

Nz(Ownership_1.[Ownership Start Date] - 1, Dmin()) AS OwnEndDate
Or
Nz(Dmin(Ownership_1.[Ownership Start Date], date())) AS OwnEndDate
Or
Dmin(Nz(Ownership_1.[Ownership Start Date] - 1, date())) AS OwnEndDate

Or am I completely wrong?

Yours in hope

Ted
 
Hi Duane

An example of actual data would be -

Company Name, Ownership Start Date, Item
CompA, 01/01/2011, Item1
CompA, 01/01/2011, Item2
CompA, 01/01/2011, Item3
CompA, 01/01/2011, Item4
CompA, 01/01/2011, Item5
CompA, 01/01/2011, Item6
CompA, 01/01/2011, Item7
CompA, 01/01/2011, Item8
CompA, 01/01/2011, Item9
CompA, 01/01/2011, Item10
CompB, 01/01/2011, Item11
CompB, 01/01/2011, Item12
CompB, 01/01/2011, Item13
CompB, 01/01/2011, Item14
CompB, 01/01/2011, Item15
CompB, 01/01/2011, Item16
CompB, 01/01/2011, Item17
CompB, 01/01/2011, Item18
CompC, 01/01/2011, Item19
CompC, 01/01/2011, Item20
CompC, 01/01/2011, Item21
CompB, 02/01/2011, Item1
CompA, 03/01/2011, Item1
CompB, 04/01/2011, Item1

And the result would be -

OwnDate CompA CompB CompC
01/01/2011 10 8 3
02/01/2011 9 9 3
03/01/2011 10 8 3
04/01/2011 9 9 3

Kind Regards

Ted
 
Did you try the suggested queries?
They seem to match your desired result.
[tt]
OwnDate CompA CompB CompC
1/1/2011 11 8 3
1/2/2011 10 9 3
1/3/2011 11 8 3
1/4/2011 9 9 3
[/tt]

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top