It is necessary to withdraw momentum for a certain period, namely, from October to March. there are datetime1 , datetime2 (with dates) and value .
Closed due to the fact that the essence of the question is incomprehensible by the participants Vladimir Martianov , aleksandr barakin , D-side , zRrr , Grundy 13 Apr '16 at 17:39 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
2 answers
Well, you actually need to ask the question more specifically. Here are a couple of examples. Option number one - select all values in a certain interval:
select * from Periods p where p.BeginDate > '20.01.2014' and p.EndDate < '20.02.2016' In this example, BeginDate and EndDate are your datetime1 and datetime2 . Well, it's just for example. Periods is an arbitrary table from which we make a selection.
Let's move on to the second example, this is if you need a sum for a specific column or turnovers, as you say:
select SUM(p.Value) from PeriodDictionaries p where p.BeginDate > '20.01.2014' and p.EndDate < '20.02.2016' Since I have not heard a specific question, I cannot give a more specific answer. Turnovers for a certain period can be calculated in different ways, there are such names as income, expense, balance, etc., therefore, the results can be different. Both sums, and pivot tables, and differences.
- Yes, thanks, there is in another documentCost table, I need to output this cost for a certain period. how is it for JOINit to the first table with dates? - Niflungar
- To link two tables need some kind of connection, for example by identifier. Add more details. That is, what is in the first table and what is in the second table.
Правильно заданный вопрос - это уже часть ответа.- Denis Bubnov - I succeeded, I added another table to my query with the documentCost and the Client, the very task of such. It is necessary to bring out distinct clients with their total turnover for a certain period, I cannot summarize them. secured the current code - Niflungar
- @Niflungar, oh ... not readable ... now I will try to make out - Denis Bubnov
- Sorry, green is in this business .. - Niflungar
It may come in handy, count the number of records by month using Pivot.
SELECT YourProduct, [01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12] From ( SELECT YourProduct,id,month (transactionStartDate) AS DataS FROM YourDataBase..yourTable )Test PIVOT (Count (id) FOR DataS IN ([01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12]) ) Отчёт