473,395 Members | 2,689 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,395 software developers and data experts.

Select Count SQL

Hi there.

Well the situation is this, I have two tables:

1) tbl_registry where rows with code = 123 are 35 ;
1) tbl_Registered where rows with code = 123 are 43

In tbl_Registered I have it 7 rows more respect tbl_registry can you tell my why I have this situation:

ID = 1
NAME = JOHN
DATE = 2008-04-08
CODE = 123
Q = AAA
ACTIVITIES = XYZ
HOURS = 2

ID = 2
NAME = JOHN
DATE = 2008-04-08
CODE = 123
Q = AAA
ACTIVITIES = ABC
HOURS = 3

ID = 3
NAME = JIM
DATE = 2008-04-08
CODE = 123
Q = AAA
ACTIVITIES = XYZ
HOURS = 5
In tbl_Registered the rows is perfectly equal EXCEPT for the field HOURS and the field ACTIVITIES:

That is JOHN today 2008-04-08 has divided the total of 5 hours in two pieces of activity (2 -XYZ- and the other from 3-ABC-), while JIM has concentrated the total 5 hours only activity ABC.

I would like to query data extracts are the same:

Tot tbl_Registry = 35 ===> is right
Tot tbl_Registered = 35 ===> is right

Tot tbl_Registry = 35 ===> is right
Tot tbl_Registered = 43 ===> is wrong

My queries:

[php]

<%
sql_count = " SELECT "
sql_count = sql_count & " COUNT(CODE) "
sql_count = sql_count & " FROM "
sql_count = sql_count & " tbl_registry "
sql_count = sql_count & " WHERE "
sql_count = sql_count & " CODE LIKE '123%' "
sql_count = sql_count & " AND "
sql_count = sql_count & " Q = 'AAA' "

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open sql_count, cn

response.write objRS(0) & "<br>"

objRS.Close
Set objRS = Nothing

sql = " SELECT"
sql = sql & " DATE,"
sql = sql & " CODE,"
sql = sql & " Q,"
sql = sql & " COUNT(CODE) AS tot"
sql = sql & " FROM "
sql = sql & " tbl_Registered "
sql = sql & " WHERE "
sql = sql & " CODE LIKE '123%'"
sql = sql & " AND DATE = '2008-04-08'"
sql = sql & " AND Q = 'AAA'"
sql = sql & " GROUP BY "
sql = sql & " DATE "

Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open sql, cn

response.write RS("tot") & "<br>"

RS.Close
Set RS = Nothing

cn.Close
Set cn = Nothing

%>
[/php]

Thanks for your help.
Apr 8 '08 #1
7 4027
jeffstl
432 Expert 256MB
Maybe I am misunderstanding, but you want the count of records in your table with code 123 then why do you use the SQL LIKE syntax?

This will pull records with the code 1234, or 1230000, etc. Or possibly even 2123. Anything with the 123 sequence will be pulled as part of your count with the LIKE in there.

I might have missed a reason for it in your description of the problem though, if so please point it out.

If you want this to pull strictly code 123 just use the = sign instead of like and remove the %.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. sql_count = " SELECT "
  3. sql_count = sql_count & " COUNT(CODE) "
  4. sql_count = sql_count & " FROM "
  5. sql_count = sql_count & " tbl_registry "
  6. sql_count = sql_count & " WHERE "
  7. sql_count = sql_count & " CODE = '123' "
  8. sql_count = sql_count & " AND "
  9. sql_count = sql_count & " Q = 'AAA' "
  10.  
  11. Set objRS = Server.CreateObject("ADODB.Recordset")
  12. objRS.Open sql_count, cn
  13.  
  14.  
Another suggestion would be to print out your rows on your page (just for testing purposes) and see what records are actually being pulled that should not be. This will help narrow down the reason for the incorrect COUNT.
Apr 9 '08 #2
viki1967
263 100+
... sorry i wrong post...
Apr 9 '08 #3
Hi.

Thanks for your answer, but the problem is more difficult... I do not explain you...

I attach sql tables, excel files and ASP page:

http://www11.asphost4free.com/Miguel61/r937.zip

PSW the ZIP file is forum.

This file ZIP is not contagious or dangerous.... :)

Please help my !!!

I am desperate case.... jejejeje

Regards.
Apr 9 '08 #4
DrBunchman
979 Expert 512MB
Hi Mike1961,

I don't understand what it is you're trying to do! Please explain it again and try to be clearer.

Don't be afraid of giving too much information about your problem - that's better than too little!

Dr B
Apr 10 '08 #5
Hi Mike1961,

I don't understand what it is you're trying to do! Please explain it again and try to be clearer.

Don't be afraid of giving too much information about your problem - that's better than too little!

Dr B
Hi DrBunchman and thanks for your answer.

It has been difficult for me, but this is the solution:

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. sDatabaseConnection = "DRIVER={MySQL ODBC 3.51 Driver};"_
  4.                             & "SERVER=localhost;"_
  5.                             & "DATABASE=test;"_
  6.                             & "UID=root;PWD=XXXX; OPTION=35;"
  7.  
  8. Set connCount = Server.Createobject("ADODB.Connection")
  9. connCount.open sDatabaseConnection        
  10.  
  11. sql_count = " SELECT "
  12. sql_count = sql_count & " COUNT(CODE) "
  13. sql_count = sql_count & " FROM "
  14. sql_count = sql_count & " tbl_registry "
  15. sql_count = sql_count & " WHERE "
  16. sql_count = sql_count & " CODE LIKE '9168383%' "
  17. sql_count = sql_count & " AND "
  18. sql_count = sql_count & " Q = 'AAA' "
  19.  
  20. Set objRS = Server.CreateObject("ADODB.Recordset")
  21. objRS.Open sql_count, connCount
  22.  
  23.     response.write sql_count & "<br><br>"
  24.  
  25. if not objRS.eof then
  26.  
  27. sql = " SELECT "
  28. sql = sql & " NAME "
  29. sql = sql & " FROM " 
  30. sql = sql & " tbl_Registered "
  31. sql = sql & " WHERE "
  32. sql = sql & " CODE LIKE '9168383%'"
  33. sql = sql & " AND DATE = '2008-04-08'"
  34. sql = sql & " AND Q = 'AAA'"
  35. sql = sql & " GROUP BY "
  36. sql = sql & " NAME "
  37.  
  38. Set RS = Server.CreateObject("ADODB.Recordset")
  39. RS.Open sql, connCount
  40.  
  41. response.write sql & "<br><br>"
  42.  
  43. if not Rs.eof then
  44.  
  45. count = 0
  46.  
  47. Rs.MoveFirst()
  48. Do While Not Rs.EOF
  49.  
  50.     response.write Rs("NAME") &"<br>"
  51.  
  52. count = count + 1
  53.  
  54. Rs.MoveNext()
  55. Loop
  56.  
  57.    response.write "<br> Totali tbl_registered = " & count & "<br>"
  58.    response.write "Tot tbl_Registry = " & objRS(0) & "<br>"
  59.  
  60. end if
  61. end if 
  62.  
  63. objRS.Close
  64. Set objRS = Nothing
  65.  
  66. RS.Close
  67. Set RS = Nothing
  68.  
  69. connCount.Close
  70. Set connCount = Nothing 
  71.  
  72. %>
  73.  
I have:

Totali tbl_registered = 2 ===> right
Tot tbl_Registry = 35 ===> right

Try this code, pls....

Regards
Mike
Apr 11 '08 #6
DrBunchman
979 Expert 512MB
Hi Mike, have you resolved this yet?
Apr 15 '08 #7
Hi Mike, have you resolved this yet?
Yes, I resolved.
thanks
Apr 16 '08 #8

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

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
6
by: RCS | last post by:
I've been running into more and more complexity with an application, because as time goes on - we need more and more high-level, rolled-up information. And so I've created views, and views that use...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
4
by: headware | last post by:
I have a <select> control that contains many entries. It allows the user to multi-select a group of them, click a button, and store the selected data in a database. Normally they do this starting...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
4
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
5
by: Silvio Matthes | last post by:
Hello, I'm new to the list and did not find a suitable answer to my question so here it is: I try to select the rows of a table where the content of a varchar-column is empty ('') and...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
0
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.