partitioning by year over historical data - sql-server

I cannot separate data between 2016 and 2017 using my partition clause
select *, COUNT(*) OVER(PARTITION BY [Customer Number] ORDER BY [Week Ending]) FROM [dbo].[mytable]
My output is giving me this:
Week Ending Customer Number
Apr 5 2017 12:00AM 11
Apr 6 2016 12:00AM 2
Apr 12 2017 12:00AM 11
Apr 13 2016 12:00AM 4
Apr 19 2017 12:00AM 11
Apr 20 2016 12:00AM 6
Apr 26 2017 12:00AM 11
Apr 27 2016 12:00AM 11
Aug 2 2017 12:00AM 9
Aug 3 2016 12:00AM 11
Aug 9 2017 12:00AM 11
Basically, I would like to count how many times Customer # appears partitioned on years 2016 and 2017

Add the year to the partition by... and ideally the select too so you know what count goes with what year.
select
*,
TheYear = DATEPART(year,[Week Ending]),
TheYearlyCount = COUNT(*) OVER(PARTITION BY [Customer Number], DATEPART(year,[Week Ending]) ORDER BY [Week Ending])
FROM [dbo].[mytable]

Related

Count Consecutive Days where value greater than 0

Using SQL Server 2012, I am trying create a query that provides me with, say, the top 10 longest wet (or dry) periods from a climate database.
My temp table provides the following data output:
select monthid as [id], date, rain_today
from #raindays
order by monthid asc, date asc
Output:
id date rain_today
-------------------------------
1 24 Dec 2014 2.4
1 25 Dec 2014 0
1 26 Dec 2014 8.7
1 27 Dec 2014 1.8
1 28 Dec 2014 0.3
1 29 Dec 2014 0
1 30 Dec 2014 0
1 31 Dec 2014 0.3
2 01 Jan 2015 0.3
2 02 Jan 2015 0.3
2 03 Jan 2015 18.3
2 04 Jan 2015 0.3
etc. etc.
I would like to return a ranked table that would count the period where rain_today is > 0, (or rain_today = 0) i.e:
Rank Start_Date End_Date Wet Period
----------------------------------------
1 31 Dec 2014 04 Jan 2015 5
2 26 Dec 2014 28 Dec 2014 3
...
The closest I have got from reviewing other similar queries is the following (this is for dry days):
select
#raindays.monthid as id,
min(#raindays.date) as [FirstDryDay],
max(#raindays.date) as [LatestDryDay],
count(*) as countdays
from
(select
monthid,
coalesce(max(case
when rain_today > '0'
then #raindays.date end), '19000101') as latestdry
from
#raindays
group by
monthid) g
join
#raindays on #raindays.monthid = g.monthid
and #raindays.date > g.latestdry
group by
#raindays.monthid
order by
countdays desc
Output:
id FirstDryDay LatestDryDay countdays
-----------------------------------------------
23 21 Oct 2016 31 Oct 2016 11
21 23 Aug 2016 31 Aug 2016 9
**15 23 Feb 2016 29 Feb 2016 7**
10 25 Sep 2015 30 Sep 2015 6
8 28 Jul 2015 31 Jul 2015 4
24 28 Nov 2016 30 Nov 2016 3
29 29 Apr 2017 30 Apr 2017 2
30 30 May 2017 31 May 2017 2
31 29 Jun 2017 30 Jun 2017 2
20 30 Jul 2016 31 Jul 2016 2
7 29 Jun 2015 30 Jun 2015 2
5 30 Apr 2015 30 Apr 2015 1
11 31 Oct 2015 31 Oct 2015 1
17 30 Apr 2016 30 Apr 2016 1
22 30 Sep 2016 30 Sep 2016 1
As you can see, I don't really want to group by id as I want to be able to span over different months and I'm missing other periods that occur earlier in the month. The actual count is working fine correctly it seems, checking above highlighted period:
id date rain_today
15 22 Feb 2016 3.9
15 23 Feb 2016 0
15 24 Feb 2016 0
15 25 Feb 2016 0
15 26 Feb 2016 0
15 27 Feb 2016 0
15 28 Feb 2016 0
15 29 Feb 2016 0
16 01 Mar 2016 3
Thanks in advance for any help!
Is this what you want???
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
id INT NOT NULL ,
[Date] DATE NOT NULL,
Rain_Today DECIMAL(9,2) NOT NULL
);
INSERT #TestData (id, Date, Rain_Today) VALUES
(1, '24 Dec 2014', 2.4),
(1, '25 Dec 2014', 0),
(1, '26 Dec 2014', 8.7),
(1, '27 Dec 2014', 1.8),
(1, '28 Dec 2014', 0.3),
(1, '29 Dec 2014', 0),
(1, '30 Dec 2014', 0),
(1, '31 Dec 2014', 0.3),
(2, '01 Jan 2015', 0.3),
(2, '02 Jan 2015', 0.3),
(2, '03 Jan 2015', 18.3),
(2, '04 Jan 2015', 0.3);
--======================================
WITH
cte_AddRankGroup AS (
SELECT
td.id,
td.Date,
td.Rain_Today,
hr.HasRain,
RankGroup = DENSE_RANK() OVER (PARTITION BY td.id ORDER BY td.Date) -
DENSE_RANK() OVER (PARTITION BY td.id, hr.HasRain ORDER BY td.Date)
FROM
#TestData td
CROSS APPLY ( VALUES (IIF(td.Rain_Today = 0, 0, 1)) ) hr (HasRain)
)
SELECT
arg.id,
BegDate = MIN(arg.Date),
EndDate = MAX(arg.Date),
WetPeriod = IIF(arg.HasRain = 1, 'Wet', 'Dry'),
ConsecutiveDays = COUNT(1)
FROM
cte_AddRankGroup arg
GROUP BY
arg.id,
arg.HasRain,
arg.RankGroup
ORDER BY
arg.id,
MIN(arg.Date);
Results...
id BegDate EndDate WetPeriod ConsecutiveDays
----------- ---------- ---------- --------- ---------------
1 2014-12-24 2014-12-24 Wet 1
1 2014-12-25 2014-12-25 Dry 1
1 2014-12-26 2014-12-28 Wet 3
1 2014-12-29 2014-12-30 Dry 2
1 2014-12-31 2014-12-31 Wet 1
2 2015-01-01 2015-01-04 Wet 4
Edit: Code version using CASE expression in place of IIF...
--======================================
WITH
cte_AddRankGroup AS (
SELECT
td.id,
td.Date,
td.Rain_Today,
hr.HasRain,
RankGroup = DENSE_RANK() OVER (PARTITION BY td.id ORDER BY td.Date) -
DENSE_RANK() OVER (PARTITION BY td.id, hr.HasRain ORDER BY td.Date)
FROM
#TestData td
CROSS APPLY ( VALUES (CASE WHEN td.Rain_Today = 0 THEN 0 ELSE 1 END) ) hr (HasRain)
)
SELECT top 10
arg.id,
BegDate = MIN(arg.Date),
EndDate = MAX(arg.Date),
WetPeriod = CASE WHEN arg.HasRain = 1 THEN 'Wet' ELSE 'Dry' END,
ConsecutiveDays = COUNT(1)
FROM
cte_AddRankGroup arg
WHERE
arg.HasRain = '0' -- Top 10 Dry
--arg.HasRain = '1' -- Top 10 Wet
GROUP BY
arg.id,
arg.HasRain,
arg.RankGroup
ORDER BY
ConsecutiveDays desc, MIN(arg.Date);
Modified original script to produce the Top 10 by each period type which was my ultimate aim (output is from the full dataset):
id BegDate EndDate WetPeriod ConsecutiveDays
31 10 Jun 2017 26 Jun 2017 Dry 17
4 02 Mar 2015 14 Mar 2015 Dry 13
5 12 Apr 2015 24 Apr 2015 Dry 13
20 15 Jul 2016 26 Jul 2016 Dry 12
29 01 Apr 2017 11 Apr 2017 Dry 11
26 17 Jan 2017 27 Jan 2017 Dry 11
23 21 Oct 2016 31 Oct 2016 Dry 11
25 01 Dec 2016 09 Dec 2016 Dry 9
21 10 Aug 2016 18 Aug 2016 Dry 9
21 23 Aug 2016 31 Aug 2016 Dry 9
This problem can be resolved by recursion in this way:
-- this variable is needed to stop the recursion
declare #numrows int=(select count(1) from #raindays)
-- add a row number to the table creating a new table as "tabseq"
;WITH tabseq as (select row_number() over(order by date) as rownum, * from #raindays),
-- apply recursion to tabseq keeping a toggle running totals of wet and dry periods
CTE as
(
select *,
(case when rain_today=0 then 1 else 0 end) as dry,
(case when rain_today>0 then 1 else 0 end) as wet
from tabseq where rownum=1
union all
select s.*,
(case when s.rain_today=0 then cte.dry+1 else 0 end) as dry,
(case when s.rain_today>0 then cte.wet+1 else 0 end) as wet
from tabseq s
join cte on s.rownum=cte.rownum+1
where s.rownum<=#numrows
)
select * from cte
Once you have the table (cte) with the dry/wet accumulators you can order and select from it to suit your output requirements.
Please note that this is assuming consecutive days on the table, if there are gaps then instead of adding +1 on the case statement you may need to add a datediff to one side or the other depending how you consider the missing dates (wet or dry).

How do I get sorted dates using union on multiple select statements?

SELECT ITEM, TXNDate, RIGHT(CONVERT(VARCHAR(10),PREVDATE,6),7), RIGHT(CONVERT(VARCHAR(10),DATEADD(year,1,NEXTVDATE),6),7) FROM TABLE
this retrieves as below
-------------------------------------------
ITEM | TXNDate |PREVDATE | NEXTDATE |
-------------------------------------------
item-A 03 Jan 13 Jan 13 Jan 14
item-C 06 Jan 13 Jan 13 Jan 14
item-B 08 Jan 13 Jan 13 Jan 14
item-A 05 Feb 13 Feb 13 Feb 14
item-B 07 Feb 13 Feb 13 Feb 14
item-B 16 Mar 13 Mar 13 Mar 14
item-A 03 Apr 13 Apr 13 Apr 14
_______________________________________
Here I'm trying to show dates in Ordered manner
SELECT * INTO #DATES FROM (SELECT PREVDATE DATES from #DATA
UNION
SELECT NEXTDATE DATES from #DATA) p
SELECT * FROM #DATES
But I'm getting this result
----------
| DATES |
----------
Apr 13
Apr 14
Feb 13
Feb 14
Jan 13
Jan 14
Mar 13
Mar 14
---------
Please help me to get result as
----------
| DATES |
----------
Jan 13
Jan 14
Feb 13
Feb 14
Mar 13
Mar 14
Apr 13
Apr 14
---------
You can just add ORDER BY to your last SELECT to get the dates in an ascending order:
SELECT * INTO #DATES FROM (SELECT PREVDATE DATES from #DATA
UNION
SELECT NEXTDATE DATES from #DATA) p
SELECT * FROM #DATES
ORDER BY DATEPART(MONTH, CONVERT(DATE, '01 ' + DATES)),
DATEPART(YEAR, CONVERT(DATE, '01 ' + DATES))
As your dates are strings, you need to convert them to dates. Then get the month and year parts as this is the order you want.
The ordering you want in your result is a little strange, because you want Jan 14 to be before Feb 13?
Without an ORDER BY clause you will never be certain of the way the records are ordered. Including this clause is therefore necessary, as explained here
SELECT * INTO #DATES FROM
(SELECT PREVDATE DATES from #DATA
UNION
SELECT NEXTDATE DATES from #DATA
) p
SELECT *
FROM #DATES
ORDER BY DATES

Query to compare two year sales

I have a table in SQL Server with these columns:
Year
Month
Product
Qty
Example:
Year Month Product Qty
2011 1 XYZQW 45
So in this table was stored all product sales.
I need to build a query to compare one year and its previous to build this report:
Year GEN FEB MAR APR MAY GIU JUL AUG SEP OCT NOV DEC
-------------------------------------------------------
2011 12 23 56 54 14 11 15 18 89 87 48 98
2012 19 21 55 50 24 10 19 17 88 81 45 90
There is a way to do this without creating a temporary table?
This is simple, try this:
WITH DT(AMonth,AYear,AQty)
AS (SELECT Month, Year, Qty
FROM YourTable)
SELECT pvt.*
FROM DT cte
PIVOT
(SUM(AQty)
FOR AYear IN ( [2011],[2012],[2013],[2014] ) ) AS pvt

ordering by name, day, date in mysql query?

I have this input.
October 21 , 2010
October 14 , 2007
October 08 , 2010
March 19 , 2009
June 25 , 2009
June 21 , 2013
June 21 , 2013
June 19 , 2006
June 01 , 2009
July 26 , 2013
July 25 , 2012
July 23 , 2013
July 19 , 2013
July 01 , 2009
January 31 , 2013
January 23 , 2013
January 23 , 2011
January 19 , 2013
January 01 , 2000
February 17 , 2011
February 16 , 1998
December 25 , 2003
August 31 , 2013
August 04 , 2013
August 03 , 2013
August 03 , 2013
August 03 , 2013
April 26 , 2013
April 26 , 2013
April 19 , 2005
I want to order this so that the latest dates are on top.. but I can't seem to do it, can someone please provide a solution?
this is my sql:
$sql = "SELECT * FROM $tbl_name WHERE Length(Year) > 7 AND `Title`
LIKE '%" . $q . "%' AND `Genres` LIKE '%" . $genre . "%' AND `Actors`
LIKE '%" . $actor . "%' AND `Year` <> 'Unknown'
ORDER BY `Year` DESC LIMIT $start, $limit";
So, this are 2 columns, right?
Then do it like this:
SELECT
STR_TO_DATE(CONCAT(month_day_column, ', ', year_column), '%M %d, %Y') AS your_ISO_conform_date
FROM yourTable
ORDER BY your_ISO_conform_date DESC
read more about str_to_date() here.
and here is some info about the format you have to supply as argument to str_to_date()
I'm not using MySQL at work, mainly SQL Server, I've found this link within a second: https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date
select str_to_date('October 21 , 2010', '%M %d,%Y')

sql server - help in query

I have a table called employee_salary_master in which we keep salary effective dates and entry dates of each employee in the company.
For processing salary of an employee for a month, we need to fetch most recently entered record. If the effective date is less than
that month then we pick the most recent entry. but if effective date is greater than the first date of the month, then there will be more than
one effective dates for that month
example: the data for an employee is as below.
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
1 5814 Jan 6 2006 Jan 12 2006
2 5814 Jan 10 2006 Jul 17 2006
3 5814 Jan 20 2006 Dec 22 2006
4 5814 May 10 2007 Jul 18 2007
5 5814 Nov 1 2007 Dec 18 2007
6 5814 Aug 1 2008 Aug 20 2008
7 5814 May 1 2008 Sep 2 2008
8 5814 Sep 1 2009 Sep 18 2008
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 5 2009 Nov 26 2009
If i need to get the record for Nov 2009, I write the query below
select EMPLOYEE_SALARY_MASTER_ID, EMPLOYEE_ID, EFFECTIVE_DATE, ENTRY_DATE, ARREAR_PROCESS_FLAG from employee_salary_master esm where employee_id = 5814
and (esm.entry_date = (select max(entry_date) from employee_salary_master where employee_id = 5814 and effective_date <= #monthfirstdate)
or (esm.effective_date between #monthfirstdate and #monthlastdate))
which gives the result below..
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 5 2009 Nov 26 2009
What I need is as follows...
For Nov 1 - Nov 4, salary should be processed as per employee_salary_masterId - 9 and
Nov 5 - Nov 30, salary should be processed as per employee_salary_masterId - 11.
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
11 5814 Nov 5 2009 Nov 26 2009
Please help me build this query.
Not quite sure if I understand you completely correct, but have a look at this example
DECLARE #employee_salary_master TABLE(
EMPLOYEE_SALARY_MASTER_ID INT,
EMPLOYEE_ID INT,
EFFECTIVE_DATE DATETIME,
ENTRY_DATE DATETIME
)
INSERT INTO #employee_salary_master SELECT 1,5814,'Jan 6 2006','Jan 12 2006'
INSERT INTO #employee_salary_master SELECT 2,5814,'Jan 10 2006','Jul 17 2006'
INSERT INTO #employee_salary_master SELECT 3,5814,'Jan 20 2006','Dec 22 2006'
INSERT INTO #employee_salary_master SELECT 4,5814,'May 10 2007','Jul 18 2007'
INSERT INTO #employee_salary_master SELECT 5,5814,'Nov 1 2007','Dec 18 2007'
INSERT INTO #employee_salary_master SELECT 6,5814,'Aug 1 2008','Aug 20 2008'
INSERT INTO #employee_salary_master SELECT 7,5814,'May 1 2008','Sep 2 2008'
INSERT INTO #employee_salary_master SELECT 8,5814,'Sep 1 2009','Sep 18 2008'
INSERT INTO #employee_salary_master SELECT 9,5814,'Nov 1 2008','Apr 20 2009'
INSERT INTO #employee_salary_master SELECT 10,5814,'Nov 10 2009','Nov 25 2009'
INSERT INTO #employee_salary_master SELECT 11,5814,'Nov 5 2009','Nov 26 2009'
DECLARE #monthfirstdate DATETIME,
#monthlastdate DATETIME
SELECT #monthfirstdate = '01 Nov 2009',
#monthlastdate = '30 Nov 2009'
SELECt *
FROM (
SELECT TOP 1
*
FROM #employee_salary_master esm
WHERE esm.EFFECTIVE_DATE BETWEEN #monthfirstdate and #monthlastdate
AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE
ORDER BY esm.ENTRY_DATE DESC
) sub
UNION ALL
SELECT *
FROM (
SELECT TOP 1
*
FROM #employee_salary_master esm
WHERE esm.EFFECTIVE_DATE <= #monthfirstdate
AND esm.EFFECTIVE_DATE < esm.ENTRY_DATE
ORDER BY esm.EFFECTIVE_DATE DESC
) sub
ORDER BY EFFECTIVE_DATE
The rule for excluding nov 10 record is that we need to filter out those records for the month of November in which entry_date is greater but effective_date is smaller.
Let me explain you wrt data provided:
As a rule, the latest/last entry will take precedence over the previous entries in a particular month which implies that the entrty date of Nov 26 2009 would take precedence over the entry date of Nov 25 2009. Now since effective date of SAL_MATSER_ID - 10 is greater than that of SAL_MATSER_ID - 11, hence Nov 10 record would be nullified.
Had the data been like the below, all the 3 records would have been used for salary processing.
SAL_MATSER_ID - 9 for salary of Nov 1 - Nov 9
SAL_MATSER_ID - 10 for salary of Nov 10 - Nov 14
SAL_MATSER_ID - 11 for salary of Nov 15 - Nov 30
SAL_MATSER_ID EMPLOYEE_ID EFFECTIVE_DATE ENTRY_DATE
------------- ----------- -------------- ------------
9 5814 Nov 1 2008 Apr 20 2009
10 5814 Nov 10 2009 Nov 25 2009
11 5814 Nov 15 2009 Nov 26 2009
But since SAL_MATSER_ID - 11 is applicable from 5th Nov. onwards, the previous record is nullified. I hope this explains the situation.
Thanks for your support,
Sweta

Resources