SQL to add only new data - sql

I'm using Pentaho Data Integration, and in it, I make a selection in database X, where I get some information with filters, then I need to update this information in database Y. However, I need to ignore the data already recorded and just add the new data, as can i do this with a sql?

you could use a merge statment for that

if your fields names are same in tables you can use this query:
insert into Y.dbo.Y_1(fieldName)
select fieldName from X.dbo.X_1

Related

Best way to duplicate data in the same table and updating a column at the same time

I'm trying to find the best way to duplicate all the rows in a table.By this i mean inserting them again in the same table, but i need to update a single column only on the values that were inserted.
This is to help me write a script so I can automate some work on some clients.
I can't use select * as it will throw an error because of the identity columns but at the same time i don't want to be manually writting all the column names for several tables.
Is there a simple way to translate this into SQL server?
Sorry for not showing a piece of code, but i have nothing at the moment and i'm not really fluent in SQL.
EDIT: I have ended up following the advice of JamieD77 in the comments below this post by moving everything to a table, drop the id column, updating what i need and then moving back as it seems to be the most effiecient.
In SQL Server Management Studio you can drag the "Columns" folder under a table and drop it on the query window and it will paste a comma-delimited list of all the columns.
Or run a query like:
select string_agg(quotename(name),', ')
from sys.columns
where object_id = object_id('MyTable')

Use SQL Field in SSIS Variable

Is it possible to reference a SQL field in your SSIS variable?
For instance, I would like use the field from the "table" below
Select '999999' AS Physician_Profile_ID
as a dynamic variable (named "CMSPhysProID" in our example) here
I plan on concatenating multiple IDs into a In statement.
Possible by using execute sql taskIn left side pan of Execute SQL task, general tab 1.Select result set as single row2. Connection type ole db 3. Set connection and form SQL statement, As you mentioned Select '999999' AS Physician_Profile_ID 4.Go to result set in your left side pan 5. Add your variable where you want to store '999999' 6. Click ok
If you are looking to store the value within the variable to be used later, you can simply use an Execute SQL Task with a single row result set. More details in the following article:
SSIS Basics: Using the Execute SQL Task to Generate Result Sets
If you are looking to add a computed column while importing data, you must use a Derived Column Transformation within the data flow task to add a column based on another one, you can refer to the following article for more details about this component:
SSIS Derived Columns with Multiple Expressions vs Multiple Transformations
What are you trying to accomplish by concatenating the IDs into an "IN" statement? If the idea is to use the values of the IDs to limit the results, as a dynamic WHERE clause, you may have better luck just using a lookup against either a table you maintain with the desired IDs or even a static list generated in the package with a script task. (If you can use the lookup table method it will be much easier to maintain as you only have to update a table, not your source code.)
Alternatively, you may even be able to accomplish the goal with a join. Create a temp table from the profile IDs you want to keep and join to it, or, again, use it as a lookup component. Dynamically creating a where clause using IN will come in a lot slower and will be cumbersome to maintain.

Convert column oriented table to row oriented one

I have a table structure as below on Greenplum database:
Wish to change it to the following structure so as to support pie charts on Tableau.
Could some one help me out ? Thanks!
Export the table to a CSV file
Install the Tableau Excel add-in
Open CSV file in Excel and use the add-in to reshape the data
SQL Server 2008 : Convert column value to row
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
Just to make sure you know about this Tableau feature:
Once you have devised the SQL select statement that will unpivot the data the way you'd like, then you can tell Tableau to use that instead of a select * by editing the data connection and selecting the Custom SQL option.
The generic way to unpivot in your situation is to union together several select statements, unless your database offers a more efficient alternative as described in the blog entry that Revanayya cited.
The following would work for a static, known beforehand, set of metrics:
SELECT
t.Date,
x.Metric,
CASE x.Metric
WHEN 'metric1' THEN metric1_week
WHEN 'metric2' THEN metric2_week
END AS week_val,
CASE x.Metric
WHEN 'metric1' THEN metric1_13week
WHEN 'metric2' THEN metric2_13week
END AS "13week_val"
FROM
atable AS t
CROSS JOIN
(VALUES ('metric1'), ('metric2')) AS x (Metric)
;
You could build a dynamic query off the above to account for an unknown number of metrics. For that, you would need to read the metadata (probably the INFORMATION_SCHEMA.COLUMNS system view) to build the dynamic bits, which are the VALUES list and the two CASE expressions, before embedding them into the query.

How to create Dynamic SQL in Pentaho Report Designer?

I have a requirement where columns of the SQL will be generated based some selection, these columns are coming from Metric dimension but there is no metric ID present in Fact table.
SQL would be something like below:
SELECT Location_Nbr, Day_Nbr, (Column_List- Coming from some other query)
FROM (Table_Name - Coming from same query which is providing Columns to be selected)
I tried creating the SQL query but I get an error saying there is not a business relation between these tables. Is there any other way to achieve this?
To use Dynamic SQL in Pentaho Report Designer, you need to use
custom jdbc connection in data source
then in master report query name, select message format
type your dynamic query in message Pattern
select
$(dynamic_data) as 'data' from table_name;

MS Access SQL (Quickbooks) Update Query

I'm trying to create an Update Query in MS Access (2013) to a QuickBooks Database using QODBC.
I need to update the table PriceLevelPerItem. I am trying to update the field in said table called PriceLevelPerItemCustomprice with a value from another table, QueryThreeTable, and a column titled UpdatedPrice.
I need to update the table PriceLevelPerItem where the PriceLevelPerItemItemRefListID matches the value of ItemID from QueryThreeTable and ListID matches the QueryThreeTable.ItemListID (yes I know these are the wrong way around...)
So far this process has been a very annoying trial of many queries and any help would be greatly appreciated
This is what I've been working with:
UPDATE
PriceLevelPerItem
SET
(PriceLevelPerItemCustomPrice = QueryThreeTable.UpdatedPrice)
FROM
QueryThreeTable, PriceLevelPerItem
WHERE
QueryThreeTable.ItemID = PriceLevelPerItem.PriceLevelPerItemItemRefListID
AND
QueryThreeTable.ItemListID = PriceLevelPerItem.ListID;
I think the problem is that you're trying to use a DAO query inside a QODBC query. I think the two use different Data Access engines.
You're going to need to lookup your UpdatedPrice in your QueryThreeTable using DLookup. Or maybe you need to create a DAO loop using QueryThreeTable that then updates values in your QODBC table from there.
Make your QODBC query work without the use of QueryThreeTable and without any joins. Then come up with a way to dynamically create your query. You're resulting SQL should look something like this:
UPDATE
PriceLevelPerItem
SET
PriceLevelPerItemCustomPrice = 150.16
WHERE
PriceLevelPerItem.ListID = '310000-1146238368';