sql server 2005 - SQL Pivot based on value between two columns -


I have a table in which I have two columns, among other things, for a start date and the other end date for. I have to write a question which will return a column for each month of the year and the value of that column is 1 if the month is between 0. PIVOT statement What I see here, but best I can tell PIVOT section is looking to match values, do not check that the value is between the other two or not Whether the PIBT clause is constructed correctly Or do I need to break and write details of 12 cases and then add them?

I think there are three basic steps I get here:

  1. Get a date in every 12 months
  2. Check whether this date is between start and end date
  3. The result of PIVOT
< P> To get 12 dates, one for each month, I have a few recursive with a statement to create a temporary table with 1 column of 12 dates Used as the date (date) as the date (date) of the month (date) (select date) Unius Select date from all months (month, 1, date) where the data (month, date, Date) & lt; 12)

From here I can include this table with the table which I really care about. Then I use the date where any entry starts and ends to start not related to that month. Then select something like this:

  Join the titles, month (months) cross, where others during the months. Start and other .end  

In this phase, we should be careful that we select only the columns, which will result in the desire to become extinct, or that will be collected using the PVOT statement. Finally, we should get the result:

  PIVOT (for PID) IN IN ([1], [2], [3], [ 4], [5], [6], [7], [8], [9], [10], [11], [12])  

Something can look like:

) Select Unisense as the date (select date) Month month (month, 1, date) where statistics (month, date, date) & lt ; 12) Selection title, [1] as of January, [2] as of February, [3] AS April, [5] AS May, [6] June, [7] as of July, [ 8] As of August, [9] as September [10] as of October, [10] [11] AS Nov, [12] from AS December (SELECT ot him. Title, mohnt (months) cross second Join in, sub-sequential PIVOT (maximum (PID)) [4], [5] for the date of ([1], [2], [3] in the form of other start dates and other. , [6], [7], [8], [9], [10], [11], [12]) ASP

I have snatched all the other complications that I went through to include other information , So I have not really written this question, but must be taught the original query structure, which was the result I needed.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -