I have a dataset with over 100,000 rows, over 100 columns and where some values are NULL. Now I want to remove all the rows which contain NULL values.
Can anybody suggest the sql command for it?
With the little information you've provided:
DELETE FROM table WHERE colA IS NULL OR colB is NULL
Add further conditions for each column that you want to check.
Change OR to AND if you only want to delete rows where all of the columns are NULL.
It's fairly easy to generate the SQL for this using a query on user_tab_columns if you don't want to type it out by hand.
use a scripting language like PHP to retreive all column names and then construct your SQL query.
Using pure SQL could get tricky.
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 column of type string in my table, where multiple values are separated by pipe operator. For example, like this,
Value1|Value2|Value3
Now, what I want is to have a query, which will show three rows for this row. Basically something similar to the concept of explode in Dataframes.
Note that I am using Spark SQL. And I want to achieve this using SQL, not dataframes.
I got it working by using the following query.
select t.*, explode(split(values, "\\|")) as value
from table t
\\| here can also be replaced by [|]. Just specifying | doesn't work.
I have a SQL table with 150+ columns and I want to apply on them an aggregation function when selecting values but I don't to list all column names by hand. Instead I want to use a for loop through the column names of the table.
I want to do something like this:
SELECT
AGREEMENT_NO,
COUNT(DISTINCT column) FOR column LOOP columns -- Instead of 150+ lines of count
WHERE ...
GROUP BY AGREEMENT_NO
Does anyone know if it's possible to do it in SQL and if yes how?
This can be done by dynamic query.
Take every column of a table by in temporary table by Information_Schema.columns.
Take a while loop for temporary table for all row.
Write dynamic query and concat column name for your requirement.
Execute the query. Eg. Exec("Query" / variable).
This will give you exact result.
I WANT LIKE THIS IN THE ATTACHMENT
thanks
For getting output what you need ,first table record you need to de-concatenate, i mean single rows record make multi rows record and then put inner join. then again you need to apply concatenation on result using stuff function in sql server.
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.