473,388 Members | 1,468 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,388 software developers and data experts.

Produce calculated fields

Hello folks. I'm new in this forum, but I have not found my question answered anywhere here so I guess it's OK for me to post it.

I have the following case.
Table [articleviews]
av_id
av_ip
av_datetime

This table records every presentation of any articles in my system.
The field [av_datetime] holds the date and time like yyyymmddhhmmss (14 chars)

What I would like to is to give in the current date/time (yyyymmddhhmmss) and retrieve the av_id (articleid) and two calculated fields. I want these calculated fields to be a counter for number of views for each [av_id] since (current datetime - 24 hours) and (current datetime - 24*7 hours).

example result:
.
..
...
av_id, av_countday, av_countweek
534, 45, 243
535, 34, 121
536, 56, 276
537, 12, 345
538, 19, 98
539, 23, 152
540, 31, 198
...

The av_countday and av_countweek should contain the count for each article for the last 24 hours and the last 24*7 hours.

I have solved this the easy way by using one record to hold the articles and the loop trough the postset and calculate (by SQL) for each of them. But I would like to do this by one single SQL-statement.

Any ideas ?

Thanx alot in advance.
Jun 1 '06 #1
3 2773
Banfa
9,065 Expert Mod 8TB
Hmmm I can think of a way of doing it with 2 SQL statements

for last 24 hours

SELECT av_id, COUNT(av_datetime) AS av_countday FROM articleviews WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= av_datetime;

for last week

SELECT av_id, COUNT(av_datetime) AS av_countday FROM articleviews WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= av_datetime;
Jun 5 '06 #2
Thanks for answering my question.

After I posted my question I spent 'bout 30 minutes to implement a SP to solve my problem. And I have used your approach in my current version like this:
Create temporary table
Insert all articleIDs
Insert count for last 24 hours for give articleID
Insert count for last week for given articleID

Select-statement to return a recordset to the ASP-page

I have gained a real boost regarding the time for returning the result in the page, but I have a feeling that this problem should be possible to solve with just one single SQL-statement.
Jun 5 '06 #3
CaptainD
135 100+
In answer to your question I created this to get personnel ID numbers from a table using two different date ranges and it works, see if you can adapt it to yours.

Expand|Select|Wrap|Line Numbers
  1. select count(*) from tblPersonnel where PersonnelID IN (SELECT personnelID from tblPersonnel where 
  2. dateHired between DateAdd(year, -2, GetDate()) and GetDate())or PersonnelID IN 
  3. (SELECT personnelID from tblPersonnel where dateHired between DateAdd(year, -5, GetDate()) and GetDate())
  4.  
Jun 5 '06 #4

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

Similar topics

0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
1
by: Miguelito Bain | last post by:
hi everybody. i'm trying to find out what you can and can't do with/in calculated fields. any help, pointers, and/or advice would be greatly appreciated. i'm a newbie, but i want to learn, and...
5
by: John Bahran | last post by:
I am trying to use calculated fields in my query but all the results are zero ven when they're not. Please help. Thanks.
2
by: david | last post by:
Hi, I have a form with a couple of calculated fields. I want to put some code into the 'Form-Load' event to set various object states on the form, depending on the value of these fields. The...
2
by: Ryker | last post by:
I have a Purchase Order where I have a calculated field called Price that is calculated by multiplying Qty * Sales Price. I have 10 of these fields...Price 1 - Price 10 (for each line of the PO). ...
2
by: Olveres | last post by:
Hi, I have managed to work out how to add new lines into a calculated text box. However in this text box some of the outcome fields are empty however when previewing the report it includes the...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
2
by: mkbrady | last post by:
I have a query that includes calculated fields that generate numeric results. I have wrapped conversion functions CLng() and CDdl() around the calculated fields to ensure the data types are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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
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
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...

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.