473,394 Members | 1,746 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.

show number of values as % of total records..?

Hi
I'm migrating from Access til MySQL.
Works fine so far - but one thing is nearly killing me:

I got the count of total records in a variabel - (antalRecords)
I got the count for the Field Q1 where the value value is = 'nej'
Now I just need to calculate how many % of my records have the value 'nej'
I access this worked very fine - but with MySQL ( and ASP) I just cant get
it right!!! I go crazy ....

My code looks like this :

strSQL="SELECT COUNT(Q1) AS Q1_nej FROM Tbl_evaluering " &_
"WHERE Q1 = 'NEJ' "
set RS = connection.Execute(strSQL)
antal_nej = RS("Q1_nej")
procent_nej = formatNumber((antal_nej),2)/antalrecords * 100

Hope ...praying for help ...Please ;-)
best wishes -Otto - Copenhagen

Feb 15 '07 #1
3 2191
Otto (ot********@tele.dk) writes:
I'm migrating from Access til MySQL.
Over MS SQL Server, or are you in the wrong newsgroup?
I got the count of total records in a variabel - (antalRecords)
I got the count for the Field Q1 where the value value is = 'nej'
Now I just need to calculate how many % of my records have the value 'nej'
I access this worked very fine - but with MySQL ( and ASP) I just cant get
it right!!! I go crazy ....
SELECT cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) / COUNT(*)
as decimal(5,2)
FROM tbl

This works on my SQL Server. I cannot promise that it works on YourSQL,
though. but at least it's ANSI.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 15 '07 #2

"Erland Sommarskog" <es****@sommarskog.seskrev i en meddelelse >

SELECT cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) / COUNT(*)
as decimal(5,2)) FROM tbl

Hi erland - thanks for your response- the code works in the way that I dont
get an error - but I dont really know how to get the number of records (in
Field Q1) and the % of these who have the value 'nej' out from this
code..:-(

You can see my Access version of webpage on
http://www.gymnasiereform.dk/svar_q12.asp
here it works fien - but I cant make it work with MY SQL which I'm trying t
migrate to.
Best wishe s- Otto
Feb 16 '07 #3
Otto (ot********@tele.dk) writes:
"Erland Sommarskog" <es****@sommarskog.seskrev i en meddelelse >

SELECT cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) / COUNT(*)
as decimal(5,2)) FROM tbl

Hi erland - thanks for your response- the code works in the way that I
dont get an error - but I dont really know how to get the number of
records (in Field Q1) and the % of these who have the value 'nej' out
from this code..:-(
That code gives you the percentage who said no. COUNT(*) gives you the
the total number of rows.

SELECT COUNT(*) AS total
cast(100.0 * SUM (CASE Q1 WHEN 'nej' THEN 1 ELSE 0 END) /
COUNT(*) as decimal(5,2)) AS percent_that_said_no
FROM tbl

If Q1 can be NULL, and you only want to count non-NULL rows, replace
COUNT(*) with COUNT(Q1).
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Feb 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: and | last post by:
hi everyone, i am using oracle 9.2.0 and i have written a simple jdbc java program to insert a record within a for loop to a table using jdbc thin driver(refer to the bottom of this email for the...
2
by: chris vettese | last post by:
I've been searching for a few days and have't found the solution I'm looking for. I want to create the following in my form's header...Material: 1 of 15. The first number will change as the user...
5
by: ????? | last post by:
I have an access query which gets data from a number of different tables. Although the tables have primary key fields, the order in which the records are returned means that none of these are in...
2
by: MLH | last post by:
Gentlemen: I have declared an array Dim MyTables(14) AS Long Now I want to assign values for MyTables(0) - MyTables(14) equal to the number of records in each table. Catch, I want the code...
2
by: ricktech101 | last post by:
Hi, I have a table with a field that shows the number of pieces that a parcel contains. It looks like this: ParcelID, Pieces, Description Data example: 1001, 5, Jackets 1002, 10, shoes etc
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
130
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places...
2
by: Sanjaylml | last post by:
I have made a form in Access 2000. In addition to just simply enter the data through form, I have appended sub-form in main form to show the all the entered records as Sub-Form (DataSheet), which is...
0
by: runway27 | last post by:
hi my question is about "SHOW TABLE STATUS LIKE 'tablename'"; following is the code i am presently using ================================================================== $conn =...
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.