Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql query

Posted on 2015-02-23
15
Medium Priority
?
227 Views
Last Modified: 2015-03-05
i have a sheet , where , i want to get sum of the amount , which has following condition

if date of column 1 falls between date of column 2 and +1 of date of column 2.

Data-of-Test-Query-22.xlsx
0
Comment
Question by:BeyondBGCM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +3
15 Comments
 

Author Comment

by:BeyondBGCM
ID: 40625779
the date column 1 is PPD , and date column 2 is PSD , and amount column is PaidAmount
0
 

Author Comment

by:BeyondBGCM
ID: 40625781
please refer the attached sheet , for data
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40625794
SELECT SUM(paidAmount) TotalPaidAmount
FROM TableName
WHERE ppd >= psd AND ppd <= psd+1

Open in new window

0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40625796
WHERE PPD <= PSD  AND PPD < PSD  + 1
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40625797
No sheet attached.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40625799
Something like this:

select sum(paidamount) from mytable
where ppd is not null and psd is not null
and ppd between psd and psd+1

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40625800
Ignore my last comment.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40625803
You may have to use the following if these dates have time included with them:

WHERE PPD <= PSD  AND PPD < PSD  + 2

Open in new window

0
 

Author Comment

by:BeyondBGCM
ID: 40626009
Ok, how can I write a regular expression which can tell me a particular row data is not correct date, out of a column , in a table
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40626079
This is a new question. Please add it as new question.
0
 

Author Comment

by:BeyondBGCM
ID: 40627345
The question of sheet is like, it should bring sum of amount for all cpd dates which falls in range of psd and psd +1,

For every year ie if psd is 1jan15 then it should calculate it for every year till 1jan95
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40627358
You are correct. My apology. I was focusing on between date of column 2 and +1 of date of column 2 part.
0
 

Author Comment

by:BeyondBGCM
ID: 40627480
do you have answer now .....?
to elaborate question , i want to share following details

1. the column CPD has date 01-Feb-14 and column PSD has date 01-Jan-14  and amount is 5 , then it should return sum =5
(because 01-Feb-14 will fall between 01-Jan-14 and 01-Jan-15
2. the column CPD has date 01-Feb-15 and column PSD has date 01-Jan-14  and amount is 7 , then it should return sum =0 ,7
(because 01-Feb-15 will fall between 01-Jan-15 and 01-Jan-16

and so on .....
0
 

Author Comment

by:BeyondBGCM
ID: 40627484
in above 2nd case 0 because , it has no amount between 01-Jan-14 and 01-Jan-15
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 total points
ID: 40631884
one very small but vital component of your question appears to be missing I think, the word: "year"

>>i want to get sum of the amount ...
>>if date of column 1 falls between date of column 2 and +1 year of date of column 2.

SELECT SUM(paidAmount) TotalPaidAmount
FROM TableName
WHERE CPD >= PSD and CPD < dateadd(year,1,PSD)


or... if needing this is a more elaborate query, perhaps this:

select somefield, SUM( CASE WHEN CPD >= PSD and CPD < dateadd(year,1,PSD) THEN paidAmount else 0 end )
where ...
group by somefield
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question