SSIS Subtracting from 2 sources - sql

I currently have two Excel sources. I'm hoping to subtract a count of rows from one Excel source (SourceA) with a row value from the other excel source (SourceB).
I've used a conditional split on each to specify which rows I want to use. SourceA returns one row which is what I wanted. Source B returns a number of rows which is what I expected.
From the SourceA data flow, I've now added an aggregate transformation to count the number of rows .
I then use a Union All, a data conversion transformation and then a Derived Column transformation. In this Derived Column transformation, I use the column from SourceB - the aggregate count of SourceA.
Then I link it to a SQL Server Destination and configure the mappings. I run the data flow and everything works. However, when I look at the results, it only gives me a NULL value (it did not calculate it for me).
How can I achieve this subtraction of a row value and an aggregate count?

The aggregate transformation is asynchronous meaning that it won't produce an output for each input row. You probably need a Merge Join instead of a Union. You may want to consider using a variable to hold the aggregated value as well.

In order to achieve what you are looking by following:
1) Get the Row count From Source A and Source B by creating variable1 and variable2.
2) You can do drag Derived column or another variable you can do the subtraction of Variable1-Variable2.
3) Map the Final variable or Derived column to the destination.

Related

How to add column to an existing table and calculate the value

Table info:
I want to add new column and calculated the different of the alarmTime column with this code:
ALTER TABLE [DIALinkDataCenter].[dbo].[DIAL_deviceHistoryAlarm]
ADD dif AS (DATEDIFF(HOUR, LAG((alarmTime)) OVER (ORDER BY (alarmTime)), (alarmTime)));
How to add the calculation on the table? Because always there's error like this:
Windowed functions can only appear in the SELECT or ORDER BY clauses.
You are using the syntax for a generated virtual column that shows a calculated value (ADD columnname AS expression).
This, however, only works on values found in the same row. You cannot have a generated column that looks at other rows.
If you consider now to create a normal column and fill it with calculated values, this is something you shouldn't do. Don't store values redundantly. You can always get the difference in an ad-hoc query. If you store this redundantly instead, you will have to consider this in every insert, update, and delete. And if at some time you find rows where the difference doesn't match the time values, which column holds the correct value then and which the incorrect one? alarmtime or dif? You won't be able to tell.
What you can do instead is create a view for convenience:
create view v_dial_devicehistoryalarm as
select
dha.*,
datediff(hour, lag(alarmtime) over (order by alarmtime), alarmtime) as dif
from dial_devicehistoryalarm dha;
Demo: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b7f9b5eef33e72955c7f135952ef55b5
Remember though, that your view will probably read and sort the whole table everytime you access it. If you query only a certain time range, it will be faster hence to calculate the differences in your query instead.

How can I create multiple rows based on the value of one column in SQL?

I have a column of type string in my table, where multiple values are separated by pipe operator. For example, like this,
Value1|Value2|Value3
Now, what I want is to have a query, which will show three rows for this row. Basically something similar to the concept of explode in Dataframes.
Note that I am using Spark SQL. And I want to achieve this using SQL, not dataframes.
I got it working by using the following query.
select t.*, explode(split(values, "\\|")) as value
from table t
\\| here can also be replaced by [|]. Just specifying | doesn't work.

Removing rows with duplicated column values based on another column's value

Hey guys, maybe this is a basic SQL qn. Say I have this very simple table, I need to run a simple sql statement to return a result like this:
Basically, the its to dedup Name based on it's row's Value column, whichever is larger should stay.
Thanks!
Framing the problem correctly would help you figure it out.
"Deduplication" suggests altering the table - starting with a state with duplicates, ending with a state without them. Usually done in three steps (getting the rows without duplicates into temp table, removing original table, renaming temp table).
"Removing rows with duplicated column values" also suggests alteration of data and derails train of thought.
What you do want is to get the entire table, and in cases where the columns you care about have multiple values attached get the highest one. One could say... group by columns you care about? And attach them to the highest value, a maximum value?
select id,name,max(value) from table group by id,name

Spotfire - Getting data from one table that falls between two dates in another table and adding to a calculated column

What would be the expression to create a calculated column in Table Example 2 called "SZODMAXCALC", that would contain the SZODMAXCALC from Table Example 1 given that the data from Table Example 1 falls between the dates (DTTMSTART and DTTMEND) within Table Example 2?
Maybe this is easier done on the SQL side that loads the data?
there is no way to create a calculated column that references a column in another table.
you will need to do a join either in Spotfire (via Insert...Columns)* or on the SQL-side of things (either via a view on your database or by creating a new information link in Spotfire).
the best method depends on your data structure, implementation, and desired results, so I'm not able to recommed there. take a look at both options and evaluate which one works best.
* NOTE that Spotfire cannot join based on a Calculated Column as a common key. that is, using your example, if [WELLNAME] is a calculated column, you cannot tell Spotfire the equivalent of SELECT wellname, ... FROM table_a LEFT JOIN table_b ON table_a.wellname = table_b.wellname.
the alternative is to Insert...Transformation and choose Insert New Calculated Column, and to join on that instead.
the reason for this is that calculated columns are very mutable; they could change frequently based on a user action. it would be inefficient to re-execute the join each time the column's contents changed. conversely, a "Transformation Calculated Column" is only updated when the data table is loaded.

Get row values as column names in t-sql

I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]