SQL Server - SQL syntax (Newbie Question)

Asked By Alastair MacFarlane
19-Aug-08 10:55 AM
Dear Group,

I have a table (Members) which has one field (for arguement's sake) which is
a datetime field called JoinDate which stores various dates.

I would like to create a SQL statement that could give me the aggregated
result:

August : 3
July : 45
June: 84

It is a total for each month. I would assume that I would use the DatePart
function to group and then count and specify that I only want the last three
months data but I am unsure of how to do this exactly.

Any hints would be appreciated. Thanks again.

Alastair MacFarlane
Grouping
(1)
ArunDhaJThanks
(1)
FromMembers
(1)
JoinDate
(1)
DatePart
(1)
Ratchev
(1)
Plamen
(1)
June
(1)
  ArunDhaJ replied...
21-Aug-08 11:08 PM
Select DatePart(mm, JoinDate), Count(DatePart(mm, JoinDate)) From
Members Group By DatePart(mm, JoinDate)

Hope I understood the problem correct. This query may help...


-ArunDhaJ
  Plame replied...
21-Aug-08 11:08 PM
You can use the DATENAME function if you need to get results as
posted:

SELECT DATENAME(MONTH, JoinDate) AS join_month,
COUNT(*) AS cnt
FROM Members
GROUP BY DATENAME(MONTH, JoinDate);

However, this may not give you the correct results if the data spans
over multiple years. You can include the year in the grouping:

SELECT CONVERT(CHAR(7), JoinDate, 126) AS join_year_month,
COUNT(*) AS cnt
FROM Members
GROUP BY CONVERT(CHAR(7), JoinDate, 126);


Plamen Ratchev
http://www.SQLStudio.com
  Alastair MacFarlane replied...
19-Aug-08 11:29 AM
ArunDhaJ

Thanks for the speedy reply.

How could I specify for the last three months including the month we are
currently in? I presume this would be the WHERE clause.

Thanks again.

Alastair
  Plame replied...
21-Aug-08 11:08 PM
I forgot to include the conditions for the last 3 months:

SELECT DATENAME(MONTH, JoinDate) AS join_month,
COUNT(*) AS cnt
FROM Members
WHERE JoinDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
- 2,
0)
AND JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
+ 1,
0)
GROUP BY DATENAME(MONTH, JoinDate);

SELECT CONVERT(CHAR(7), JoinDate, 126) AS join_year_month,
COUNT(*) AS cnt
FROM Members
WHERE JoinDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
- 2,
0)
AND JoinDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP)
+ 1,
0)
GROUP BY CONVERT(CHAR(7), JoinDate, 126);


Plamen Ratchev
http://www.SQLStudio.com
Create New Account
help
using Sql Server 2000 SQL Server I want to get the top row for a grouping. Grouping is on columns 1 and 2 and I want to order group by column 3 that top row. I am using Sql Server 2000. Thanks, maa SQL Server Programming Discussions Grouping (1) Inner (1) Alejandro (1) Mesa (1) Gina (1) Table2.maxc3 (1) Trickselect (1) Table2 Groups, using, Sql, Server, 2000 description: I want to get the top row for a grouping. Grouping is on columns 1 and 2 and I want to order group by column 3
select case when grouping(year) = 0 then cast(year as varchar(10)) else'Total' SQL Server select case when grouping(year) = 0 then cast(year as varchar(10)) else 'Total' end as year, month, SUM cost) as tCost from tblCost group by year, month with rollup having grouping(year) = grouping(month) SQL Server Programming Discussions SQL Server (1) Grouping (1) Varchar (1) Cast (1) Rollup (1) This sounds like something the presentation tier / reporting Server MVP http: / / www.sqlblog.com / http: / / www.aspfaq.com / 5006 keywords: select, case, when, grouping(year) = 0, then, cast(year, as, varchar(10)), else'Total' description: select case when grouping(year) = 0 then cast(year as varchar(10)) else 'Total' end as year, month, SUM
How do I get control of Entity grouping? SQL Server Is there a way to discourage Entity grouping? Value groups eliminate redundant values in reports; entity groups apply the same logic to entity rows. It's possible to switch off value grouping by setting 'DiscourageGrouping' to true for each attribute / value / field in a query. How does one switch off entity grouping? [Goal: allow users to extract data to, say, Excel, in a way that lets them be in control of the data] More detail, if the above is too abstract. . . Value Grouping Let's say a raw query returns something like Example 2 columns: this.a, this.b value: 1, 1 1, 2 1, 3 2, 4 2, 5 Value grouping will notice the redundancy and bundle like values together, e.g. Example 2 1, 1 export the five rows for processing by something else, say Excel. You can then disable grouping by setting 'DiscourageGrouping' to true, which will produce data as in Example 1. However, if you have two tables, 'this' and 'that', entity grouping will group all repeated values from table 'this' and collect the matching 'that' rows just
Summing a Calculated field SQL Server I have a grouping named “table1_Portfolio_Desc” that I am trying to Sum a calculated field in reporting services. Problem is, the calculation is at the “detail” grouping level which uses a sum of another field in the very same grouping level. (The next outer level grouping called “table1_Portfolio_Desc”) Below is a view of the table results I want to end I have a Calculated field in the textbox number “163” at the “table1_Portfolio_Desc” grouping sum level that looks like this: “Wtd Avg” Calculation at the Grouping Sum level: = Sum(Fields!SORIG_COST.Value, "table1_Details_Group") / Sum(Fields!SORIG_COST.Value)*Sum(Fields!MOD_DUR.Value “ - - Marty Cline SQL Server Reporting Services Discussions SQL Server (1) COST.Value (1) Grouping (1) RsInvalidAggregateScope (1) Database (1) Report (1) RunningValue (1) ReportItem (1) This is a very as where I would use this calculated field, but would I just drop the Details grouping designators? I would think it would lose its mind trying to figure out at what