469,329 Members | 1,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Question: Sample sets and how do I manage them

Hi everyone,

I have a table with a set of accounts in that contains one row for each
account for each time it has been audited. (this may not include every
account we deal with)

Each month I want to select, from a separate and full list of accounts
that i import to the database, a random selection of new accounts to be
audited this month. Hence, a row for each one needs to be added to the
original database and the most recent entry updated (if it has been
audited before).

I don't want to select an account for re-audit within 3 months (for
example) and the accounts have a category. I want the sample selection
to be variable for each category i.e. 12 account of type A, 25 accounts
of type B etc.

I'm currently doing this with query upon query upon query (about 16 at
the moment) and can't help thinking that there must be a more logical
way of doing this in code or maybe by using more effective queries...

I'm looking for advice on the approach i should take not really looking
for code samples but will take advice on that if it is provided.

Any good ideas out there?

Thanks,

Rob.

Nov 13 '05 #1
3 1420

"dkintheuk" <rm*******@firenet.uk.com> schreef in bericht news:11*********************@o13g2000cwo.googlegro ups.com...
Hi everyone,

I have a table with a set of accounts in that contains one row for each
account for each time it has been audited. (this may not include every
account we deal with)

Each month I want to select, from a separate and full list of accounts
that i import to the database, a random selection of new accounts to be
audited this month. Hence, a row for each one needs to be added to the
original database and the most recent entry updated (if it has been
audited before).

I don't want to select an account for re-audit within 3 months (for
example) and the accounts have a category. I want the sample selection
to be variable for each category i.e. 12 account of type A, 25 accounts
of type B etc.

I'm currently doing this with query upon query upon query (about 16 at
the moment) and can't help thinking that there must be a more logical
way of doing this in code or maybe by using more effective queries...

I'm looking for advice on the approach i should take not really looking
for code samples but will take advice on that if it is provided.

Any good ideas out there?

Thanks,

Rob.


Hi Rob,
If you need 16 query's for this, there is something wrong in your approch.
I think a UNION-query is what you need here.

I suppose you have a table called TblAccounts (AccountID-AccountType)
and a Table Called TblAudits (AccountID-AuditDate) (or a query with the last AuditDate for each Account)

3 months is ~ 100 days so something like:

SELECT TOP 12 TblAccounts.AccountID, TblAudits.AuditDate
FROM TblAccounts LEFT JOIN TblAudits ON TblAccounts.AccountID = TblAudits.AccountID
WHERE (((TblAudits.AuditDate) Is Null Or (TblAudits.AuditDate)>Date()-100) AND ((TblAccounts.AccountType)="A"))
UNION SELECT TOP 25 TblAccounts.AccountID, TblAudits.AuditDate
FROM TblAccounts LEFT JOIN TblAudits ON TblAccounts.AccountID = TblAudits.AccountID
WHERE (((TblAudits.AuditDate) Is Null Or (TblAudits.AuditDate)>Date()-100) AND ((TblAccounts.AccountType)="B"))
.......
UNION SELECT Top xx ........ where ...... AccountType) = "Z"))

will give you what you need.
You will have to adapt this SQL as needed of course !

Arno R
Nov 13 '05 #2

Arno R wrote:
"dkintheuk" <rm*******@firenet.uk.com> schreef in bericht news:11*********************@o13g2000cwo.googlegro ups.com...
Hi everyone,

I have a table with a set of accounts in that contains one row for each account for each time it has been audited. (this may not include every account we deal with)

Each month I want to select, from a separate and full list of accounts that i import to the database, a random selection of new accounts to be audited this month. Hence, a row for each one needs to be added to the original database and the most recent entry updated (if it has been
audited before).

I don't want to select an account for re-audit within 3 months (for
example) and the accounts have a category. I want the sample selection to be variable for each category i.e. 12 account of type A, 25 accounts of type B etc.

I'm currently doing this with query upon query upon query (about 16 at the moment) and can't help thinking that there must be a more logical way of doing this in code or maybe by using more effective queries...
I'm looking for advice on the approach i should take not really looking for code samples but will take advice on that if it is provided.

Any good ideas out there?

Thanks,

Rob.


Hi Rob,
If you need 16 query's for this, there is something wrong in your

approch. I think a UNION-query is what you need here.

I suppose you have a table called TblAccounts (AccountID-AccountType) and a Table Called TblAudits (AccountID-AuditDate) (or a query with the last AuditDate for each Account)
3 months is ~ 100 days so something like:

SELECT TOP 12 TblAccounts.AccountID, TblAudits.AuditDate
FROM TblAccounts LEFT JOIN TblAudits ON TblAccounts.AccountID = TblAudits.AccountID WHERE (((TblAudits.AuditDate) Is Null Or (TblAudits.AuditDate)>Date()-100) AND ((TblAccounts.AccountType)="A")) UNION SELECT TOP 25 TblAccounts.AccountID, TblAudits.AuditDate
FROM TblAccounts LEFT JOIN TblAudits ON TblAccounts.AccountID = TblAudits.AccountID WHERE (((TblAudits.AuditDate) Is Null Or (TblAudits.AuditDate)>Date()-100) AND ((TblAccounts.AccountType)="B")) ......
UNION SELECT Top xx ........ where ...... AccountType) = "Z"))

will give you what you need.
You will have to adapt this SQL as needed of course !

Arno R


Arno, Thanks for that,

That's kind of what i thought about but i inherited some of this and
built other parts. Each query is doing a little bit of incremental
change to the data to allow me to work with it.

Of course you're right about the UNION approach. I had though about
that but, guess what, i'd never heard of the TOP function in SQL (never
had need before). That solves a great deal of problems and makes the
query easier to handle.

I will need a cuople of preparatory queries though but now that is far
less onerous. Thanks for the advice can't think why i didn't get closer
befoer... never mind!

Cheers,

Rob.

Nov 13 '05 #3
Just a small update...

I now have to handle this in 3 stages:

1. Prepare the new records so that they have unique random numbers
attached.
2. Prepare the existing table so that i report only one row per account
(even if it has been audited more than once before)
3. Join these together as per your query but make sure I ORDER BY the
unique number so that I force a better random set and append this data
to the original table!

Easy now I come to think about it...

As the categories can vary, i've written a funky loop to handle that
and create the long UNION query before running it.

Good job mate, helped me loads!

Rob.

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by nrk | last post: by
1 post views Thread by Jim | last post: by
11 posts views Thread by Peter M. | last post: by
3 posts views Thread by Henry | last post: by
73 posts views Thread by JoeC | last post: by
3 posts views Thread by Ara Kooser | last post: by
4 posts views Thread by Ara Kooser | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.