Impala SQL: Count number of rows fetched - impala

SHOW TABLE LIKE 'my_table';
This will either return a matching table or empty result. (fetched 1 or 0 row)
How do I issue a count query to get "0" or "1" ?

You can't do this with a SQL query today. You'll need support for querying Impala schema metadata as a table, see IMPALA-1761.

Related

In DBeaver how to find Current largest fact table number of rows and size sql query

I'm using Dbeaver software and trying to write an sql query statement to find the Current largest fact table number of rows and size of the table.
How to write an sql query
My Table name is : transactionsale ( It is a fact table)
regards,
Prabhu

SQL Server: set all column rows to 0 or 1 in a temporary table

I have two tables table_1 and table_2.
Both tables contain 250 columns each, with an ID code which allows the two tables to be joined, many to many. Each column row has a value for ranging 0 - 500, with the exception of the ID codes.
I need to do some further analysis, and would like to be able to produce a temporary table or query output where, for each row for each column, the value is set to 0 or 1, where 1 is where the sum of the column by id code is >= 1.
I can do this using case when, but with 500 columns I wonder if there is a more clever way of doing this. If it helps, all the columns in each of the tables end in the word "_bin".
You haven't specified SQL server version, starting from SQL 2012 you can also use IIF or CHOOSE logical functions for this instead of 'case when' but it still require to do it for all columns.
more details here :
https://www.mssqltips.com/sqlservertip/2570/new-logical-functions-in-sql-server-2012-iif-and-choose/

Get latest data for all people in a table and then filter based on some criteria

I am attempting to return the row of the highest value for timestamp (an integer) for each person (that has multiple entries) in a table. Additionally, I am only interested in rows with the field containing ABCD, but this should be done after filtering to return the latest (max timestamp) entry for each person.
SELECT table."person", max(table."timestamp")
FROM table
WHERE table."type" = 1
HAVING table."field" LIKE '%ABCD%'
GROUP BY table."person"
For some reason, I am not receiving the data I expect. The returned table is nearly twice the size of expectation. Is there some step here that I am not getting correct?
You can 1st return a table having max(timestamp) and then use it in sub query of another select statement, following is query
SELECT table."person", timestamp FROM
(SELECT table."person",max(table."timestamp") as timestamp, type, field FROM table GROUP BY table."person")
where type = 1 and field LIKE '%ABCD%'
Direct answer: as I understand your end goal, just move the HAVING clause to the WHERE section:
SELECT
table."person", MAX(table."timestamp")
FROM table
WHERE
table."type" = 1
AND table."field" LIKE '%ABCD%'
GROUP BY table."person";
This should return no more than 1 row per table."person", with their associated maximum timestamp.
As an aside, I surprised your query worked at all. Your HAVING clause referenced a column not in your query. From the documentation (and my experience):
The fundamental difference between WHERE and HAVING is this: WHERE selects input rows before groups and aggregates are computed (thus, it controls which rows go into the aggregate computation), whereas HAVING selects group rows after groups and aggregates are computed.

SQL Developer: Select query results has the expected row but doesn't display it on the grid

I am using SQL developer 3.2.2 to query an Oracle 12 database. I have a select query where I am expecting a certain row to be picked by the query. The results of this query is moved to a global temp table for further processing. But when I query the newly created temp table for the row mentioned above using its key, the query doesn't find the row.
I initially thought that my query had a problem and it wasn't picking up the row at the first place and was debugging the query. But when I ran the query separately on SQL developer and looked for the row by applying a filter on the key column, it shows the row. But when I sort the key column and manually go look for the row in the grid, I don't see the row. I believe it is the same reason why this particular row isn't copied over to the temp table. This is happening to quite a few rows in the database. Has anyone experienced this problem before?
The query is a simple one and has just two columns UserID and LocationID. The query does a union on multiple sub-queries.
select distinct * from (
SELECT distinct UserID, LocationID
FROM TRANSACTION
WHERE "Deleted" = 0 and "TransactionType" in ('E1513','E1514')
AND "Date" <= '31-DEC-2016'
UNION
SELECT distinct UserID, LocationID
FROM FORMHIS
WHERE "FormID" in ('358465','358455')
AND "Date" <= '31-DEC-2016'
)
The output of the above query is missing few rows that I am sure should be in results.

iPhone Sqlite, Getting duplicates while fetching the data from database

I am using the sqlite database for one of my projects. I am using the following query, but when I execute the query I'm getting duplicates.
SELECT * FROM
lcs_mobile_class_folder_content, lcs_mobile_assignment, lcs_mobile_quiz
WHERE
( lcs_mobile_class_folder_content.folder_id = '70c12fcd-d0cb-4243-95ad-1979f90c8ba7'
AND
(lcs_mobile_class_folder_content.class_content_id = ( lcs_mobile_quiz.quiz_id)))
Can anyone tell me why I'm getting the duplicates even though quiz_id is the primary key for the lcs_mobile_quiz table?
When there are multiple lcs_mobile_class_folder_content records that match one lcs_mobile_quiz record, you will get multiple result records.
Furhtermore, there is no filter or join on the lcs_mobile_assignment table, so all the other results will end up duplicated for each lcs_mobile_assignment record.