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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 February 21st, 2013, 04:57 AM
RadioX RadioX is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 RadioX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 50 sec
Reputation Power: 0
Question MS SQL: Count payments for a given periode

Hi,
I am trying to solve a problem..
I got two tabels, one customer table where customerStartDate is stored.And one payment table where several paymentDates can be stored.

The customers have a customerStartDate that is not equal to the first paymentDate. The periode between the customerStartDate and the paymentDates varies from customer to customer. I would like to be able to count the numbers of first payments for a given month/periode.

This is what I got this far..


Code:
SELECT cus.CustomerStartDate, COUNT(cus.CustomerNo)AS NumberOfWeb
FROM Company.dbo.Customer cus
WHERE cus.SourceCode = 'Web'
AND cus.customerStartDate > '2012-01-01'

AND EXISTS (
	SELECT 'x'
	FROM Company.dbo.Payment py
	WHERE cus.CustomerNo = py.CustomerNo
	AND py.PaymentAmount > 0
	AND py.PaymentDate BETWEEN '2013-01-01' AND '2013-02-01'
	having count(*) = 1) 


It only counts who has had a payment during the period, but not who have had their first payment during this periode. Any suggestions on how I can fix this?

Thank you!

Reply With Quote
  #2  
Old February 21st, 2013, 06:11 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,441 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 8 m 37 sec
Reputation Power: 4141
Code:
SELECT cus.CustomerStartDate
     , COUNT(*) AS NumberOfWeb
  FROM Company.dbo.Customer cus
INNER
  JOIN ( SELECT CustomerNo
              , MIN(PaymentDate) AS first_payment
           FROM Company.dbo.Payment
          WHERE PaymentAmount > 0
         GROUP
             BY CustomerNo ) AS pay
    on pay.CustomerNo = cus.CustomerNo
 WHERE cus.SourceCode = 'Web'
   AND cus.customerStartDate >= '2012-01-01'
   AND pay.first_payment >= '2013-01-01' 
   AND pay.first_payment  < '2013-02-01'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old February 24th, 2013, 01:01 AM
RadioX RadioX is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 RadioX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 50 sec
Reputation Power: 0
Thank you! That's it

What about this case:
Every customer that have had two payments og more, at any time the two last years (after 01-01-2011)?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > MS SQL: Count payments for a given periode

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap