Delete row operation in Microsoft Access - database

The picture is a snap shop of the query that i want to work on.
1. I want to delete any row that has blank W field together with all other row that share the same product_code field. for example, from the picture, 103346 has a blank W field, therefore any row with 103346 should be deleted (even if its field W is not blank).
I want to be able to export the new query to excel (If Possible).
I dont mind if this is through SQL or VBA code.
I am new to MS Access

One way of doing this using SQL is:
Create the query, and add the table (I've called mine ProductData);
Select the fields you want in the output;
In the criteria field for PRODUCT_CODE, type:
Not In (SELECT product_code FROM ProductData WHERE W IS NULL)
The SQL should like this:
SELECT ProductData.PRODUCT_CODE, ProductData.PURE_QP1, ProductData.W
FROM ProductData
WHERE ProductData.PRODUCT_CODE NOT IN (SELECT product_code FROM ProductData WHERE W IS NULL);
And to export this to Excel, the VBA code would be:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Query3", "J:\downloads\test.xlsx", True
Where Query3 is the name of the query that you have created.
Regards,

You can do it with EXISTS:
DELETE FROM tablename AS t
WHERE EXISTS (
SELECT 1 FROM tablename
WHERE PRODUCT_CODE = t.PRODUCT_CODE AND W IS NULL
)

To delete all empty records in a Microsoft Access table. You can use:
DoCmd.RunSQL "DELETE * FROM TableName WHERE FieldName='';"
There's more delete commands at the link here

Related

SQL Server : copy from one table to another where accountID matches

I have 2 tables: tblAccounts and tblOrders. How do I copy the data in column VENUE in tblOrders to the new column I've just set up in tblAccounts also called VENUE but where the account ID matches.
I was going to try something like this:
INSERT INTO tblaccounts (venue)
SELECT venue
FROM tblorders
WHERE tblorders.accountID = tblaccounts.accountID
If that makes sense?
The tables are different sizes so I can't just copy it all across.
Since you want to copy the value of column: venue from tblOrders to tblAccounts, you should update tblAccounts instead of inserting as following.
UPDATE tblAccounts
SET
[venue] =y.[venue]
FROM tblAccounts INNER JOIN tblOrders y
ON
tblAccounts.[accountID] = y.[accountID]

SQL Server - Replacing NULL values with mode of column grouped by another column

I have a table (call it oldtable) and the relevant columns are name, group, zip code. I have selected those into a new table (call that newtable). My issue is that some of the zip codes in the first table are NULL. I want to replace the NULL zip codes with the mode (most common value) of their group.
For example, say a row in newtable looks like this:
Name Group ZipCode
Blah G1 NULL
I want to replace that NULL with the most common zip code over all the people in G1 in oldtable. I am having trouble even getting started on pulling the mode of one column when grouped by another column.
I am using Microsoft SQL Server 2016.
This can be done using CROSS APPLY on an UPDATE.
UPDATE n SET
zipcode = x.zipcode
FROM newtable n
CROSS APPLY( SELECT TOP 1 zipcode, COUNT(*) cnt
FROM newtable o
WHERE n.[group] = o.[group]
GROUP BY zipcode
ORDER BY cnt DESC) x
WHERE n.zipcode IS NULL;

Updating Rows In a Table That Was Used to Import From SSIS

I am currenly creating a SSIS package that selects the top 2000 entries from one table and insert those entries into another table. How can I update those entries in the original table (my select contains a condition whereby I check where the status is still empty) so in my next run of the SSIS package they won't be imported?
**EDIT
As I can't give the DLL due to security reasons, to sketch my scenario, I have TableA and TableB who share the same structure:
Index Number (Primary Key, Indexed, Auto Increment)
Name (varchar)
Surname (varchar)
Status (varchar)
Type (varchar)
Import Date (date)
Using an OLE DB Source within my Data Flow Task, the following query is used to determine which cases I should import from TableA to TableB (an OLE DB Destination):
SELECT TOP(2000) *
FROM TableA
WHERE Status = ''
AND [Type] = 'New Case'
ORDER BY IndexNumber ASC
Now those cases imported from TableA to TableB, I want to update the cases in Table A to not import them again. The data in TableB is often moved to another database, so I can't use that for comparison.
I suggest you use this pattern:
FIRST update the source table and mark 1000 records as 'ready to extract', i.e. set them to 1
Next, select all records from your source table that are ready to extract
Lastly, if the extract was succesful, update the 1's (ready to extract) to 2's (extracted OK)
Here is how I would do it:
I would add a Execute SQL Task at the end of the control flow to update these first 2000 records in your original table to some other status (status = 3 or something). That way, since you already have a null check in your query, it would not select those initial 2000 records in your next run.

SQL server : Inserting/updating missing data from one table to another

I have two table "Container" and "Control". These are existing tables and there is no foreign key relationship between the two. These are also very old tables so are not normalized. And I cannot change the structure now.
Below is the structure of the two tables.
Container table :
Control Table :
The Name field in Control table contains CTableName+CPName from Container table.
I want to update the columnName field of Control table with the value of CID column of Container table. and also want to insert one more record (for ctable2 i.e the fourth row in final Control table below) in Control table.
The tablename and columnname columns have will always be have default values.
The final Control table should look like this:
How do I do this?
I hope you want to apply this fix because you want normalize your table structure.
Try this:
First step:
In this way you'll UPDATE all Control rows with the value of Container table where the couple fields CTableName and CPName are the same of Name (excluding the rows of Container with the same couple fields)
UPDATE Control
SET ColumnValue = (
SELECT c.CID
FROM Container c
WHERE c.CTableName + '+' + c.CPName = Control.Name
AND NOT EXISTS(
SELECT 'PREVIOUS'
FROM Container c2
WHERE c.CTableName = c2.CTableName
AND c.CPName = c2.CPName
AND c.CID < c2.CID
)
),
TableName = 'default', ColumnName = 'default'
WHERE ColumnValue IS NULL
Second step:
Adding elements don't present in Control table
INSERT INTO Control (field list)
SELECT field list
FROM Container co
WHERE NOT EXISTS(
SELECT 'in_control'
FROM Control ct
WHERE co.CID = ct.ColumnValue
)
After these two steps you can drop column Name in Control table
I am an Oracle plsql programmer and worked with Sql-server as well.
First you should describe the relationship between the 2 tables, in the end i could figger it out but it's better you explain it yourself.
To update a table with information from another table you should ask yourself:
- when should the update take place?
- what are the conditions to start the update?
- how should the update be done?
In Oracle there is a database object called a trigger. It's quite a handy object and probably just what you need. I believe that sql-server has it too.
Pls fee free to ask any questions but do read the sql-server appropriate manual as well.
Good luck, Edward.

SQLITE Replace Column IDs with NAMES from SEPERATE TABLE in same DB

I have a sqlite database avalible here => https://www.dropbox.com/s/9cn093ns3ot2cik/drinks.db?dl=0
I'm trying to show ( or replace?) the drinks.glass_id with its proper name from glasses.name
I have tried
select drinks._id DID,
drinks.glass_id GID,
glasses.name GNAME,
glasses._id GIDD
from drinks DRNKS
join glasses GLS
on drinks.glass_id = glasses._id
What is wrong??
I have spent several hours looking this up.
I have read these links and still cant figure it out.
http://community.spiceworks.com/topic/242166-sql-query-help-convert-category-id-to-the-text-name
Replacing a pulled SQL ID value with its name from another table
How to replace fetched values with another column while querying with SQL Server 2008 R2
how to get name from another table with matching id in another table?
If you specify an alias for your tables then you have to use it everywhere
select d._id DID,
d.glass_id GID,
g.name GNAME,
g._id GIDD
from drinks d
join glasses g on d.glass_id = g._id

Resources