Solved

General SQL two table count

Posted on 1998-09-28
8
277 Views
Last Modified: 2010-05-18
I have these two tables and I want to do a count for a condition, the problem is that I have to use two tables so it makes the results inaccurate.

select count(fsmSocialSecurity) from fsmain, fsErrorLog where (fsmTechnicianNumber = '1701') and (fsmSignOffStatus = 3) and ((fseHadTANFError=1) or (fseHadFSError=1))

I only want to count records in the fsMain table

fields that start with fsm are in the fsMain table fields that start with fse and in the fsErrorLog table.

another thing is that I am assigning this count value to a variable in Delphi by doing this
MyError := DataModule1.ErrorQuery.Fields[0].AsInteger;
is this the best way?

Thanks
0
Comment
Question by:Greedy
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:Jim_SQL
ID: 1090304
This is for MS SQL Server:

DECLARE @v int

select @v = count(fsmSocialSecurity)
from fsmain, fsErrorLog
where fsmTechnicianNumber = '1701'
and fsmSignOffStatus = 3
and ((fseHadTANFError=1) or (fseHadFSError=1))

select @v


The total that is given will only count the records that match between the tables (records have to be in fsMain table). If fsmSocialSecurity can ever be NULL then you probably want to do a count(*) instead of a count(fsmSocialSecurity). I don't know Delphi, but the above code is what I'd use for MS SQL 6.5 as far as assigning the value goes.

Hope this helped.
Jim

0
 

Expert Comment

by:doubas
ID: 1090305
for this query to return the correct results, you need to "join" the two tables in your query.  a join is what establishes the relationship between the two tables, and is needed for the query to run correctly.

while you didn't provide a complete layout of the tables, if they have an established relationship, you just need to add the columns that make up this relationship to your "where" clause.  

for example, if the two tables are related by technician number, the following query should return the correct results:

eg:  select count(fsmSocialSecurity)
       from fsMain, fsErrorLog
      where fsmTechnicianNumber = '1701'
        and fsmSignOffStatus = 3
        and fsErrorLog.fseTechnicianNumber = fsMain.fsmTechnicianNumber -- this is the "join" you need
        and fseHadTANFError = 1
        and fseHadFSError = 1

While the technician number may not be the way the tables are related in actuality, hopefully you get the idea.  :)

HTH,
doug
0
 
LVL 2

Expert Comment

by:Jim_SQL
ID: 1090306
Oops, I didn't notice that... doubas is obviously correct regarding the join.
0
 
LVL 1

Author Comment

by:Greedy
ID: 1090307
I still must be doing somthing wrong.

Here's the sturcture and relations

fsMain
  fsmHousehold       - int
  fsmSocialSecurity  - int
  fsmReviewNumber    - smallint
  fsmCaseNameLast    - varchar
  fsmCaseNameFirst   - varchar
  fsmDateOfReview    - datetime
  fsmTechnicianNumber- varchar
  fsmReviewerNumber  - varchar
  fsmTypeOfReview    - smallint
  fsmReviewPiriodFrom- datetime
  fsmReviewPiriodTo  - datetime
  fsmSignOffStatus   - smallint

fsErrorLog
  fseSocialSecurity    - int
  fseReviewNumber      - smallint
  fseHadTANFError      - bit
  fseHadTANFDeficiency - bit
  fseHadFSError        - bit
  fseHadFSDeficiency   - bit
  fseDateWorkCompleted - datetime
  fseResponseNotes     - text
  fseResponseDate      - datetime

The tables should be related with fsXSocialSecurity and fsXReviewNumber  were the X is a m or e for Main or ErrorLog

These are the queries that I am using to get the counts for diferent things, they look a bit strange because I'm using Delphi to build the set MyTechSet it looks like ('1701',1702'...)

Total -
'select count(*) from fsmain where (fsmTechnicianNumber in ' + MyTechSet + ') and (fsmSignOffStatus = 3)'

# of Errors -
'select count(*) from fsmain, fsErrorLog where (fsMain.fsmSocialSecurity = fsErrorLog.fseSocialSecurity) and (fsmTechnicianNumber in ' + MyTechSet + ') and (fsmSignOffStatus = 3) and ((fseHadTANFError=1) or (fseHadFSError=1))'

# of Deficiency -
'select count(*) from fsmain, fsErrorLog where (fsMain.fsmSocialSecurity = fsErrorLog.fseSocialSecurity) and (fsmTechnicianNumber in ' + MyTechSet + ') and (fsmSignOffStatus = 3) and ((fseHadTANFDeficiency=1) or (fseHadFSDeficiency=1))'

right now I have 5 recors in the database all with the same SocialSecurity and TechnicianNumber, SigOffStatus = 3...if no errors or defs the Querries work but if one record has a error it then sayes five are in error? then if two records have errors it says 10!? what do you think it is?  I've tried it from ISQL_w and my program same results
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Greedy
ID: 1090308
oh I think I got it...I forgot to add the Review number too -
and (fsMain.fsmReviewNumber = fsErrorLog.fseReviewNumber)

Would you like to recieve the points doubas?

Thanks

0
 

Expert Comment

by:doubas
ID: 1090309
yes, i would - i guess you need to reject Jim_SQL's answer and i will submit an answer that you can then accept?  i'm new to this, so i'm not sure if that's the way it should work or not.  let me know.

also, may i make one more suggestion?  imho, you should move the location of your joins in your where clauses so that the joins do not occur until after you have screened out a portion of the data.  look at the example below:

select count(*)
  from fsmain, fsErrorLog
 where (fsmTechnicianNumber in ' + MyTechSet + ')
   and (fsmSignOffStatus = 3)
   and (fsMain.fsmSocialSecurity = fsErrorLog.fseSocialSecurity)
   and ((fseHadTANFError=1) or (fseHadFSError=1))

in this query, the join should not occur until you have already narrowed down the rows in fsMain to the proper technician number and sign-off status.  if you put the join first, then all rows n the two tables would be joined, with some/most of them being discarded later after you specify the technician number/sign-off status.  

it's true that the query optimizer would probably handle it automatically for you, but i think it's a good habit to assume that it won't.

doug
0
 
LVL 1

Author Comment

by:Greedy
ID: 1090310
Yeah, that sounds like a good idea.  Thanks
0
 

Accepted Solution

by:
doubas earned 50 total points
ID: 1090311
see earlier comments.  thanks, greedy.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now