473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Need help putting together the most efficient query

TheSmileyCoder
2,322 Expert Mod 2GB
I have a document review system, in which users perform reviews of documents.

When a review is done, it gets set in a status of "Ready for Signing" reflected by a field in the table tbl_Review
tbl_Review
PK_Review (Primary Key, autonumber)
ID_Status (Long, value of 1="Ready for signing")

Furthermore I have a table tbl_QA in which I information is stored on who is supposed to sign for the review and whether its done or not.
tbl_QA
ID_Review (Long, Foreign Key of Review)
ID_User (Long, Foreign Key of User supposed to sign)
ID_SignedBy (Long, Foreign key of user who signed)
ID_Type (Long, Type of signature)

The ID_Type of signature can be:
1 - Prepared By
2 - Checked By
3 - Approved By


More then 1 person can be required to sign for a particular process, for example you could have 3 people who needs to sign off that they prepared the document.

Now to the question
I want create a form that shows all the reviews that User X has to sign off on, but has not yet done. To complicate it a bit, a user cannot sign the checked field until all prepare's has signed, and likewise it cannot be approved until all checkers has signed.

So a query to show User X all the document that are waiting for his signature. How would be the most efficient way of setting that up?
Sep 19 '12 #1
9 1399
zmbd
5,501 Expert Mod 4TB
TheSmileyCoder

What I'm missing here is how do you know which document requires what type of signiture? Do they all require all three? Once I have that I think I can work towards an answer for you. (although Rabbit will have it!)

-z
Sep 19 '12 #2
zmbd
5,501 Expert Mod 4TB
Should have added the following question:

Why have tbl_review? If the document is ready for: "Prepared By"; "Checked By"; or "Approved By" signitures then that entry should appear in your tbl_QA at that point.
Sep 19 '12 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Before a review can be issued, there must be minimum of 1 signature for each field. I.e. someone must sign to indicate he/she has prepared it, and someone else (cannot be the same as the preparer, but don't worry about that. That logic is handled elsewhere) must sign for the checking. Finally the Approval is signed by someone in management.


The tbl_Review has alot more information then shown here, I only showed what is relevant for the question at hand. The tbl_Review is important since a document does not require signing until it has reached the state "Ready for Signing". Other status can be "Under Review"(Prior to signing) or "Review Submitted" (After signing is complete).

When a user marks the review as ready for signing he is forced to select minimum 1 preparer, 1 checker and 1 approver. That means 3 entries in the tbl_QA table like so for review 8429:
Expand|Select|Wrap|Line Numbers
  1. ID_Review  ID_User  ID_Type  ID_SignedBy
  2. 8429       246      1        Null
  3. 8429       748      2        Null
  4. 8429       15       3        Null
When user 246 signs (Click of button) the ID_SignedBy switches to the user id (246)

Now I need to present a form to the user showing all reviews waiting for his signature. In the example illustrated the review should only show up on user 246's form. (Since user 748 cannot sign the checked field until the prepared field has been signed) Once user 246 has signed, it should no longer appear on his form.
Sep 19 '12 #4
zmbd
5,501 Expert Mod 4TB
Great! I was hoping that ID_Type was being used as the required signiture class and not something else...

I started with an aggregate query such as:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. tbl_qa.QA_FK_User, 
  3. tbl_qa.QA_FK_Review, 
  4. tbl_qa.QA_Type, 
  5. Count(tbl_qa.QA_FK_SignedBy) 
  6.    AS CountOfQA_FK_SignedBy
  7. FROM tbl_qa
  8. GROUP BY 
  9.  tbl_qa.QA_FK_User, 
  10.  tbl_qa.QA_FK_Review, 
  11.  tbl_qa.QA_Type
  12. HAVING ((Count(tbl_qa.QA_FK_SignedBy)=0));
We can toss a parameter in there to filter down by QA_FK_User so now we only get back documents that are related to the specific user that are not signed.

I'm thinking thru the rest of the logic. I'm thinking a self join back against the tbl_qa... just don't quite have it yet.

I do this best when I'm burning things in the lab and it just so happens I have few samples to just that to.... (maniacal laughter fills the room as the MadChemist skips away to the lab).
Sep 19 '12 #5
Rabbit
12,516 Expert Mod 8TB
You could do something like this.
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    q.ID_Review,
  3.    MIN(q.ID_Type) AS sigLevel
  4. FROM 
  5.    tbl_Review r INNER JOIN
  6.    tbl_QA q ON
  7.       r.PK_Review = q.ID_Review
  8. WHERE
  9.    r.ID_Status = 1 AND
  10.    q.ID_SignedBy IS NULL
  11. GROUP BY
  12.    q.ID_Review
Which will return the lowest signature level still needed for each review that is ready to be signed. You can then use that to join back to tbl_QA on the ID_Review and sigLevel to get everyone that still needs to sign at the lowest signature level.
Sep 19 '12 #6
zmbd
5,501 Expert Mod 4TB
Rabbit,
I am truely impressed!!!!!
The only mod I made to your query was to add a line in the WHERE clause to filter by user.
<bow to Rabbit>

I'd hit on the solution from my starting point and there's this truely elegant one from Rabbit. I think I should sit at Rabbit's feet and learn for awhile.
Sep 19 '12 #7
Rabbit
12,516 Expert Mod 8TB
lol z. You pick up a few things when most of what you do all day is write queries.
Sep 19 '12 #8
TheSmileyCoder
2,322 Expert Mod 2GB
@Zmbd You cannot filter on the user too early, as that might result in incorrect "signature" levels.

@Rabbit I knew you would come through for me on this one, presenting something that was simpler and more elegant then my solution which consisted of several nested queries. You always seem to turn SQL into an art. Thank you
Sep 19 '12 #9
Rabbit
12,516 Expert Mod 8TB
It comes from years of practice and reading. I have read my fair share of articles about SQL algorithms and efficient SQL code.
Sep 19 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Linus Nikander | last post by:
Having recently load-tested the application we are developing I noticed that one of the most expensive (time-wise) calls was my fetch of a db-connection from the defined db-pool. At present I fetch...
0
by: R U B'n | last post by:
Hi everyone, I have to make a (case-insensitive) search from a form with only one search string, e.g. "Doe Peters english California", which will search in several fields of my table for each...
2
by: Belmin | last post by:
Hi all, Wanted to know what is the most efficient way of doing a select query for mysql that only returns one value. For example: $mysqli->query('select count(*) from log'); $temprec =...
3
by: Brent Minder | last post by:
What is the most efficient way to code asp.net pages when you break your page up into user controls? For example: If you have a page with a header (control .ascx), body, and footer (control...
13
by: chrisben | last post by:
Hi, I need to insert more than 500,000 records at the end of the day in a C# application. I need to finish it as soon as possible. I created a stored procedure and called it from ADO to insert...
1
by: cwertman | last post by:
I have a document like so (Its actually a serilization of an Object) <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">...
2
by: chris | last post by:
I have a few byte arrays that I would like to combine into one array (order needs to be kept). What would be the most efficient way to do this? Thanks for your time, Chris
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
8
by: secutos | last post by:
Programming Language: C#, .NET Framework 3.5 In this context, Form and App both describe a Microsoft Windows desktop application i'm creating. I'm creating a wordlist generator. I need to be able...
0
by: jw11 | last post by:
I have three cross-tab queries, each for different properties of Jobs. I want to put these together into one query so I can get a report that looks something like this: Empls Job A Hours Job...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.