using vb 2010 on an access database
INSERT INTO UniqueTable
SELECT DISTINCT
1,2,3,4,5
FROM DataTable
This will get only unique rows in all fields and if I only specify the one field I want to be distinct it only inserts the data in that field
How can I import all data from every field where field 5 is unique?
If I set the database field properties to not allow duplicates all import fails.
Thanks
Don't use distinct in this case, you can't specify which field need to be distinct, it works for entire columns selected. Use group by instead, like: ..GROUP BY 5 .. HAVING COUNT(*) = 1. That will return all rows having field 5 value appear only once in the table, in other word distinct.
Related
I am struggling to understand what the output of SELECT is meant to be in SQL (I am using MS ACCESS), and what sort of criteria this output needs to specify, if any. As a result, I don't understand why some queries work and others don't. So I know it retrieves data from a table, does calculations with it and displays it. But I don't understand the "inner" working of SELECT function. For instance, what is the name of data structure / entity it displays? Is it a "new" table?
And for example, suppose I have a table called "table_name", with 5 columns. One of the columns called "column_3", and there are 20 records.
SELECT column_3, COUNT(*) AS Count
FROM table_name;
Why does this query fail to run? By logic, I would expect it to display two columns: first column will be "column_3", containing 20 rows with relevant data, and second column will be "Count", containing just one non-empty row (displaying 20), and other 19 rows will be empty (or NULL maybe)?
Is it because SELECT is meant to produce equal number of rows for each column?
Your questions involve a basic understanding of SQL. SELECT statements do not create tables, but instead return virtual result sets. Nothing is persisted unless you change it to an INSERT.
In your example question, you will need to "tell" the SQL engine what you want a count "of". Because you added column_3, you need to write:
SELECT column_3, COUNT(*) AS Count
FROM table_name
GROUP BY column_3
If you wanted a count of all the rows, simply:
SELECT COUNT(*) FROM table_name
I have a table with 92k records with only one column, containing notes about leads.
I have another table with 32k lead records with 3 phone columns and one email column among others.
I want to query the 92k records to see if they contain any of the numbers or emails, then concatenate all those records and set that concatenated string as the value of the Notes column of the 32k table.
I have created a spreadsheet that will work but it's been processing for hours and is only half way through.
enter image description here
=IFERROR(ifs(
not(isblank(H30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&H30627&"'",0)),
not(isblank(F30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&F30627&"'",0)),
not(isblank(E30627)),JOIN(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&E30627&"'",0)),
not(isblank(D30627)),Join(char(10),QUERY(Tasks!A:A,"SELECT A Where A
Contains '"&D30627&"'",0))),"")
I felt that Bigquery would save a lot of time but I am a SQL noob and this is returning a left outer join error without the STRING_AGG and 0 modified rows with it.
#standardSQL
UPDATE sfdc.workingFin
SET Notes = (SELECT STRING_AGG(string_field_0) from sfdc.tasks where
string_field_0 LIKE (SELECT Email from sfdc.workingFin))
WHERE TRUE
You can use a correlated subquery:
UPDATE sfdc.workingFin wf
SET Notes = (SELECT STRING_AGG(string_field_0)
FROM sfdc.tasks t
WHERE t.string_field_0 LIKE wf.Email
)
WHERE true;
I have an excel sheet with two fields: list of user id, and corresponding create date. I want to query from an external database having around million records by only returning records where user_id in (?). How can i pass a range of id's like $A1:A17 as the single parameter to the query?
One option to achieve the same result using another method would be to create a temporary table with just one column for user_id.
Then you could run the query and do:
where user_id in (select user_id from my_temp_table)
I'm working on a C# application that imports data from Access into SQL Server. They select an Access file, then a table in the file.
I then perform checks on the data to see if it's valid to import. I want to display a list of columns from the table that have no data in them, so the user has to confirm they want to import regardless of certain empty columns.
Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?
Is there an approach to this in Access besides looping through SELECT ... WHERE (field) IS NULL queries?
I don't know of an alternative to looping, but I will suggest a different strategy for the queries you run in the loop. Seems to me you would want to know whether any rows include non-Null values for the given field.
SELECT Count(*) AS row_count
FROM Table_Name
WHERE field_name Is Not Null;
No looping necessary
Use Count(*) to get the number of total records
Use Count with WHERE (field) IS NULL and compare the counts
If the counts are equal all of the rows are null for that column.
Here's the problem:
I have an Access query that feeds a report, which sometimes doesn't return any records for certain criteria. I would like to display zeros in the report instead of an empty line (an empty recordset is currently being returned).
Is there an SQL solution that (perhaps using some kind of union statement and/or nested SQL) always returns one record (with zeros) if there are not matching records from the initial query?
One possible solution would be to create a second table with the same primary key, and add just one record. In your query, choose as join type all records in the second table, including those with no matching records in the first one. Select as output all fields in the first table.
You can materialize a one-row table with zero for all columns. This is a slight pain to achieve in Access (ACE, Jet, whatever) because it doesn't support row constructors and the FROM must resolve to a base table. In other words, you'll need a table that is guaranteed to always contain at least one row.
This isn't a problem for me because my databases always include auxilliary tables e.g. a calendar table, a sequence table of integers, etc. For exmaple, to materialize a table one-row, all-zeros table using my 3000 row Calendar table:
SELECT DISTINCT 0 AS c
FROM Calendar;
I can then UNION my query with my materialized table but include an antijoin to ensure the all-zeros row only appears in the resultset when my query is the empty set:
SELECT c
FROM T
UNION
SELECT 0
FROM Calendar
WHERE NOT EXISTS (
SELECT c
FROM T
);
Note the use of UNION allows me to remove the DISTINCT keyword and the AS clause ("column alias") from the materialized table.