Apply grouping to a SQL Server view - sql

I have a view in my SQL Server database named PictureCount. I am getting data according the following query-
with result as
(SELECT CAST(Createddate AS DATE) AS StartDate, COUNT(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID='11' AND
CAST(CreatedDate As DATE) BETWEEN '2014-10-31' AND '2014-12-05'
GROUP BY CAST(Createddate AS DATE)
)
SELECT
(CAST(DATEPART(YYYY, CAST(StartDate AS DATE)) as varchar) + '-' + CAST(DATEPART(MONTH ,CAST(StartDate AS DATE)) AS varchar)) AS StartDate,
TotalPicture
FROM result
Data is appearing in the following format-
But I want data with group by StartDate. I am not able to apply GROUP BY clause. Please tell me if anyone knows about it.
Thanks in advance.

Try this query :
SELECT Cast(Datepart(YEAR, StartDate) AS VARCHAR(4))
+ '-'
+ Cast(Datepart(MONTH, StartDate) AS VARCHAR(2)) AS StartDate,
Count(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID = '11'
AND CreatedDate BETWEEN '2014-10-31' AND '2014-12-06'
GROUP BY Datepart(YEAR, StartDate),Datepart(MONTH, StartDate)

How about this, if you can show data in yyyyMM format rather than yyyy-MM. This query has less no of functions, conversions.
SELECT Datepart(YEAR, StartDate)*100 + Datepart(MONTH, StartDate) as yyyyMM
, Count(ServicePictureID) AS TotalPicture
FROM PictureCount
WHERE ProjectID = '11'
AND CreatedDate BETWEEN '2014-10-31' AND '2014-12-05'
GROUP BY Datepart(YEAR, StartDate)*100 + Datepart(MONTH, StartDate)

Related

How to concatenate datepart in SQL Server?

I am trying to make a master calendar in SQL Server. I am facing the problem with this syntax:
[dateid] as DATEPART(YEAR, [date]) & DATEPART(day, format( [date],'DD')) & DATEPART(month, format([date], 'MM'))
[dateid] as convert(varchar(4), DATEPART(YEAR, [date])) + convert(varchar(2), DATEPART(day, format( [date], 'DD'))) + convert(varchar(2), DATEPART(month, format([date], 'MM')))
[dateid] as cast(DATEPART(YEAR, [date]) as varchar(4)) + cast(DATEPART(day, format( [date], 'DD')) as varchar(2)) + cast(DATEPART(month, format([date], 'MM')) as varchar(2))
These statements are under create table tablename (...).
I have tried above syntax and It is showing:
Conversion failed when converting date and/or time from character string
or
The data types varchar and varchar are incompatible in the '&' operator.
Can someone help me with this, I want to show date like (YearDayMonth)-20160111 - how to concatenate these three ?
Why can't you just use
SELECT FORMAT(SYSDATETIME(), 'yyyyddMM')
or in your CREATE TABLE statement:
CREATE TABLE dbo.YourTable
(
.....
SomeDate DATE,
DateId AS FORMAT(SomeDate, 'yyyyddMM'),
......
)

T-SQL Sorting by year and month

I would like to sort by date according to date yyyy,mm.
I tried using VARCHARS first:
GROUP BY (YEAR(join_date) AS VARCHAR(4)) + '-' + CAST(MONTH(join_date) AS VARCHAR(2))
But converting them to varchar meant my sort went like
2014.1 - 2014.11 - 2014.12 - 2014.2
How could I sort them in properly by year then month order?
SELECT Year(join_date) + MONTH(join_date) AS Date, COUNT(*) AS Count
FROM X
WHERE mtype ='A' AND (join_date BETWEEN DATEADD(year, -5, GETDATE()) AND GETDATE())
GROUP BY YEAR(join_date) + MONTH(join_date)
ORDER BY Date ASC
You just don't need to convert them to varchar, YEAR and MONTH return ints:
SELECT Year(join_date) As JoinedYear, MONTH(join_date) AS JoinedMonth, COUNT(*) AS Count
FROM X
WHERE mtype ='A' AND (join_date BETWEEN DATEADD(year, -5, GETDATE()) AND GETDATE())
GROUP BY YEAR(join_date), MONTH(join_date)
ORDER BY YEAR(join_date) ASC, MONTH(join_date) ASC

Select empty weeks in SQL

I am grouping by weeks using DATEPART in SQL. However, I want to include the empty weeks also, not only weeks that include values. I have a script that works perfectly collecting data by weeks, but not the empty weeks. I´ve been trying to use left outer join but my SQL skills are not all that great. I decided to ask you guys.
Here is my script:
select
DatePart(Year, MYTABLE.CREATION_DATE) as DateYear,
DatePart(Month, MYTABLE.CREATION_DATE) as DateMonth,
DatePart(Week, MYTABLE.CREATION_DATE) as DateWeek,
sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
min(MYTABLE.CREATION_DATE) as DateT
from MYTABLE
where MYTABLE.SEGMENT2 like '00601'
and MYTABLE.SEGMENT4 like '%'
and MYTABLE.CREATION_DATE > '2012-12-30 12:00:00'
and MYTABLE.CREATION_DATE < '2013-12-30 11:00:00'
group by
DatePart(Year, MYTABLE.CREATION_DATE),
DatePart(Month, MYTABLE.CREATION_DATE),
DatePart(Week, MYTABLE.CREATION_DATE)
order by DateYear, DateMonth, DateWeek
And this is the result of that script.
Could you guys possibly give me an example of a join clause could look like in this case?
[updated 2014-01-09 08:06 UTC]
you can create the dates dynamically (DateRange) and the outer-join them to your select like this:
declare #startdate date;
set #startdate = '2012-12-30';
declare #enddate date;
set #enddate = '2013-12-30'
;with DateRange AS
(
SELECT
DatePart(Year, #startdate) as DateYear,
DatePart(Month, #startdate) as DateMonth,
DatePart(iso_Week, #startdate) as DateWeek,
#startdate as DateValue
UNION ALL
SELECT
DatePart(Year, dateadd(dd,7,DateValue)) as DateYear,
DatePart(Month, dateadd(dd,7,DateValue)) as DateMonth,
DatePart(iso_Week, dateadd(dd,7,DateValue)) as DateWeek,
dateadd(dd,7,DateValue)
FROM DateRange
WHERE dateadd(dd,7,DateValue) <= #enddate
), data as (
select
DatePart(Year, MYTABLE.CREATION_DATE) as DateYear,
DatePart(Month, MYTABLE.CREATION_DATE) as DateMonth,
DatePart(iso_Week, MYTABLE.CREATION_DATE) as DateWeek,
sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
min(MYTABLE.CREATION_DATE) as DateT
from MYTABLE
where MYTABLE.SEGMENT2 like '00601'
and MYTABLE.SEGMENT4 like '%'
and MYTABLE.CREATION_DATE > '2012-12-30 12:00:00'
and MYTABLE.CREATION_DATE < '2013-12-30 11:00:00'
group by
DatePart(Year, MYTABLE.CREATION_DATE),
DatePart(Month, MYTABLE.CREATION_DATE),
DatePart(iso_Week, MYTABLE.CREATION_DATE)
)
select
DateRange.DateYear,
DateRange.DateMonth,
DateRange.DateWeek,
data.Value,
data.DateT
from data
right outer join
DateRange
on
data.DateYear = DateRange.DateYear
and data.DateWeek = DateRange.DateWeek
order by
DateRange.DateYear,
DateRange.DateMonth,
DateRange.DateWeek
This should do it. My SQLFiddle is http://www.sqlfiddle.com/#!6/b777f/1/0
declare #WeekTable table(
[WeekDate] datetime
)
declare #startDate datetime,
#EndDate datetime
select #startDate = '1/1/2013',#EndDate = '12/31/2013'
while #startDate<=#EndDate begin
insert into #WeekTable values(#startDate)
set #startDate = #startDate+7
end
select
DatePart(Year, [WeekDate]) as DateYear,
DatePart(Month, [WeekDate]) as DateMonth,
DatePart(Week, [WeekDate]) as DateWeek,
sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
min(isnull(MYTABLE.CREATION_DATE,[WeekDate])) as DateT
from
#WeekTable
left outer join
MYTABLE
on
DatePart(Week,MYTABLE.CREATION_DATE) =DatePart(Week, [WeekDate])
and
DatePart(Year,MYTABLE.CREATION_DATE) =DatePart(Year, [WeekDate])
and
MYTABLE.SEGMENT2 like '00601'
and
MYTABLE.SEGMENT4 like '%'
group by
DatePart(Year, [WeekDate]) ,
DatePart(Month, [WeekDate]),
DatePart(Week, [WeekDate])
order by
DateYear, DateMonth, DateWeek

Filter out rows that have a computed column value of null

I have an SQL query something along the lines of:
SELECT CONVERT(varchar, DATEPART(month, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(day, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(year, titlStreaming)) AS [Day],
COUNT(titlTitleID) AS Total
FROM Title
GROUP BY DATEPART(year, titlStreaming), DATEPART(month, titlStreaming), DATEPART(day, titlStreaming)
ORDER BY DATEPART(year, titlStreaming) DESC, DATEPART(month, titlStreaming) DESC, DATEPART(day, titlStreaming) DESC
That generally returns a table like:
Day | Total
--------------------
4/23/2013 | 2
...
NULL | 14234
What I would like to do is filter out the row that has a NULL value from returning.
Because Day is a computed column obviously I cannot simply do a WHERE Day IS NOT NULL.
I'll admit my knowledge of SQL is quiet lacking so any help or suggestions would be appreciated.
as far as all dateparts depend on titlStreaming a where condition on titlStreaming should be enough.
....
From Title
where titlStreaming is not null
Group by ....
You can use a subquery:
select *
from (SELECT CONVERT(varchar, DATEPART(month, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(day, titlStreaming)) + '/' + CONVERT(varchar, DATEPART(year, titlStreaming)) AS [Day],
COUNT(titlTitleID) AS Total
FROM Title
GROUP BY DATEPART(year, titlStreaming), DATEPART(month, titlStreaming), DATEPART(day, titlStreaming)
) t
where [day] is not null
ORDER BY [day] DESC

How do I group DATE field by YEAR-MM in SQL Server?

I have a date field in a query and I do want to get GROUP BY report like this:
DATE COUNT
2010-01 10
2010-02 2
...
2010-12 24
2012-13 34
What is the proper syntax to obtain this on SQL Server?
All these conversions to string work, but I find this method more efficient, albeit less readable:
SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0), COUNT(*)
FROM dbo.TheTable
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0);
If you don't want to repeat the expression, then:
;WITH x AS (SELECT m = DATEADD(MONTH, DATEDIFF(MONTH, 0, [DATE]), 0)
FROM dbo.TheTable)
SELECT m, COUNT(*)
FROM x GROUP BY m;
This way the output is still a date/time value and can be used that way for other things. And it doesn't involve any messy string conversions.
CONVERT(VARCHAR(7), CreationDate, 120) as Date
You can simply do:
select convert(char(7), #myDate, 20)
Example
declare #myDate as DateTime
set #myDate = '2012-06-23'
select convert(char(7), #myDate, 20)
Output
-------
2012-06
So the full statement would look like:
select convert(char(7), myDate, 20), count(*) as Count
from MyTable
group by convert(char(7), myDate, 20)
Update
The sample data includes the value 2012-13. I am going to assume this is a typo and that the number after the dash represents the month.
SELECT CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR) as Date, count(*) As Count
FROM myTable
GROUP BY CAST(DATEPART(year, dateCol) as VARCHAR) + '-' + CAST(DATEPART(month, dateCol) as VARCHAR)