Tuesday, July 25, 2017

SSIS - Looping with ADO Enumerator

In SSIS, using the Foreach Loop Container we can loop around 7 different things: file, item, ADO, ADO.NET Schema Rowset, Variable, Nodelist and SMO.
  1. File: we loop around a collection of files in a folder
  2. Item: we loop around rows of data that we define ourselves (can be more than 1 column)
  3. ADO: we loop around rows of a existing table
  4. ADO.NET schema rowset: we loop around a collection of any of these: Catalogs, Character Sets, Check Constrains, Collations, Column Privileges, Columns, Columns Domain Usage, Constraint Column Usage, Constraint Table Usage, Foreign Keys, Indexes, Key Column Usage, Indexes, Primary Keys, Procedure Columns, Procedure Parameters, Procedures, Provider Types, Referential Constraints, Schemata, Statistics, Table Constraints, Table Privileges, Tables, Translations, Usage Privileges, View Column Usage, Views, View Table Usage, SQL Languages, DB Info Keywords, DB Info Literals, Check Constraints by Table, Table Statistics, Tables Info, and Trustees.
  5. Variable: we loop around the values of a variable, for example a DataTable where we loop around the rows.
  6. Nodelist: we loop around a set of XML nodes, which are the results of applying XPath expression to an XML file.
  7. SMO: we loop around SQL Server Management Objects, such as tables, users, statistics, jobs, traces, brokers.
In this article we will use ADO enumerator. We’ll create a package that:
  1. Create a DataSet
  2. Create a DataTable in the DataSet
  3. Populate the DataTable with 2 rows, row 1: file1.txt, row 2: file2.txt
  4. Assign that DataSet to variable Var1
  5. Have a FELC ADO Enumerator with source = Var1 to loop around the rows of the DataTable
  6. Set the Variable Mapping of this FELC to Var2
  7. Inside this FELC loop create a Script Task that checks if a file with file name = the value of Var2 exists or not.
The overall package looks like this:
True, we don’t need to use FELC to loop around the DataTable rows. We could do it in the first C# Script task, right after step 3 (populate the DataTable). The reasons of we might want to use FELC are a) to use SSIS functionality such as transaction and checkpoint, b) to reuse the variable that was transferred to the FELC in downstream task(s).
1-4 Create & Populate DataTable, assign to Var1
OK let’s start. First create a variable called Var1, of type object, scope = package:
Create the Script Task, and set Var1 as ReadWrite variable:
Edit Script, and create the DataSet and DataTable:
// Create the table
DataSet DS1 = new DataSet();
DataTable T1 = new DataTable();
DataColumn C1 = new DataColumn();
C1.DataType = Type.GetType(“System.String”);
C1.ColumnName = “Col1”;
T1.Columns.Add(C1);
DS1.Tables.Add(T1);
Usually we populate the datatable from a database table, but in this instance I’m going to populate it from a file:
// Populate the table from the file
StreamReader sr1;
sr1 = new StreamReader(“C:\\Data\\FELC per row\\list.txt”);
String s1;
DataRow DR1;
while ((s1 = sr1.ReadLine()) != null)
{
DR1 = DS1.Tables[0].NewRow();
DR1[“Col1”] = s1;
DS1.Tables[0].Rows.Add(DR1);
}
sr1.Close();
The List.txt file only contains 2 lines:
After populating the DataSet, we assign it to Var1:
// Set VariableDts.Variables[“Var1”].Value = DS1;Dts.TaskResult = (int)ScriptResults.Success;
5. Create the Foreach Loop Container
Have a FELC ADO Enumerator with source = Var1 to loop around the rows of the DataTable
Set the collection of the FELC to ADO enumerator, rows in the first table:
6. Set the Variable Mapping
Set the Variable Mapping of this FELC to Var2 (create Var2 first, scope = package, type = string):
This means that every row on the DataTable will be assigned to Var2. It’s the first column only that will be assigned (Index 0 = first column).
7. Add a Script Task inside the loop
Add script task inside the loop to process each row. Set Var 2 as ReadOnlyVariables because we want to use within the script.
Display the value of Var2 and check the existance of the files mentioned in Var2:
string Var2 = Dts.Variables[“Var2”].Value.ToString();
System.Windows.Forms.MessageBox.Show(Var2);
if (File.Exists(“C:\\Data\\FELC per row\\”+Var2))
{
System.Windows.Forms.MessageBox.Show(Var2 + ” exists”);
}
else
{
System.Windows.Forms.MessageBox.Show(Var2 + ” does not exist”);
}
Dts.TaskResult = (int)ScriptResults.Success;
Apologies about the indentation. Within {} the code should be indented, but WordPress wouldn’t let it. Unless I format it as “preformatted”. But using preformatted the font would appear very small.
Now if we run it, it will display the value of Var2 and whether that file exist or not:

SSAS MDX: Filter

We can filter an axis based on the members, or based on the measure value.
Based on the members: begins with F or after      Based on the measure value: Sales > $40,000
To filter based on members, we can use string comparison:
select non empty
filter
( {[Customer].[City].[All].Children},
[Customer].[City].CurrentMember.Name > “F”
) on 1,
[Measures].[Internet Sales Amount] on 0
from [Adventure Works];
Or use string functions:
select non empty
filter
( {[Customer].[City].[All].Children},
left([Customer].[City].CurrentMember.Name,1) = “F”
) on 1,
[Measures].[Internet Sales Amount] on 0
from [Adventure Works];
To filter based on measure value, we use numeric comparison:
select non empty
filter
( {[Customer].[City].[All].Children},
[Measures].[Internet Sales Amount] > 40000
) on 1,
[Measures].[Internet Sales Amount] on 0
from [Adventure Works];
Other examples of string functions:
Second letter is A:
mid([Customer].[City].CurrentMember.Name,2,1) = “A”
Ends with A:
right([Customer].[City].CurrentMember.Name,1) = “A”
Begins with O and ends with A:
left([Customer].[City].CurrentMember.Name,1) = “O” and
right([Customer].[City].CurrentMember.Name,1) = “A”

SSAS MDX- MDX: Sorting

We can sort an axis based on the member on that axis, or on the measure.
Based on the member:                        Based on the measure:
To sort an axis based on the member, we can use the NAME property:
select non empty
order
( {[Customer].[City].[All].Children},
[Customer].[City].CurrentMember.Name, DESC
) on 1,
[Measures].[Internet Sales Amount] on 0
from [Adventure Works];
To sort an axis based on the measure, we use the measure name:
select non empty
order
( {[Customer].[City].[All].Children},
[Measures].[Internet Sales Amount], DESC
) on 1,
[Measures].[Internet Sales Amount] on 0
from [Adventure Works];

Sunday, July 23, 2017

SQL Server - GROUP BY Clause

The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped according to one or more columns.
For Example: If you want to know the total amount of salary spent on each department, the query would be:
SELECT dept, SUM (salary) 
FROM employee 
GROUP BY dept; 
The output would be like:
deptsalary
------------------------------
Electrical25000
Electronics55000
Aeronautics35000
InfoTech30000
NOTE: The group by clause should contain all the columns in the select list expect those used along with the group functions.
SELECT location, dept, SUM (salary) 
FROM employee 
GROUP BY location, dept;
The output would be like:
locationdeptsalary
---------------------------------------
BangaloreElectrical25000
BangaloreElectronics55000
MysoreAeronautics35000
MangaloreInfoTech30000

SQL Server -SQL WHERE Clause

he WHERE Clause is used when you want to retrieve specific information from a table excluding other irrelevant data. For example, when you want to see the information about students in class 10th only then you do need the information about the students in other class. Retrieving information about all the students would increase the processing time for the query.
So SQL offers a feature called WHERE clause, which we can use to restrict the data that is retrieved. The condition you provide in the WHERE clause filters the rows retrieved from the table and gives you only those rows which you expected to see. WHERE clause can be used along with SELECT, DELETE, UPDATE statements.

Syntax of SQL WHERE Clause

WHERE {column or expression} comparison-operator value

Syntax for a WHERE clause with Select statement is:

SELECT column_list FROM table-name 
WHERE condition;
  • column or expression - Is the column of a table or a expression
  • comparison-operator - operators like = < > etc.
  • value - Any user value or a column name for comparison
or Example: To find the name of a student with id 100, the query would be like:
SELECT first_name, last_name FROM student_details 
WHERE id = 100;
Comparison Operators and Logical Operators are used in WHERE Clause. These operators are discussed in the next chapter.
NOTE: Aliases defined for the columns in the SELECT statement cannot be used in the WHERE clause to set conditions. Only aliases created for tables can be used to reference the columns in the table.

How to use expressions in the WHERE Clause?

Expressions can also be used in the WHERE clause of the SELECT statement.
For example: Lets consider the employee table. If you want to display employee name, current salary, and a 20% increase in the salary for only those products where the percentage increase in salary is greater than 30000, the SELECT statement can be written as shown below
SELECT name, salary, salary*1.2 AS new_salary FROM employee 
WHERE salary*1.2 > 30000;
Output:
namesalarynew_salary
-------------------------------------
Hrithik3500037000
Harsha3500037000
Priya30000360000
NOTE: Aliases defined in the SELECT Statement can be used in WHERE Clause.

Search This Blog

DAX - Grouping on multiple columns and the count

Please go thorugh the below URL for entire soultion. http://community.powerbi.com/t5/Desktop/DAX-Grouping-on-multiple-columns-and-the-cou...