in my data base three columns are there 1. date 2. call_type(either incident/request) 3. count( it will tell how many times incident/request occured in one week).
Now problem is i know the starting and ending dates of week from these i have to find the 12 weeks back and have to group the data by weekly
example i need output in the form of
week call_type count
28/feb/2008 to 05/mar/2008 incident 10
28/feb/2008 to 05/mar/2008 request 4
21/feb/2008 to 27/feb/2008
similarly 12 weeks back from 05/mar/2008 (totaly i need 12 weeks).
can u help me?
4 9520
A cumbersome (perhaps cool)SQL can be worked out but it might be
cleaner maintenance-wise to write a simple store procedure to handle your
listing
Just loop for N-times going back 7 days per loop and within that
do your counting should be just a few lines
Ciao
in my data base three columns are there 1. date 2. call_type(either incident/request) 3. count( it will tell how many times incident/request occured in one week).
Now problem is i know the starting and ending dates of week from these i have to find the 12 weeks back and have to group the data by weekly
example i need output in the form of
week call_type count
28/feb/2008 to 05/mar/2008 incident 10
28/feb/2008 to 05/mar/2008 request 4
21/feb/2008 to 27/feb/2008
similarly 12 weeks back from 05/mar/2008 (totaly i need 12 weeks).
can u help me?
You showed your desired output, would you mind posting some of the sample data on your database?
-- CK
in my data base three columns are there 1. date 2. call_type(either incident/request) 3. count( it will tell how many times incident/request occured in one week).
Now problem is i know the starting and ending dates of week from these i have to find the 12 weeks back and have to group the data by weekly
example i need output in the form of
week call_type count
28/feb/2008 to 05/mar/2008 incident 10
28/feb/2008 to 05/mar/2008 request 4
21/feb/2008 to 27/feb/2008
similarly 12 weeks back from 05/mar/2008 (totaly i need 12 weeks).
can u help me?
[code = sql ]
probably the following code will help you. just change the table name and column name to match your database tables
DECLARE @StartDate datetime
set @StartDate = GetDate() - (7*12)
DECLARE @Sql varchar(max)
SELECT @Sql = ''
WHILE ( @StartDate <= Getdate())
BEGIN
SELECT @Sql = @Sql +
'SELECT ''' + CONVERT(VARCHAR(20),@StartDate,110) + ' - ' + CONVERT(VARCHAR(20),DATEADD(DD,7,@StartDate),110) +''',
call_type,SUM(count) FROM Table_Name
WHERE CONVERT(VARCHAR(20),date,110)' +' BETWEEN '''+ CONVERT(VARCHAR(20),@StartDate,110) + ''' AND ''' + CONVERT(VARCHAR(20),DATEADD(DD,7,@StartDate),110) + ''''+
'GROUP BY call_type ' + ' UNION '
SET @StartDate = DATEADD(DD,7,@StartDate)
END
SELECT @Sql= SUBSTRING(@Sql,0,(LEN(@Sql) - LEN('UNION')))
--PRINT @SQL
EXEC (@SQL)
[/code]
[code = sql ]
probably the following code will help you. just change the table name and column name to match your database tables
DECLARE @StartDate datetime
set @StartDate = GetDate() - (7*12)
DECLARE @Sql varchar(max)
SELECT @Sql = ''
WHILE ( @StartDate <= Getdate())
BEGIN
SELECT @Sql = @Sql +
'SELECT ''' + CONVERT(VARCHAR(20),@StartDate,110) + ' - ' + CONVERT(VARCHAR(20),DATEADD(DD,7,@StartDate),110) +''',
call_type,SUM(count) FROM Table_Name
WHERE CONVERT(VARCHAR(20),date,110)' +' BETWEEN '''+ CONVERT(VARCHAR(20),@StartDate,110) + ''' AND ''' + CONVERT(VARCHAR(20),DATEADD(DD,7,@StartDate),110) + ''''+
'GROUP BY call_type ' + ' UNION '
SET @StartDate = DATEADD(DD,7,@StartDate)
END
SELECT @Sql= SUBSTRING(@Sql,0,(LEN(@Sql) - LEN('UNION')))
--PRINT @SQL
EXEC (@SQL)
[/code]
Hi,
Try the following query to get the group by results without using a stored procedure
hope probably will help you... -
SELECT FROMDATE,TODATE, Call_Type,KOUNT
-
FROM (
-
-
SELECT T1.R1,T1.DATE1 AS FROMDATE,CONVERT(VARCHAR(20),DATEADD(DD,7,T1.DATE1),110) AS TODATE,
-
T1.Call_Type,SUM(T2.KOUNT) AS KOUNT
-
FROM
-
( SELECT ROW_NUMBER() OVER (ORDER BY CONVERT(VARCHAR(20),DATE,110))AS R1,
-
CONVERT(VARCHAR(20),DATE,110) DATE1,Call_Type,Table_Name.Count AS KOUNT
-
FROM Table_Name
-
GROUP BY CONVERT(VARCHAR(20),Date,110),Call_Type
-
) AS T1,
-
-
( SELECT ROW_NUMBER() OVER (ORDER BY CONVERT(VARCHAR(20),DATE,110))AS R2,
-
CONVERT(VARCHAR(20),DATE,110) DATE1,Call_Type,Table_Name.Count AS KOUNT
-
FROM Table_Name
-
GROUP BY CONVERT(VARCHAR(20),Date,110),Call_Type
-
) AS T2
-
WHERE DATEDIFF(DD,T1.DATE1,T2.DATE1) BETWEEN 0 AND 7
-
GROUP BY T1.R1,T1.DATE1,T1.Call_Type
-
) AS DATES
-
WHERE FROMDATE BETWEEN (@DateGiven - (7*12)) AND @DateGiven AND R1 % 7 = 1
-
thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: cooldv |
last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and
Access 2000 database. (with a hosting company)
traffic is slow at this time but expect to grow. lately i have been
reading...
|
by: Peter |
last post by:
I run most of my SQL scripts via kornshell on AIX.
I use the "here-document" to run some of the smaller ones.
Example:
#!/bin/ksh
# Analyze the table.
sqlplus...
|
by: Jan |
last post by:
I store sql-commands in a database table. In the first step I get the
sql command out of the database table with embedded sql. In the second
step I try to execute the command, which i got from the...
|
by: Dagwood |
last post by:
Good morning:
At least it's morning where I am. :)
I have a rather newbie question I'm afraid. I have VisualStudio.NET, and
have installed it along with SQL server. However I can't seem to...
|
by: Mark Yudkin |
last post by:
The documentation is unclear (at least to me) on the permissibility of
accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003,
with separate transactions scope, from separate...
|
by: coosa |
last post by:
Hi,
I was installing SQL Server on my machine and during installation my PC
freezed. It happens frequently on my machine. So i tried after
restarting to install it again and since then i always...
|
by: Peter |
last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server
2005 Express, but get the following error in the error log.
Please could someone help me....
Microsoft SQL Server 2005...
|
by: Fuzzydave |
last post by:
I am back developing futher our Python/CGI based web application run by
a Postgres DB
and as per usual I am having some issues. It Involves a lot of Legacy
code. All the actual
SQL Querys are...
|
by: Developer |
last post by:
Hello All,
i have recently installed VS2005 and was trying to install SQL sever
2000.
I have Win XP' SP2. But when I tried installing, it only installed
client tools and not the database.
Can...
|
by: dbrother |
last post by:
Access 2003 Win XP Pro SP3
Using SQL /ADO Recordsets in a Do Loop
Hello,
I'm using a random number generator based on an integer input from a user from a form that will get X number of random...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |