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

sql-group by week

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?
Mar 4 '08 #1
4 9520
tifoso
41
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
Mar 5 '08 #2
ck9663
2,878 Expert 2GB
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
Mar 5 '08 #3
deepuv04
227 Expert 100+
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]
Mar 5 '08 #4
deepuv04
227 Expert 100+
[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...
Expand|Select|Wrap|Line Numbers
  1. SELECT  FROMDATE,TODATE, Call_Type,KOUNT
  2. FROM (
  3.  
  4.     SELECT T1.R1,T1.DATE1 AS FROMDATE,CONVERT(VARCHAR(20),DATEADD(DD,7,T1.DATE1),110) AS TODATE,
  5.            T1.Call_Type,SUM(T2.KOUNT) AS KOUNT
  6.     FROM 
  7.         (    SELECT ROW_NUMBER() OVER (ORDER BY CONVERT(VARCHAR(20),DATE,110))AS R1,
  8.                    CONVERT(VARCHAR(20),DATE,110) DATE1,Call_Type,Table_Name.Count AS KOUNT 
  9.             FROM   Table_Name
  10.             GROUP BY CONVERT(VARCHAR(20),Date,110),Call_Type
  11.          ) AS T1,
  12.  
  13.         (    SELECT ROW_NUMBER() OVER (ORDER BY CONVERT(VARCHAR(20),DATE,110))AS R2,
  14.                    CONVERT(VARCHAR(20),DATE,110) DATE1,Call_Type,Table_Name.Count AS KOUNT 
  15.             FROM   Table_Name
  16.             GROUP BY CONVERT(VARCHAR(20),Date,110),Call_Type
  17.          ) AS T2
  18.     WHERE DATEDIFF(DD,T1.DATE1,T2.DATE1) BETWEEN 0 AND 7
  19.     GROUP BY T1.R1,T1.DATE1,T1.Call_Type
  20. ) AS DATES
  21. WHERE FROMDATE BETWEEN (@DateGiven - (7*12)) AND @DateGiven AND R1 % 7 = 1
  22.  
thanks
Mar 10 '08 #5

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

Similar topics

3
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...
2
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...
0
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...
10
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...
11
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...
4
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...
1
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...
6
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...
14
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...
5
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....

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.