Select records with with two like statements - sqlite

This query in sqlite:
SELECT * from INVENTORY WHERE product like '250541%'
returns some records. Also the following returns some records:
SELECT * from INVENTORY WHERE product like '250341%'
However if I want to select both of them like:
SELECT * from INVENTORY WHERE product like '250541%' and product like '250341%'
I don't get any result. What am I type wrong?

Your query is perfectly fine,Its up to you which condition you want to use AND or OR,if you want to fetch the records starting with product "250541" and "250341" then you have to use OR condition.Because both condition does not meet with AND condition that's why you are not getting any record.

I don't know your data, but if you want both results you should use OR instead of AND between the LIKE statements

Related

Get top records by latest date in Azure CosmosDB

I have a table like this:
Assume there are lots of Names (i.e., E,F,G,H,I etc.,) and their respective Date and Produced Items in this table. It's a massive table, so I'd want to write an optimised query.
In this, I want to query the latest A,B,C,D records.
I was using the following query:
SELECT * FROM c WHERE c.Name IN ('A','B','C','D') ORDER BY c.Date DESC OFFSET 0 LIMIT 4
But the problem with this query is, since I'm ordering by Date, the latest 4 records I'm getting are:
I want to get this result:
Please help me in modifying the query. Thanks.

PeopleSoft Query Manager - 'count' function

I'm using the current version of PeopleSoft and I'm using their Query manager. I've built a query that looks at the job table and a customized version of the job table (so I can see future hires). In order to do this I've created a union. Everything works fine, except now I want to do a count of the job codes.
When I put in a count, I get an error. I don't know how to get it to work properly. I also don't really know how to using the 'having' tab.
I've attached some screenshots, including the SQL code.
SQL:
Having tab
You have a criteria in your query:
AND COUNT(*) = A.JOBCODE
Your job codes are string values that uniquely identify a job. It will never be equal to a count.
If you remove that criteria, your query will work:
The bigger issue is, what do you want to count? If your query was simply:
SELECT DEPTID, JOBCODE, COUNT(*)
This will give the count of employees in this department and job code. In your description, you said that you wanted the count of job codes. But each row has JOBCODE on it. The count of job codes on the row is one. What do you really want? The count of job codes in the database? The count of job codes in the result set?
If you want to get anything other than the count of rows within the group, you are not able to put that logic in PeopleSoft Query. You will need to create a view in AppDesigner and then you can add that to the query.

Sqlite fetch a particular value not present within the specified limit

I have a table Customer which has the following columns,
user_name,current_id,id,params,display,store.
I am writing a query like this,
SELECT * FROM Customer WHERE user_name='Mike' AND current_id='9845' AND id='Get_Owner' AND params='owner=1' order by(display) limit 6 offset 0
Now there are times when I want to fetch a particular value which is not there in the first six and I want to fetch that particular value and rest 5 values in the same way like above how can I do that?
For example I want something like this
SELECT * FROM Customer WHERE user_name='Mike' AND current_id='9845' and id='Get_Owner' AND params='owner=1' AND stored='Shelly.Am'
I want Shelly.Am and other 5 value like my first query
You can combine two queries by using a compound query.
The ORDER BY/LIMIT clauses would apply to the entire compound query, so the second query must be moved into a subquery:
SELECT *
FROM Customer
WHERE user_name='Mike'
AND current_id='9845'
AND id='Get_Owner'
AND params='owner=1'
AND stored='Shelly.Am'
UNION ALL
SELECT *
FROM (SELECT *
FROM Customer
WHERE user_name='Mike'
AND current_id='9845'
AND id='Get_Owner'
AND params='owner=1'
AND stored!='Shelly.Am'
ORDER BY display
LIMIT 5);

I have to update one table field as another field of different table in sql

I have to update one table field as another field of different table in sql. There are two tables, create_account and Trans_Details. I have to update amount field of create_account table and set it into current_balance of Trans_Details table. How can I join these two tables? please write a query for this.
You can utilize the sub queries to do this...
Example:
I do not know what kind of data base r u using i am just giving a pseudo code..
UPDATE create_account SET (amount) = ( SELECT current_balance FROM Trans_Details
WHERE filter = some_value) WHERE filter = some_value.
I have added the where clause in the query, please ignore if u want to update the entire row in the column amount.

How to get the number of rows of the selected result from sqlite3?

I want to get the number of selected rows as well as the selected data. At the present I have to use two sql statements:
one is
select * from XXX where XXX;
the other is
select count(*) from XXX where XXX;
Can it be realised with a single sql string?
I've checked the source code of sqlite3, and I found the function of sqlite3_changes(). But the function is only useful when the database is changed (after insert, delete or update).
Can anyone help me with this problem? Thank you very much!
SQL can't mix single-row (counting) and multi-row results (selecting data from your tables). This is a common problem with returning huge amounts of data. Here are some tips how to handle this:
Read the first N rows and tell the user "more than N rows available". Not very precise but often good enough. If you keep the cursor open, you can fetch more data when the user hits the bottom of the view (Google Reader does this)
Instead of selecting the data directly, first copy it into a temporary table. The INSERT statement will return the number of rows copied. Later, you can use the data in the temporary table to display the data. You can add a "row number" to this temporary table to make paging more simple.
Fetch the data in a background thread. This allows the user to use your application while the data grid or table fills with more data.
try this way
select (select count() from XXX) as count, *
from XXX;
select (select COUNT(0)
from xxx t1
where t1.b <= t2.b
) as 'Row Number', b from xxx t2 ORDER BY b;
just try this.
You could combine them into a single statement:
select count(*), * from XXX where XXX
or
select count(*) as MYCOUNT, * from XXX where XXX
To get the number of unique titles, you need to pass the DISTINCT clause to the COUNT function as the following statement:
SELECT
COUNT(DISTINCT column_name)
FROM
'table_name';
Source: http://www.sqlitetutorial.net/sqlite-count-function/
For those who are still looking for another method, the more elegant one I found to get the total of row was to use a CTE.
this ensure that the count is only calculated once :
WITH cnt(total) as (SELECT COUNT(*) from xxx) select * from xxx,cnt
the only drawback is if a WHERE clause is needed, it should be applied in both main query and CTE query.
In the first comment, Alttag said that there is no issue to run 2 queries. I don't agree with that unless both are part of a unique transaction. If not, the source table can be altered between the 2 queries by any INSERT or DELETE from another thread/process. In such case, the count value might be wrong.
Once you already have the select * from XXX results, you can just find the array length in your program right?
If you use sqlite3_get_table instead of prepare/step/finalize you will get all the results at once in an array ("result table"), including the numbers and names of columns, and the number of rows. Then you should free the result with sqlite3_free_table
int rows_count = 0;
while (sqlite3_step(stmt) == SQLITE_ROW)
{
rows_count++;
}
// The rows_count is available for use
sqlite3_reset(stmt); // reset the stmt for use it again
while (sqlite3_step(stmt) == SQLITE_ROW)
{
// your code in the query result
}

Resources