c# - SQL Multiple Date Column, Value Bucket -


Please help with this SQL table query, I have the table

  + - ------------------------------------------- + | Date code 1 Date code 2 Date code 3 Date code 4 | + --------------------------------------------- + | 2014-10-01 Faucet 2014_10_04 | | 2014_10_02 2014_10_01 tap 2014_10_04 | | 2014_10_02 tap 2014_10_01 2014_10_04 | | 2014_10_02 tap 2014_10_01 tap | + --------------------------------------------- +// Code >  

If the user is giving input such as- FromDate = '2014-10-01' and ToDate = '2014-10 = 05' I have to show

  + - ------------------------------------------------- - --------------- + | Activity DateTit Date 1_Cnt DateCall 2_Cnt DateCall 3_Cnt DateCol4_Cnt | + ------------------------------------------------- ----------------- + | 2014-10-01 1 1 2 0 | | 2014-10-02 3 0 0 0 | | 2014-10-03 0 0 0 0 | | 2014-10-04 0 0 3 3 | + ------------------------------------------------- ----------------- +  

what I have to show, number of activity on a partial date

is what Calculate the activity from date 1_ATT activity, date column 1 activity_date, calculation (1), where my date 'column2 @ @ @ and @ToDate select all between' Date2_ACTY 'activity, Date column 2 Activity_Date Activity from myTable (count) (1) Activity_count where dateColumn2 @frondate and @ToDate are all .....

column left for now

Please Please help solve

The problem is I am receiving DateCol_Acty , DateCo2_Acty and so on, as column values ​​do not have columns: (

It may be that a UNPIVOT is a new PIVOT What will happen after this? It is not beautiful, but it seems to me that this is the right result, except for losing empty dates, but it may be the door of joining a column with all the dates in the left category. Be determined

MS SQL Server 2012 schema setup .

Create table T (id int, datecall 1 date, date code 2 date, datecall 3 date, datecall 4 date); Insert T values ​​(1, '2014-10-01', blank, empty, '2014-10-04'), (2, '2014-10-02', '2014-10-01', tap, '2014 -10-04'), (3, '2014-10-02', tap, '2014-10-01', '2014-10-04'), (4, '2014-10-02', Tap, '2014-10-01', blank);

Question 1 :

  announcement @ formatted date = '2014-10-01', @ToDate date = '2014 -10-05 'ActivityDate, DateCol1 Select DateCol1_Cnt, DateCol2 DateCol2_Cnt, DateCol3 as DateCol3_Cnt, DateCol4 as DateCol4_Cnt (from selection d, Date Dates as ActivityDate, D2 as the date (Selection) DateCol1, DateCol2, DateCol3, DateCol4 as T-to-date) UNPIVOT (DateCol1, DateCol2, DateCol3, DateCol4) for una (as unpvt) for derivative axis (COUNT (D2)) (DateCol1 , DateCol2, DateCol3, DateCol4)) Private @ Wherefrom @FromDate and @ToDate   

:

  | Workdays | DATECOL1_CNT | DATECOL2_CNT | DATECOL3_CNT | DATECOL4_CNT | | -------------- | -------------- | -------------- | ---- ---------- | -------------- | | 2014-10-01 | 1 | 1 | 2 | 0 | | 2014-10-02 | 3 | 0 | 0 | 0 | | 2014-10-04 | 0 | 0 | 0 | 3 |  

Comments