SQL query to retrieve my data - sql

I have a problem and I will explain the situation to understand more my needs:
I have all my data in one table, and I have put it into #All_Data variable. See attached image to see how it looks like
I want to return data showed in #Final_Data table
To do that I have done:
I have created an #All_Lots temp table that contains all lots
And I want to loop on this variable to find how many commands in each lot and put it into the #Final_Data temp table with corresponding Lot
I also want to get the min date and put it for the 3rd column
I have tried this query but it’s not working:
Select
#ALL_Data.Lots,
count(Select #All_Data.Commands
From #ALL_Data
Where #ALL_Data.Lots = #ALL_Lots.Lots)
From
#ALL_Data
But this is not working can you help me please thanks a lot

Related

MS Access - Enter Parameter Value on INSERT INTO

I have a database, which contains information that I can't share images of due to compliance reasons.
I have a table I need to copy data from, so I was using the following SQL:
INSERT INTO completedtrainingstestfinal (MALicenseNum)
SELECT MALicenseNum
FROM CompletedTrainings
WHERE (CompletedTrainings.MALicenseNum IS NOT NULL)
AND (CompletedTrainings.Employee = completedtrainingstestfinal.Employee);
It keeps popping up the Enter Parameter Value, centered on the new table (named completedtrainingstestfinal) at the Employee column.
Background: The original table is a mess, and this is to be the replacement table, I've had to pivot the table in order to clean it up, and am now trying to remove an ungodly amount of nulls. The goal is to clean up the query process for the end users of this who need to put in training and certification/recertification through the forms.
When you look in the old table, it has been designed to reference another table and display the actual names, but as seen in the image below it is storing the data as the integer number Employee.
The new table Employee column was a direct copy but only displays the integer, my instincts tell me that the problem is here, but I have been unable to find a solution. Anyone have any suggestions to throw me in the right direction?
Edited to add: It might be an issue where the tables have different numbers of rows?
This is the design view of the two relevant tables :
Table 1
Table 2

Pentaho replace table name in a loop dynamically

I currently have a transformation setup with 2 table inputs and one Merge Rows (Diff), the SQL select statement in both table inputs are constant, they are not changing except for the table name. So I have:
select * from THIS_WILL_CHANGE
I have around 100 tables and I don't want to manually enter the table names every iteration, especially because this is automation...
What is the best way to achieve this? Is there any way to read like a CSV file with all the table names and loop that way? Any help is appreciated..
This is something I've had to do before too!
You can do this with a variable and a job which executes once for each row of the previous step.
Create a parent job to host these steps
Create a transformation which gets the table names from 'somewhere' eg. CSV file, or database query - a select on all_tables for tables with the same column names might be a nice way to do this for all time...
In this same transformation, use copy rows to result step to push the data back to the job
Create a new 'sub job', which executes once for each row, and has a hop from the 'get data' step in the main job
In the sub job, create two transformations, one to set the variable from the results field, and one to do your select
In your select query, check the box 'substitute variables' and place your variable with the same name as your set variables step into your SQL as ${yourVariableHere}
I've put this in an image below, which hopefully helps you.
The dynamic sql row step is a good option for this, provided the tables all have the same layout/metadata.

Retrieve results from a batch of SQL queries in Pentaho or Postgres?

I'm still relatively new to SQL and Pentaho.
I've pulled a table with two different IDs and need to run a query for each specific instance.
For example,
SELECT *
FROM Table
WHERE RecordA = 'value in column A'
AND RecordB = 'value in column B'
I need the results back, either appended to new columns in the original table or part of their own text file output.
I was initially looking at using a formula for this inside of Pentaho, but couldn't quite figure it out. Since I have the query written I threw it into Excel and got the concatenated results (so a string of 350 or so queries that I need to run). I'm just not sure how to accomplish this - I tried the Execute SQL Script inside of Pentaho but it doesn't seem to do output?
Any direction would be useful. I've searched a little but have come up short so far, possibly because I am still pretty new to this platform.
You can accomplish this behavior in a lot of ways, with a "Database Lookup" step for example, but I usually do that in a quite easy way and here is a example for your tests, I hope it helps.
The idea here is to have two Table input steps, the first one will fetch the IDs we want to look at. For example you may use a SQL query similar to note on the left. The result will be a 1 column stream of rows.
Next we have a Table Input that reads the rows received and executes it's query for each row. I'll add a screenshot with the options that I selected.
What it does is replace a placeholder '?' with the data that is received. If you need two columns use two '?' but remember that it will replace the first one with the first column and the second one with the second column
And you are good to go. Test it a couple of times and good luck.
And the config for the second table input.

Copy SQL column from one to another maintaining ID

I am looking to copy SQL long binary data from the "photo" column to the "id_photo_c" column. Both columns are in separate tables. A got a query to show exactly what I need, but unfortunately you cannot copy & paste outputs from the "Results Pane" of mssql.
I cannot copy the entire table from one to another, the new database has more rows (including some duplicates).
http://i.imgur.com/tmRpMh2.png
Here is the code:
SELECT
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber], [
GroupTables].[dbo].[VisitorsAdvanced].[photo],
[SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c],
[SugarCRM].[dbo].[contacts_cstm].[id_photo_c]
FROM
[GroupTables].[dbo].[VisitorsAdvanced], SugarCRM].[dbo].[contacts_cstm]
WHERE
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c];
It seems like such a simple task (would take two clicks in Excel) - but I can't seem to get it to work.
This isn't a duplicate question. I've seen similar questions on here, none of which describe how to simply copy data from one column to another.
Thank you.
Unless I'm missing something here, it seems like a simple update statement is all you need:
UPDATE [SugarCRM].[dbo].[contacts_cstm]
SET [id_photo_c] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c]
FROM
[GroupTables].[dbo].[VisitorsAdvanced]
INNER JOIN [SugarCRM].[dbo].[contacts_cstm]
ON
[GroupTables].[dbo].[VisitorsAdvanced].[RecordNumber] = [SugarCRM].[dbo].[contacts_cstm].[xxx_id_number_c];
Isn't this as straight-forward as updating the data in the contacts_cstm table?
UPDATE contacts_cstm
SET id_photo_c = photo
FROM VisitorsAdvanced
WHERE RecordNumber = xxx_id_number_c

Query to select items from a list and update table

I've got a query that I ran against a table of stock items to change a field from false to true so that a label would be printed out as per the below.
UPDATE [StockDB].[dbo].[StockItem]
SET [UseDescriptionOnDocs] = 0
Where [UseDescriptionOnDocs] = 1
I have since been told that all of the items do not need this and to only do it for the ones in a list of 1150. From that I need to modify the query, something which I am really struggling with!, so it can only do it per item which are in the format of R368/WASHER/M4/ZINC/PL etc. I thought I may be able to do it using LIKE but it will only do one line? If I could get it to query a list from Excel or CSV that would be great.
Any pointers would be good as I'm an absolute novice with this once you get past the simple stuff like the above!
Thank you.
With MySQL (and others) you can load data from a file to a table. See http://dev.mysql.com/doc/refman/5.1/en/load-data.html to understand and for exemple.
Once you have your items loaded into a SQL table, you just have to do an update + join query. See http://dev.mysql.com/doc/refman/5.1/en/update.html to understand and How can I do an UPDATE statement with JOIN in SQL? for exemples.