Create table without omitting empty rows? - sql

I have a short question but no reproducible sample as I simply use the standard function for now.
I want to use SQL's CREATE TABLE function. However, I want the table to display rows even if there are no data points for that specific ovbservation. Currently, whenever there is no data for a certain row, the whole row gets omitted. Ideally I'd like it to still display the row for that specific date, and just have empty cells wher ethe data is missing.
This is important because I need the table to be of specific size, even if data is missing.
One idea I had was to include a WHERE ROWNUM statement, which refers to a materialised view or table of exactly the size I want the table to be in. Is this possible? Any other solution is welcome.

Create table first of all.
use insert statement to insert all the required rows at once (empty rows with only date, or rowindex)
'View' idea is not better, because you have to show those record (empty rows to enter data perhaps) to user, so if user enters data in 5th rows, you will not be able to add 5th row in table prior to first 4 rows.

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

Compare Two Rows and Update Start and End Dates

I need some help and I know I am not the only one to deal with this issue but I am wondering if you might have some ideas on how to handle the situation of comparing two rows of data filling out start and end dates.
To give you some context, we have a huge hierarchy (approx 8,000 rows and about 12 columns wide) that is updated each year. Sometimes the values change and sometimes they don’t. When the values don’t change, then I don’t need to adjust the dates. When the values do change and a new row is added, I need to change the data.
I have attached some fake data to try and illustrate my data. I am building this in MS Access, so I think this is more of a DBA type question that is going to be manipulated via a recordset type method.
In my example I have two tables – Old Table and New Table. In each table there is a routing code field that represents my join field and primary key for this table.
The Old table represents existing data - tblMain. The New Table represents the data to be appended - tblTemp.
To append the data, I have an append query set up in Access. I perform a left join between the Old and New tables, joining on every field and append the rows that are null in the Old table. That’s fine and that is not where my issue is.
What is causing me issue is how to fill out the start and end dates.
So as you can see from my tables, we are running a zoo. Let’s just say for the sake of the argument, our zoo started off pretty simple and has become more sophisticated. We now want our hierarchy to expand out and become a bit more detailed as we are now capturing the type of animal (Level 4) and the native location (Level 5).
As you can see when comparing one table to another the routing codes are the same, so the append query has to have a join on each field. When you do this, you return the Result Table which is essentially the Old and New tables stacked on top of each other. You might think about a Union query but this is going to give me duplicates and I don’t want that.
If you notice in the Result Table there is a Start and End Date. Let’s just say I get the start and end dates via message box that pops up upon the import of the data and is held in a variable. I think there are dates in my real data but still trying to verify this.
So how do I compare (pseudo code for the logic needed)?
• For each routing code:
Compare Levels 1-5
If the routing code is the same but Levels 1 -5 are not the same
fill out the end date of the old record
fill out the start date of the new record
This idea of comparing two records and filling out a data is quite prevalent in my organization but I haven’t found a way of creating the logic that consistently works so any help or suggestions would be appreciated.
Old Table
New Table
Result Table

How to interact between two tables on both directions in Excel?

I have 2 sheets in my Excel project:
In "Sheet1", I have a big data table, with 4 columns:
DataTable[Country], DataTable[Brand], DataTable[Parameter], DataTable[Calculated].
On "Sheet 2" I have a dashboard. I have there a table called FilteredTable that presents the data from DataTable. I have there a drop down command that lets the user select country, and the table is filtered accordingly.
I want that the FilteredTable will not only show data from the original DataTable, but to let the user to change the [Parameter] column. When changing it, I want that the [Calculated] column in both tables will be updated accordingly. If the user will change the country, then the FilteredTable will show the parameter that is stored in DataTable for that combination of [Country] and [Brand]. And if the user will get back to the first country, the displayed parameter will be the last one that the user entered.
I am a bit confused how to do it.
What I have done so far is:
1) to read into FilteredTable rows from DataFilter, using a formula array. I am mirroing this way [country],[brand] and [calculated]
2) in the DataTable[paramter], I read from FilteredTable[parameter] the same way, with a formula array.
It works fine, untill I change a country, and then the parameters in FilteredTable are already do not match the new country, and in DataTable, the parameters for the old country where changed to 0.
I'm in a logical loop. Is there a way out?
Thanks
Have you considered using 3 tables instead of 2? This would be: the original data table, a table which pulls in all values created through manual entry from the display table, and the display table itself.
In VBA, create a function which runs each time a formula is changed in the display table. That function should take the new data from the user, and copy the altered row onto the 'manual entry' table. For the display table itself, you could either recalculate manually through VBA every time a change is made, or have a formula which looks to see whether there is data in the manual table. If there is data in the manual table, that is where the value is pulled from. Otherwise, data is pulled from the original data table.

MS Access Delete query based on combobox

I have a table with a bunch of different fields. One is named period.
The period is not part of the raw data but I run a query when I import new data to the database that gives each record a period.
Now I need a delete query that will delete all the records that have the same period as what is selected in a combobox.
The values in the combobox come from a calendar table that contain all the possible values that could be in that period column at any time.
This is the basic query i thought would solve this issue but it tells me it is going to delete 0 rows every time I run it:
DELETE *
FROM PlanTemp
WHERE PlanTemp.period = Forms![Plan Form]!Combo163;
If you don't need the key field, just remove it.
Look at the "PROPERTIES" section and look at the column names.
Ether remove it there, or from your QUERY source.
You can also look at the Data section of the properties, and change your BOUND column, to Column 2... or whatever holds the data you want to use.

stored procedure sql (Excel data to T-SQL)

I need to set up a new company for automated data import. The utility has provided the data in a spreadsheet. (Image 1)
Based on this data, I need to create a stored procedure that will identify the correct meter, if it exists, and perform either an insert or update to the monthly data table. For automated utility data import, I want to make sure I restrict everything to a particular utility company.
The steps are the following ( I am having a hard time converting this to SQL)
1- I just want a script that identify the correct meter to see if it exists, basically check the Meter# column in the excel with the MeterNumber column in the Meters table.
2- The next step is perform either an insert or update to the MonthlyData table. This is a screen shot of all its columns.
3- Then I just want to make sure that I am restricting everything to the particular company which in this case Site1 since 2 different companies might have the same meter#. The UtilityCompany table contains 3 columns: ID, Name, UtilityType
I honestly do not know from where to get started, would anybody help me with the script? Thank you
You will want to:
perform a Bulk Insert operation to take your data from the excel file into a staging table.
write a query to select ALL rows for the corresponding utility company (notice I didn't see iterate over each row...). This select could be an update where you update an additional column to mark the row as an INSERT, or an UPDATE.
Then the last step (2 parts), retrieve all of the rows that were marked as INSERT, and insert those into your table. Then grab all rows that were marked with an UPDATE, and update their corresponding values based on your matching criteria.