B4J Question Query that returns only the column names that have never been evaluated

amorosik

Expert
Licensed User
I need a query that returns, given the name of a table in the db, all the names of the fields that have never been filled in any row of the data present
Basically the fields I could also delete because at the moment they don't contain any information
How to make a unique query that spits out the above?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
This is the english part of the forum. Please write - at least - english here.
Alternatively you can use the italian forum.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You mean you have a table
e.g tbl_population
that you want to query.
You also have another table or list
e.g tbl_selected
Inside tbl_population you have all the data or information while in tbl_selected contains the rows of countries you have added.
You want to query the data in tbl_population where the data contains the countries "not yet" added into tbl_selected.
Am I guess correctly?
 
Upvote 0

amorosik

Expert
Licensed User
Even though the first post has been edited to English, I am still don't understand what you want to do.

I have a db with one table, named CUSTOMERS
This table contain tre field, Code, Name, Note
In the table there are 2 row, like these:
-----------------------------
Code, Name, Note
1, , Note for expert
2, ,Note for novice
-----------------------------
The question is: how to write a query that extract the name of field never used?
(in the example the query must return 'Name')
 
Upvote 0

amorosik

Expert
Licensed User
You mean you have a table
e.g tbl_population
that you want to query.
You also have another table or list
e.g tbl_selected
Inside tbl_population you have all the data or information while in tbl_selected contains the rows of countries you have added.
You want to query the data in tbl_population where the data contains the countries "not yet" added into tbl_selected.
Am I guess correctly?

No, see the example above at #6
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I have a db with one table, named CUSTOMERS
This table contain tre field, Code, Name, Note
In the table there are 2 row, like these:
-----------------------------
Code, Name, Note
1, , Note for expert
2, ,Note for novice
-----------------------------
The question is: how to write a query that extract the name of field never used?
(in the example the query must return 'Name')
You mean the value return from the query is empty string?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The question is: how to write a query that extract the name of field never used?
(in the example the query must return 'Name')
This code iterates over the TEXT type columns and displays in a list the column names where their data is empty string. Assumes if the given column first row is empty, then rest of the rows for that same col have also empty strings, hence LIMIT 1.
B4X:
Dim l As List
    l.Initialize
    Dim rs As ResultSet =SQL.ExecQuery2($"SELECT * FROM CUSTOMERS WHERE
    Name =?
    or Note = ? LIMIT 1"$, Array As String("",""))    'assumes if the column first row is empty, then rest of the rows have also empty strings
    If rs.NextRow Then
        If rs.GetString2(1) =""  Then
'            Log(rs.GetColumnName(1))
            l.Add(rs.GetColumnName(1))
        End If
        If rs.GetString2(2) ="" Then
'            Log(rs.GetColumnName(2))
            l.Add(rs.GetColumnName(2))
        End If
    Else
        Log("All table columns have data")
    End If
    rs.Close
    For Each s As String In l
        Log(s)  'displays all the column names where the data is empty strings
    Next
Perhaps there are more elegant ways.
 
Upvote 0

amorosik

Expert
Licensed User
This code iterates over the TEXT type columns and displays in a list the column names where their data is empty string. Assumes if the given column first row is empty, then rest of the rows for that same col have also empty strings, hence LIMIT 1.
B4X:
Dim l As List
    l.Initialize
    Dim rs As ResultSet =SQL.ExecQuery2($"SELECT * FROM CUSTOMERS WHERE
    Name =?
    or Note = ? LIMIT 1"$, Array As String("",""))    'assumes if the column first row is empty, then rest of the rows have also empty strings
    If rs.NextRow Then
        If rs.GetString2(1) =""  Then
'            Log(rs.GetColumnName(1))
            l.Add(rs.GetColumnName(1))
        End If
        If rs.GetString2(2) ="" Then
'            Log(rs.GetColumnName(2))
            l.Add(rs.GetColumnName(2))
        End If
    Else
        Log("All table columns have data")
    End If
    rs.Close
    For Each s As String In l
        Log(s)  'displays all the column names where the data is empty strings
    Next
Perhaps there are more elegant ways.

Ok, but is not a single query
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Sorry, deleted my response, just re-read the question.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try:
SQL:
SELECT Field FROM(
SELECT Field, CountNullName, COUNT(*) AS CountAll FROM CUSTOMERS, (SELECT 'Name' AS Field, COUNT(*) AS CountNullName FROM CUSTOMERS WHERE "Name" IS NULL) AS B)
WHERE CountNullName = CountAll
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you want to check for both column "Name" and "Note", try:
SQL:
SELECT Field FROM(
SELECT Field, CountNull FROM
(SELECT 'Name' AS Field, COUNT(*) AS CountNull FROM CUSTOMERS WHERE "Name" IS NULL
UNION
SELECT 'Note' AS Field, COUNT(*) AS CountNull FROM CUSTOMERS WHERE "Note" IS NULL))
WHERE CountNull = (SELECT COUNT(*) FROM CUSTOMERS)
 
Upvote 0

DarkoT

Active Member
Licensed User
I have a db with one table, named CUSTOMERS
This table contain tre field, Code, Name, Note
In the table there are 2 row, like these:
-----------------------------
Code, Name, Note
1, , Note for expert
2, ,Note for novice
-----------------------------
The question is: how to write a query that extract the name of field never used?
(in the example the query must return 'Name')
Maybe I don't understand the question but I suggest to use IFNULL function in SqlLite - like this:

SQL:
select *
from Customers
where Ifnull(Name,'') = ''
 
Upvote 0

amorosik

Expert
Licensed User
If you want to check for both column "Name" and "Note", try:
SQL:
SELECT Field FROM(
SELECT Field, CountNull FROM
(SELECT 'Name' AS Field, COUNT(*) AS CountNull FROM CUSTOMERS WHERE "Name" IS NULL
UNION
SELECT 'Note' AS Field, COUNT(*) AS CountNull FROM CUSTOMERS WHERE "Note" IS NULL))
WHERE CountNull = (SELECT COUNT(*) FROM CUSTOMERS)

Without know the field name, i can't test for each one
And i know only table name
What i need is to know if is possibile, with a single query and table name, have in return all the field never used (or like 'null') in every row of table
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Perplexity ?

1683186142380.png


B4X:
PRAGMA_TABLE_INFO('table_name');
SELECT name FROM PRAGMA_TABLE_INFO('table_name')
WHERE name NOT IN (SELECT name FROM table_name WHERE name IS NOT NULL);

[Note that this solution only works if the table has at least one row. If the table is empty, there is no data from which to determine which columns are filled or not]
 
Upvote 0

amorosik

Expert
Licensed User
May be, but actually not function to me

B4X:
SELECT name
FROM pragma_table_info('AAA_TEST_NULL')
WHERE name not IN
  (SELECT DISTINCT name
   FROM AAA_TEST_NULL
   WHERE name is not null)
 
Upvote 0
Top