MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 17th, 2008, 04:22 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Angry Monsterous SQL Query... Suggestions?

Hi All,

I work for a telco. We've got a table in a database which shows phone calls made by customers and when they made them.

I need to generate a list of customers who have made phonecalls last month and have NOT had a five days in a row without making any calls.

Can any of you help? I'm not sure how to tackle this one without getting a very bloated and inelligent solution. Basically, the only solution I can think of is generating 31 tables, one for each day and then just checking calls made on each day.

Has anyone got a better idea?

I'm using SQL Server 2000.

Thanks

Dave

Reply With Quote
  #2  
Old January 17th, 2008, 07:50 AM
Death Goddess's Avatar
Death Goddess Death Goddess is offline
Crushing a million faces
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 282 Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 5 Days 11 h 25 m 14 sec
Reputation Power: 89
Quote:
Originally Posted by BanksySan
I need to generate a list of customers who have made phonecalls last month and have NOT had a five days in a row without making any calls.

Can any of you help? I'm not sure how to tackle this one without getting a very bloated and inelligent solution. Basically, the only solution I can think of is generating 31 tables, one for each day and then just checking calls made on each day.

For the love of sweet Sophia, DON'T DO THAT!

All you need is one table:
Code:
Calls
-----
CallID (bigint, identity)
CustomerID (bigint, fk to Customers table)
DateCalled (smalldatetime)


Your database software should have a DateDiff function or something similar.

You can generate a list of calls in the last month with a very simple query:
Code:
SELECT * FROM Calls
WHERE DateCalled between DateAdd(month, getDate(), -1) and getDate()


The other part of your query, "have NOT had a five days in a row without making any calls" is probably not possible to do in SQL. As soon as you start writing queries that don't fit into a relational model, or can't be drawn up in a relational diagram, then your query is probably doing too much.

The most reasonable solution I can think of is creating a seperate table, something that would hold the most frequent calls in a month:
Code:
CallFrequency
----------
FrequencyID (bigint, identity)
CustomerID (bigint, fk to Customers table)
Month (int)
Year (int)
SmallestDurationBetweenCalls (int)


- each time a customer makes a call, look up their last call and calculate the duration in days between their two calls. Update the CallFrequency table if needed.

I'd consider that a hackish solution, but its a lot more reasonable than 31 tables, and its super easy to join on and get the data you want.
__________________
Baby soft, because its made from real babies.

Reply With Quote
  #3  
Old January 17th, 2008, 07:58 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Thank,

Aye, I though it would be a pain in rear. It's not a relational thing. but it is necessary thanks to a contract agreed by some non-IT peoples. I'm thinking some procedural development will be needed.

Thanks again.

Reply With Quote
  #4  
Old January 17th, 2008, 09:12 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
Quote:
Originally Posted by Death Goddess
The other part of your query, "have NOT had a five days in a row without making any calls" is probably not possible to do in SQL.
it most certainly is possible

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old January 17th, 2008, 09:15 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Quote:
Originally Posted by r937
it most certainly is possible



I can think of a way to do it, but it's not exactly pretty and it's not set theory.

Can you think of a way?

Reply With Quote
  #6  
Old January 17th, 2008, 09:26 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
Quote:
Originally Posted by BanksySan
Can you think of a way?
yes, i sure can


Reply With Quote
  #7  
Old January 17th, 2008, 09:29 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Quote:
Originally Posted by r937
yes, i sure can



Please will you tell me?

Reply With Quote
  #8  
Old January 17th, 2008, 09:35 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
Quote:
Originally Posted by BanksySan
Please will you tell me?
i would be happy to

would you kindly show me your table layout, give some meaningful sample rows, and show what output you expect from the sample data

Reply With Quote
  #9  
Old January 18th, 2008, 08:09 AM
marcoV marcoV is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 1 marcoV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 44 sec
Reputation Power: 0
First create a table Callday (fields CallDate and Customer) with a record for every day the customer made at least one call. For calculation of the first and last five days of the month, presume they have called on the last day of previous month and the first day of the next month. In code:
select distinct custmerID, convert(char(8), Date,112) from CallTable where Month(Date) = @thismonth
union
select distinct customerID, dateadd(d, -1, convert(smalldatetime, '2008' + @thismonth + '01') from CallTable
union
select distinct customerID, convert(smalldatetime, '2008' + @nextmonth + '01') from CallTable

Next, create a table CallInterval with for every day a customer called, the next day the same customer made a call.

select customerID, c1.date as day1, min(c2.date) as day2
into CallInterval
from CallDay c1
join CallDay c2 on c1.customerID=c2.customerID and c1.date < c2.date
group by customerID, c1.date

To find the customers who have not five days in a row without making a call:
select distinct customerID
from CallInterval
where customerID not in
(
select customerID
from CallInterval
where Day1 + 5 < Day2
)

piece of cake!
Comments on this post
Death Goddess disagrees: Incomprehensible abuse of select distinct, union, and dateadd logic

Reply With Quote
  #10  
Old January 18th, 2008, 08:25 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Quote:
Originally Posted by r937
i would be happy to

would you kindly show me your table layout, give some meaningful sample rows, and show what output you expect from the sample data


Aye.

It's a table with the following columns

CallID PK
TelephoneNumber
StartDate
Duration

I was going to ignore the duration column and just take the StartDate as the time the call was made and not calculate is a call has gone from one day to the next.

Thanks

Reply With Quote
  #11  
Old January 18th, 2008, 08:53 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
okay, table layout looks workable

how about those sample rows please?

Reply With Quote
  #12  
Old January 18th, 2008, 09:15 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Quote:
Originally Posted by r937
okay, table layout looks workable

how about those sample rows please?


They're just what you'd expect.

CallID PK
TelephoneNumber
StartDate
Duration


CallID = integer
TelephoneNumber = varchar(11) e.g. 01135487625
StartDate = SmallDateTime e.g. '01/12/2007 10:30:00'
Duration = int (Number of minutes on call)

Does this help? It's a simple table.

Reply With Quote
  #13  
Old January 18th, 2008, 09:23 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
no, i meant actual sample rows, so that i could use them to create a test table over here, so that i would have something to test the query on that i was going to write for you

ah, never mind, use marco's

Reply With Quote
  #14  
Old January 18th, 2008, 09:33 AM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 116 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 21 h 12 m 17 sec
Reputation Power: 8
MySpace
Quote:
Originally Posted by r937
no, i meant actual sample rows, so that i could use them to create a test table over here, so that i would have something to test the query on that i was going to write for you

ah, never mind, use marco's


Ah, well, if you are still willing then I'll send it, but in what format do you to use it? XML or csv or the like?

Reply With Quote
  #15  
Old January 18th, 2008, 09:51 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 19,830 r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level)r937 User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 6 Days 9 h 55 m 15 sec
Reputation Power: 1640
there is only one format that will ensure i don't have to put in too much work --
Code:
CREATE TABLE xxxxx
( xxxxx INTEGER NOT NULL
, xxxxx DATETIME NOT NULL
, ...
) 
INSERT INTO xxxxx ( xxxxx, xxxxx, .... ) VALUES ('xxxxx', 'xxxxx', ...)
INSERT INTO xxxxx ( xxxxx, xxxxx, .... ) VALUES ('xxxxx', 'xxxxx', ...)
INSERT INTO xxxxx ( xxxxx, xxxxx, .... ) VALUES ('xxxxx', 'xxxxx', ...)
INSERT INTO xxxxx ( xxxxx, xxxxx, .... ) VALUES ('xxxxx', 'xxxxx', ...)
...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Monsterous SQL Query... Suggestions?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT