Display a yearly summary by month in SQL - sql-server

I have a table that has a date/time field and am trying to figure out how to run a report so that I can view the sum of the amount between each month separately in the 2012 year.
The table has 2 fields, Amount and TimeStamp, and I'm trying to return a report like this:
January, 2012: $221.20
February, 2012: $150.20
etc etc.
Anyone have any ideas how to accomplish this easily in SQL Server? I want to avoid writing a seperate query for each individual month.

This can be solved by using the MONTH and YEAR Functions on SQL.
SELECT
SUM(Amount) as [Amount]
,MONTH(TimeStamp) as [Month]
,YEAR(TimeStamp) as [Year]
FROM
[MyTable]
GROUP BY
MONTH(TimeStamp)
,YEAR(TimeStamp)

Related

Creating Attendance Report with SSRS

I am trying to create a Attendance result set in SQL Server for using it in a SSRS report. The Employee Attendance table is as below:
EmpId
ADate
In
Out
1
2023-01-01
8:00
15:00
I need to calculate the Total working days for all months in a year and display the number of working days per employee. Report format should be as follows:
Saturday and Sunday being weekend, I can able to get the no of working days monthly.
Another table tbl_Holiday has entries for holidays Fromdate and ToDate. I need to consider that also when calculating working days. Several number of results i got from the internet for calculating this. But when creating a view using this data , it has to calculate workdays for each employee row
SELECT
EmpName, EmpId,
(SELECT COUNT(*) FROM tbl_EmpAttendance
WHERE EmpRecId = A.RecId
GROUP BY MONTH(Adate), YEAR(Adate)) AS WorkedDays,
dbo.fn_GetWorkDays(DATEFROMPARTS(YEAR(ADate), MONTH(ADate), 1), EOMONTH(ADate)) AS workingDays
FROM
tbl_Employee A
LEFT JOIN
tbl_EmpAttendance B ON A.RecId = B.EmpRecId
fn_GetWorkDays - calculates the working days for month.
I need to get number of holidays from tbl_holiday too, I understand that this query is becoming more complex than I thought. There must be simpler way to achieve this, can anyone please help?
I tried to get the result in one single view, for using that as SSRS report dataset

SQL Server 2014 Management Studio

I'm a non profit lawyer trying to set up a SQL Server database for my agency. The issue I'm having is query based: I need a simple query that will aggregate the total number of rows on a table, not the sum of the cell contents.
I working with 4 columns of I to: attorney's name, client name, trial date and remedy (the last 2 are date and dollar amount, so integers].
*** Script for SelectTopNRows command from SSMS***
SELECT TOP 100
[attorney]
,[client]
,[trial_date]
,[remedy]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
I'm trying to find a way (script, batch file, etc) that will populate a total number of cases by month (according to trial date) total number of clients, and sum the remedy column.
Sorry for the vagueness. There are privilege rules in place. Hope that helps clarify.
Thanks
Assuming that your case history spans years, not just months, try this:
SELECT
,YEAR([trial_date]) AS [Year]
,MONTH([trial_date]) AS [Month]
,COUNT(1) AS [Trial_Count]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
GROUP BY YEAR([trial_date]), MONTH([trial_date])
If you want to separate this by attorney, you would need to add that column to the SELECT list, as well as the GROUP BY clause, as such:
SELECT
[attorney]
,YEAR([trial_date]) AS [Year]
,MONTH([trial_date]) AS [Month]
,COUNT(1) AS [Trial_Count]
FROM [MyLegalDB]
WHERE [trial_date] between '20160101' and '20160531'
GROUP BY [attorney], YEAR([trial_date]), MONTH([trial_date])
This is a very general answer to a very general question. If you want me to be more specific, I'm going to have to understand your goal a little better. Hope it helps.

Group date by month in Pivot table using data from MS SQL Server

This is my pivot table and I have sales amount from 6 months. I want to group these dates by month but the group button is disabled/not allowing me to do so.

Parameterize MDX in SSRS

Relatively new to MDX but familiar with SSRS and I have a query that works just as I want it to in the query designer in SSMS however I'm not quite sure how to parameterize this in SSRS.
Query returns department and shift as dimensions and Hours and Period as Measures with the query being filtered by a fiscal year and the Period measure as a specific period for that year.
Query works as expected but do not know how to convert to SSRS by implementing parameters for Fiscal Period and Fiscal Year.
Query:
WITH MEMBER [measures].[Period] AS
SUM([Date].[Fiscal Period].[P9],[Measures].[Hours])
SELECT NON EMPTY [Department].[Department].[Department] * [Associate].[Current Shift].[Current Shift] on Rows,
{[Measures].[Hours], [Measures].[Period]} ON Columns
FROM Personnel
WHERE [Date].[Fiscal Year].[FY2015]
Create two parameters for Fiscal year and Fiscal Period. Then use the below query:
SELECT NON EMPTY [Department].[Department].[Department] * [Associate].[Current Shift].[Current Shift] on Rows,
[Measures].[Hours] ON Columns
FROM Personnel
WHERE
(
StrToMember(#FiscalYear, CONSTRAINED),
StrToMember(#FiscalPeriod, CONSTRAINED)
)

Querying a table with a date/time filter in SQL Server 2008

I have a table in my database. This table is called Order. It has a structure like this
ID
CustomerID
OrderDate
Quantity
Price
I need to get all of the orders for the past 2 weeks. How do I do that? I don't understand how to work with dates in this manner in SQL.
Thank you!
You could incorporate something like this into your WHERE clause:
WHERE OrderDate >= DATEADD(day,-14,GetDate())
(i.e OrderDate is more recent than today minus 14 days.)
[I don't have access to SQL Server here so I can't check it - but it might work :)]
Edit: Depending on the exact datatype of OrderDate, I'm not sure what will happen in cases where you have e.g. an order half way through the day two weeks ago, so you might want to check what happens.
marnir answer is the way to do it but this is another option excluding OrderDate > today:
select * from Order
where [OrderDate]
BETWEEN DATEADD(dd, -14, GetDate()) AND GetDate()
Here is another possible way of retrieving orders placed in the last 2 weeks. This is assuming that OrderDate is a column of data type datetime. Screenshot #1 shows sample data in a table named dbo.[Order] similar to your requirements and output of below query against that table data. This query was tested in SQL Server 2008 R2 but is compatible with other SQL Server versions as well.
SELECT Id
, CustomerId
, OrderDate
, Quantity
, Price
FROM dbo.[Order]
WHERE DATEDIFF(WEEK, OrderDate, GETDATE()) <= 2
Hope that helps.
Screenshot #1

Resources