Solved

General SQL two table count

Posted on 1998-09-28
8
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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