| Author |
Topic |
|
mrdatabase
Starting Member
12 Posts |
Posted - 2012-04-28 : 05:17:08
|
| Hi everyone, i am brand new to sql and i am using microsoft access SQL for coding. I have developed a query the only problem is i want the query to display my results as a percentage. Could anybody point me in the right direction?SQL Currently is:SELECT Weekday(date_time) AS Weekday, Count (*) AS Amount_Of_Usage FROM activity_usageWHERE date_time BETWEEN #JAN-01-2012# AND #MAR-01-2012#AND Activity_code = "A002"GROUP BY WEEKDAY(date_time);Thanks |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-04-28 : 05:45:16
|
If you just want the Percentage sign(%) with the Count then you can change your code:SELECT Weekday(date_time) AS Weekday, Count (*) AS Amount_Of_Usage FROM activity_usageWHERE date_time BETWEEN #JAN-01-2012# AND #MAR-01-2012#AND Activity_code = "A002"GROUP BY WEEKDAY(date_time); to the following code:SELECT Weekday(date_time) AS Weekday, Cast(COUNT(*) As Varchar)+'%' AS Amount_Of_Usage FROM activity_usageWHERE date_time BETWEEN #JAN-01-2012# AND #MAR-01-2012#AND Activity_code = "A002"GROUP BY WEEKDAY(date_time); Hope this helps you.N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
mrdatabase
Starting Member
12 Posts |
Posted - 2012-04-28 : 06:13:40
|
| Hi thankyou for your response! I entered the code however i am getting a syntax error with "Cast(COUNT(*) As Varchar)+'%'".Would you happen to know what it is?Currently my results look like this from the old SQLWeekday Amount of usage1 52 93 64 65 66 57 5 |
 |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-04-28 : 08:13:07
|
quote: Originally posted by mrdatabase Hi thankyou for your response! I entered the code however i am getting a syntax error with "Cast(COUNT(*) As Varchar)+'%'".Would you happen to know what it is?Currently my results look like this from the old SQLWeekday Amount of usage1 52 93 64 65 66 57 5
Based upon the sample data you provided I altered the Query a little and tested it on the sample data and it is working fine. Following is the working query.--Creating TableCreate Table Ex(Weekdayy int, Amount_of_usage int)--Inserting sample dataInsert Into ExSelect 1, 5Union ALLSelect 2, 9Union ALLSelect 3, 6Union ALLSelect 4, 6Union ALLSelect 5, 6Union ALLSelect 6, 5Union ALLSelect 7, 5--Altered QuerySELECT Weekday, Cast(COUNT(*) As Varchar)+'%' AS Amount_Of_Usage FROM Ex--WHERE date_time BETWEEN #JAN-01-2012# AND #MAR-01-2012#--AND Activity_code = "A002"GROUP BY WEEKDAY; I had to comment out the Where part because the sample data you provided does not have any fields for dates, neither does it have an Activity_code field.Other than that this query doesn't give any error.One more thing is that I really don't understand your business requirement behind this query. What are you actually trying to do???....The result set returns 1 as count for each weekday. What is the point??N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-04-28 : 08:24:49
|
| What is the Error that you are getting while running the query?N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
mrdatabase
Starting Member
12 Posts |
Posted - 2012-04-28 : 09:11:11
|
| The theory behind the query is that i have a table called activity usage which records the datetime when a customer uses an activtiy. So the table design is Activity_Usage [Customer_ID#, Activity Code#, Date_Time]I need to get a query running to display the results of the activity usage per day. ( example monday has 6%, tuesday has 10%...) so far i have counted the amount an activity has been entered on a certain day (example monday (which is displayed as 2 i think in the database results) equals 5 and so on) however i wish to turn this count into a percentage.Thats the problem i am having |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-04-28 : 09:34:50
|
[code]SELECT Weekday(date_time) AS Weekday, Count (*) * 100.0 / count(*) over() AS Amount_Of_UsageFROM activity_usageWHERE date_time BETWEEN #JAN-01-2012# AND #MAR-01-2012#AND Activity_code = "A002"GROUP BY WEEKDAY(date_time);[/code]and display that "%" in your front end applicationjust noticed that you are using MS Access. The query that i posted might not work in MS Access. It is for SQL Server KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mrdatabase
Starting Member
12 Posts |
Posted - 2012-04-28 : 09:37:29
|
| Still an error, with the "Count (*) * 100.0 / count(*) over()" of the function.What does the over() function do?Thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-04-28 : 09:43:46
|
quote: Originally posted by mrdatabase Still an error, with the "Count (*) * 100.0 / count(*) over()" of the function.What does the over() function do?Thanks
Sorry, didn't realized that you are using MS Access as you have posted under "New to SQL Server Programming". I don't think the over() is available under MS Access. And sorry i can't help you with MS Access as that is not my fortethere is a MS Access forum here http://www.sqlteam.com/forums/forum.asp?FORUM_ID=3 You should post your questions on Access over there KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-04-30 : 00:18:33
|
| I agree with Khtan....You'll get better solutions at the Ms Access Forum. You should post there.N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
|