Generate a column indicating year and quarter - pivot-table

I have a column with dates for financial transactions. I'd like to group them by quarter for a pivot table report. Not quite sure how to do this.

Assuming your dates are in ColumnA with Row1 a data label: insert =if(month(A2)<10,if(month(A2)>6,"Q3",if(month(A2)>3,"Q2","Q1")),"Q4") in Row2 of a helper column and copy down. Then use the helper column for your pivot table.

Related

SQL Server select distinct some columns but show all

I have a SQL Server database that updates weekly, there is often repeat information but the datetime column is always different. How do I display all columns including the timestamp but only display unique rows not including the timestamp?
I need to keep the information for a month so I am unable to delete the repeat rows. Showing the most recent timestamp row would be ideal, but that does not matter.
Thanks in advance.
SELECT col_a, col_b, col_c...., max(timestamp)
FROM table
GROUP BY col_a, col_b, col_c... -- Same cols as above except timestamp

How to join Year and week column with week dates column

I have these two columns on separate tables:
A column on table 1, which shows the year and the week. There are duplicates as there are different sets of data belonging to each row in this column on the right side of the table, but I have left this out to make the problem clearer:
|.Year Week.|
|..2015 52..|
|..2015 52..|
|..2015 53..|
|..2016 01..|
|....etc....|
|....etc....|
|..2017 23..|
|..2017 23..|
|..2017 23..|
|..2017 24..|
|..2017 25..|
|..2017 25..|
Then there's this column on table 2, which shows chronologically the length of a week as you go down the years in 1-week increments. This is more like a calendar table/database:
|----Week-----|
|080115_140115|
|150115_210115|
|220115_280115|
|.....etc.....|
|.....etc.....|
|090217_150217|
|160217_220217|
|230217_010317|
My question is how do I most efficiently join the weekly column in table 2 with the year week column in table 1 without hard coding any join statements
where [year week] = 'yyyy ww'
etc etc?
I was thinking a joint statement at first but realised I have no common column in both tables.
I have to keep the formats for both tables the same. Was thinking, is there away to extract the weeks from the |year week| column into a separate column then join it that way?
I'm not sure where to begin.
Try extracting the year , week from table 1 using string functions and join with the table 2 year and week. Extract year and week from table 2 as shown below.
year is
select DATEPART( year,CONVERT(DATE,SUBSTRING(SUBSTRING('080115_140115',0,7),5,2) + SUBSTRING(SUBSTRING('080115_140115',0,7),3,2) +SUBSTRING(SUBSTRING('080115_140115',0,7),0,3),102))
week is
select DATEPART( wk,CONVERT(DATE,SUBSTRING(SUBSTRING('080115_140115',0,7),5,2) + SUBSTRING(SUBSTRING('080115_140115',0,7),3,2) +SUBSTRING(SUBSTRING('080115_140115',0,7),0,3),102))
Ok, so this might not be the most efficient answer, but i did a select distinct statement for table 1 then joined table 2 through excel.
Since this is a static/historical table (and i only need to do it once) i imported the template/data from excel into sql again and used year week as the common column to join the two tables.

SQL Server Reporting Service- datasets- grouping

I have table1 that is using dataset-A. I grouped this table by date. I have other dataset B which contains total_qty column.
I have to sum this column (total_qty) and show the result in table1 under every group total.
Data set A columns:
Date
company_code
location_name
volumes
Data set B columns:
Date
Company_code
Total_Qty
How can I get Sum(total_qty) from dataset B into table1 under group total? Also, total_Qty should change as it is placed in group.
Thanks
If you don't need to SUM anything for DataSet B's Company_code, you can use a Lookup to get the value. You would need to match the date and Company_code from both tables.
=LookUp(CSTR(FIELDS!Company_code.VALUE) & "|" & CSTR(FIELDS!Date.VALUE),
CSTR(FIELDS!Company_code.VALUE) & "|" & CSTR(FIELDS!Date.VALUE),
,FIELDS!Total_Qty.VALUE, "DataSetB")
This will combine your company code and date fields into one string and lookup the same combination in DataSet B.

Index on Date field in Calendar table

I have a calendar table that has a list of all days frtom 01-JAN-1990 to 31-DEC-2050
That results in 22279 rows in my table.
A lot of queries we do, I join to the calendar as I need a list of dates based on certain data. For example:
SELECT ...
FROM Person A
INNER JOIN Calendar C
ON C.DateValue BETWEEN A.StartDate and A.EndDate
This is an example... but I'm looking for a list of the dates for the person, and a date column to come back.
What I'd like to know, is: Is the DateValue column a good candicate for an Index? And would there be ebefit of it being Clustered?
(SQL Server 2008 R2)
No, the Date type columns is not good candidate. Columns which you want to choose should be more simple. like int or BigInt types.

How to show data in column in SSRS

I'm using SSRS for my reporting, my reporting solution is in Visual Studio 2008 Business Intelligence Development Studio.
I have a report in which the data should be displayed in this format.
I have added a Column Group in my table which is having the values of Customer Name and details, the data is coming fine in the vertical format i.e column after column.
My Issue :
There should be only three columns in each row, after three records the next row should begin and again not more than three records should be displayed as shown in the image above.
My attempts :
I tried to add a row group and in that gave the expression
= Ceiling(Fields!Row_Count.Value/3)
here Row_Count is a field which is coming from my query which holds the serial number of the records.
My SQl Query
SELECT Row_Number() over(order by table_ID) AS Row_Count, Field_1,Field_2 from MyTable
In my Column group i have Customer Name and in my Row Group i have other details of the customer. The data is getting populated column wise but the issue is its not breaking the current row after three records.
Below is my table of report.
You were on the right track. Say you have data like this:
I have created a tablix like this:
The Row Group expression is:
=Ceiling(Fields!Row_Count.Value / 3)
This works together with the Column Group expression to split over three columns:
=(Fields!Row_Count.Value - 1) Mod 3
The other thing to note compared to your tablix is that CustomerName is not in a table header row, but rather there are two row header rows, one for CustomerName and one for Details.
This is looking OK to me, obviously you can format to taste:

Resources