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

help with group by statement

A client wants to keep track of the number of searches for keywords in
a date range. So, I'm storing each occurance of a search in a table.

The columns are:

PK: id
search_string
search_date

I'm trying to wrap my head around how I would select the number of
occurances for each string, divided by days. The desired result would
look something like:

search_date search_string numOccurances
----------------------------------------------------------
March 20 dogs 4
March 20 pigs 2
March 21 dogs 8
March 22 pigs 3
March 22 pigeons 5

I've tried a query like:
select search_string, CONVERT(CHAR(11),search_date,106) as search_date,
count(search_string) as numOccurances
from searches
where search_date >= dateadd(d,-3,getdate())
group by search_date, search_string
but it doesn't give me the desired results. I'm sure I'm just looking
at it the wrong way.

Suggestions?

Thanks!

Mar 23 '06 #1
2 4238
Hi Jerball

Your only real problem is that "dateadd(d,-3,getdate())" run right now
3/23/3006 at 5:05pm returns
3/20/3006 at 5:05pm so those dated "3/20/2006" are excluded.

"cast(convert(varchar(8), getdate() -3, 112) as datetime)" will return
"3/20/2006" if run today.

I also added a order by to your query.

So the query is:

select search_string, CONVERT(CHAR(11),search_date,106) as search_date,
count(search_string) as numOccurances
from searches
where search_date >= cast(convert(varchar(8), getdate() -3, 112) as
datetime)
group by search_date, search_string
order by search_date, search_string

and I get
search_string search_date numOccurances
------------------- ----------- -------------
dogs 20 Mar 2006 4
pigs 20 Mar 2006 2
dogs 21 Mar 2006 8
pigeons 22 Mar 2006 5
pigs 22 Mar 2006 3

Which I believe is what you want.
--
-Dick Christoph
"jerball" <je*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
A client wants to keep track of the number of searches for keywords in
a date range. So, I'm storing each occurance of a search in a table.

The columns are:

PK: id
search_string
search_date

I'm trying to wrap my head around how I would select the number of
occurances for each string, divided by days. The desired result would
look something like:

search_date search_string numOccurances
----------------------------------------------------------
March 20 dogs 4
March 20 pigs 2
March 21 dogs 8
March 22 pigs 3
March 22 pigeons 5

I've tried a query like:
select search_string, CONVERT(CHAR(11),search_date,106) as search_date,
count(search_string) as numOccurances
from searches
where search_date >= dateadd(d,-3,getdate())
group by search_date, search_string
but it doesn't give me the desired results. I'm sure I'm just looking
at it the wrong way.

Suggestions?

Thanks!

Mar 23 '06 #2
jerball (je*****@gmail.com) writes:
I'm trying to wrap my head around how I would select the number of
occurances for each string, divided by days. The desired result would
look something like:

search_date search_string numOccurances
----------------------------------------------------------
March 20 dogs 4
March 20 pigs 2
March 21 dogs 8
March 22 pigs 3
March 22 pigeons 5

I've tried a query like:
select search_string, CONVERT(CHAR(11),search_date,106) as
search_date,
count(search_string) as numOccurances
from searches
where search_date >= dateadd(d,-3,getdate())
group by search_date, search_string
but it doesn't give me the desired results. I'm sure I'm just looking
at it the wrong way.


I don't know what the search_date in the GROUP BY clause is intended
to mean, but it refers to the table column, not your convert expression.
Either repear the convert in the GROUP BY, or use a derived table:

SELECT search_string, search_date, count(*)
FROM (SELECT search_string,
CONVERT(CHAR(11),search_date,106) as search_date
FROM searches
WHERE search_date >= dateadd(d, -3, getdate())) AS a
GROUP BY search_date

--
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
Mar 23 '06 #3

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

Similar topics

46
by: Kingdom | last post by:
In my data base I have a list of componet types e.g. type A - I have 8 off - type B I have 12 off etc. I'm using Set objRS = objDC.Execute("Select DISTINCT Component_Type FROM Parts_Table") ...
3
by: chowda | last post by:
trying to get to the bottom of this for some time...... eventually to be used with asp. heres the problem the following rather complex SQL statement works fine via query analyser: SELECT...
3
by: Edward | last post by:
I am having conceptual trouble with the following query: select r.ServiceID,r.ContractID,sum(Total) from ( select csc.ServiceID,c.ContractID, sum(csc.ContainerMovement) as Total from...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
1
by: David | last post by:
Hi, I cannot get the following (MS Access) SQL statement working in my asp page, please can anyone help me ? Thanks :-) ------------------------------------------------ <% strQuery =...
6
by: Twobridge | last post by:
I hope someone can help me out with my problem. I have found a sql statement that basically pulls all bills filed within a certain time period and the payments made on those bills with in the...
6
by: troy_lee | last post by:
I am trying to count the total number of units for a given part number that have a Priority rating of 1. Based upon some research, this is what I came up with for my query. Access says that I have...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.