How to combine aggregated result and a table in SSIS? - ssis

I need to get the total transaction in the table and our formula involves a count distinct that's why I used derived column and aggregate transformation.
Now that I have the result, I want to add the output of aggregate transformation with another table. I tried union all, but it adds the output in the last entry.
Example:
Table1
businessdate,storekey,itemkey,vf,trasnumber
1000,200,200,N,1234
1000,200,123,N,1235
1000,200,124,N,1235
1000,200,200,N,1236
1000,200,200,T,1236
AggregateTable
formula: (count distinct transnumber than have vf=n) subtract it with (count distinct transnumber that have vf=t)
result
4
I want the combined table to look like this:
Table2
businessdate,storekey,itemkey,vf,trasnumber,result
1000,200,200,N,1234,4
1000,200,123,N,1235,4
1000,200,124,N,1235,4
1000,200,200,N,1236,4
1000,200,200,T,1236,4
Would like to ask help on how I can add the result of aggregate transformation at the end of each row in table 1.
Thank you very much.

I would add a "Dummy Merge Key" e.g. an integer with a fixed value of 1 to both data flow paths (from Table1 and from AggregateTable).
Then you can use a Merge Join transformation to connect the 2 paths (joined on the "Dummy Merge Key", and add the "result" column.

Add an OLE DB Command transformation after the aggregate. This would contain SQL similar to this update table1 set result = ? where businessdate = ? and storekey = ? You can then tie the "?" parameters to the output from the Aggregate transformation (I'm assuming here that businessdate and storekey are the keys that you've aggregated on)

Related

Sort by specific Column name Matrix in SSRS

I have multiple tables with some rows and columns in sql.
Columns are like (Key, column1,column2,column3), I did unpivot tables in sql.
And got dataset in ssrs like : TableName, Key,ColumnName,ColumnValue.
I've created multiple matrixes like below structure for each table by setting filter = "TableName":
----------------------
Key | [ColumnName]
----------------------
[Key] | [ColumnValue]
------------------------
Question: How I can do sort (order by) based on column in SSRS matrix. I have as I said multiple matrixes. 1st matrix I want to sort by Column1, 2nd matrix by Column2, 3rd Matrix by Column2 and Column3 Where I need mention to sort by certain column name?
So far I've tried to sort my first Matrix.. tried it with "Row Group Properties ----> Sorting ---> Expression : SUM(IIF(ColumnName="Column1",ColumnValue,0)), but it didn't work. Also I've tried COUNT instead of SUM. No luck so far.
I solve it by this...
So if we want to get like "Order by Column1" then after unpivot Put your dataset in the matrixes. Go the the matrix which you want to Sort by specific column (in my case column1) Row group properties---->Sorting----> Add---> Expression:
MAX(
IIF(Fields!ColumnName.Value="Column1",Fields!ColumnValue.Value,Nothing)
)
and if you want to ad also Column2 to this matrix to sort, just after set 1st expression hit "Ok" and in the same window click --->add---> expression----> same query but with your column name (ex. Column2).
Hope this will help to some one.

MySQL aggregate function to filter nulls and conform with ONLY_FULL_GROUP_BY

I have a single record which joins to N other tables, and extracts a single column from each of them. I would like to put all N of those extracted columns in a single record.
After constructing the diagram below it seems like I can get to the second step easily, and then I should be able to use an aggregate function to filter out the NULL's. I have looked around for something like GROUP_COALESCE, but I couldn't find something which accomplishes this.
I have a fiddle here which unfortunately works, because MySQL will let you select columns which aren't in the GROUP BY without an aggregate at your own peril http://sqlfiddle.com/#!9/304992/1/0.
Is there a way I can make sure that it always selects the column from the record, if the record exists?
The end result should one record per group, and each column would contain the value which was inside the only row successfully joined for that group..
If I followed you correctly, you can just use aggregate functions on the columns coming from the joined tables. Aggregate functions ignore null values, so, since you have two null values and one non-null value for each column and each group, this will return the expected output (while conforming to the ONLY_FULL_GROUP_BY option).
SELECT
group_table_id,
MAX(t1.v) t1_v,
MAX(t2.v) t2_v,
MAX(t3.v) t3_v
FROM group_table
LEFT JOIN t1 ON t1.group_id = group_table_id
LEFT JOIN t2 ON t2.group_id = group_table_id
LEFT JOIN t3 ON t3.group_id = group_table_id
GROUP BY group_table_id

MySQL rename column

This is a homework question I had but I just wasn't sure if I got the correct answer. The question was:
Write a SQL statement to count the number of rows in the relation R(A, B, C) and rename the result column as num
My answer:
COUNT(*) AS num
Is this correct or do I have to use ALTER?
No, you've got it right. ALTERis used to change existing database objects (for example the name of a table - it's a data definition statement (DDL)). Using ASgives your result an alias, so this statement:
SELECT COUNT(*) AS num FROM TABLE_A
would count the number of rows in the table and output the result in a column named num. Note that the count is not for distinct rows unless you specify it, or use agroup byclause, so if there are duplicate rows they would all get counted.
Yeah, you got the right answer. Using alias is way in renaming fieldname in using SQL Statement.
SELECT COUNT(*) AS num FROM TABLE

Two datasets issues in ssrs?

I have a column name 'APPs % of total' and it requires two different data sets to be populated .This doesn't seem to work. Any tips will be appreciated. Thanks
=(Fields!AppQty.Value/Fields!AppQty.Value,"second dataset")
The problem is: how does SSRS know which row to pull from the second dataset to get the field? So you have to use aggregation or lookups:
Method 1: Simply aggregate at the current level
There's usually no need for a secondary dataset just for your sums. You can aggregate at the group level within the current dataset by using the following formula:
=Fields!AppQty.Value / SUM(Fields!AppQty.Value, "table1_Group1")
where table1_Group1 is the group where the data is summarised.
Method 2: Aggregate the entire dataset
Aggregate at the dataset level for the either the current dataset or a secondary one:
=Fields!AppQty.Value / SUM(Fields!AppQty.Value, "SomeDataset")
Method 3: Lookup the value from another dataset
You'll need a dataset that sums the values at a group level. You usually achieve this result using method 1 and grouping, but here for completeness. So, let's say you are grouping by DepartmentId, you would have a dataset that aggregates like so:
SELECT DepartmentId, SUM(AppQty) AS AppQty
FROM MyTable
GROUP BY DepartmentId
Then lookup the appropriate value for the department from the current row (in the current table's dataset):
=Fields!AppQty.Value / Lookup(Fields!DepartmentId.Value, Fields!DepartmentId.Value, Fields!AppQty.Value, "SummaryDataset")
So the Lookup matches the DepartmentId from this dataset with the DepartmentId in the SummaryDataset and returns the AppQty value.

Select Left(columnA,2) or right(columnA,1)

Is there a way in MySql to use an either or in a select column. For instance
select left(columnA,2) or right(columnA,1) as columnAlias, sum(columnB)
from table
where ((left(columnA,2) in ('aa','bb','cc')) or (right(columnA,1) in ('a,','b','c')))
group by columnAlias
what I have is a table where either the first 2 characters of the column or the last character of the column indicates the facility. I need to sum the values by facility. A union gets me part way there then I could loop through the resulting dataset and sum things up in the code (or do a stored proc to return the sums), but I am wondering if there is a way to just get it from the query.
I've tried using the union query as an on the fly temp table and doing the select and group on that but if there are no records returned from either of the select statments then it throws a "column columnA cannot be null error.
Also tried with the syntax above, but not getting the results I am expecting. Any other ways to do this through the query?
using a CASE would prob be your best bet here.
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html