I want to select recipe_id for recipes that have my both ingredient but I cant use AND like this(I mean it returns null for all columns) :
SELECT
vsearch.ingredient_id,
vsearch.ingredtypes_id,
vsearch.name,
vsearch.recipe_id,
vsearch.qingred
FROM
vsearch
WHERE
vsearch.name = 'olive' AND vsearch.name = 'apple'
vsearch is a View which is:
SELECT
ingredient.ingredient_id,
ingredient.ingredtypes_id,
ingredient.name,
quantity.recipe_id,
quantity.ingredient_id AS qingred
FROM
ingredient ,
quantity
WHERE
strong textingredient.ingredient_id = quantity.ingredient_id
And heres my database tables:
To find (for example) find the recipe id's that contain both cucumber and tomato;
SELECT recipe_id
FROM vsearch
WHERE name = 'cucumber' OR name = 'tomato'
GROUP BY recipe_id
HAVING COUNT(*) = 2
...where 2 is the name of ingredients that have to match (in this case both)
An SQLfiddle to test with.
select *
from vsearch
where recipe_id
in
( select recipe_id from vsearch where name = 'apple'
intersect
select recipe_id from vsearch where name = 'olive'
)
Related
I am new to BigQuery using firebase-analytics and trying to fetch data from the created event with custom param.
Getting error at line 13 on
UNNEST(user_dim.user_properties) user_properties
Error : Unrecognised name:user_dim; Did you mean by user_id?
I have tried with mentioned code.
SELECT
user_properties.value.value.string_value AS total_price,
AVG((
SELECT
SUM(value.string_value)
FROM
UNNEST(event_dim),
UNNEST(params)
WHERE
key = "quantity")) AS quantity
FROM
`uniorder-prod.analytics_200255431.events_*` t,
UNNEST(user_dim.user_properties) user_properties
WHERE
event_name = "total_consumption_res"
AND user_properties.key = "total_price"
I expect 2 columns one for total_price and another for the quantity which will some all quantity data.
Any ideas on how I can resolve this?
I was doing mistakes so If you want the result you can refer to this answer.
//Query to run on BigQuery Console, you can change event param as per your
//need
SELECT
param2.value.string_value AS item_name,
SUM(param3.value.double_value) AS quantity,
SUM(param4.value.double_value) AS total_price
FROM
`uniorder-prod.analytics_200255431.events_*`,
UNNEST(event_params) AS param1,
UNNEST(event_params) AS param2,
UNNEST(event_params) AS param3,
UNNEST(event_params) AS param4
WHERE
event_name = "total_consumption_res"
AND param1.key = "user_id"
AND param1.value.int_value = 118
AND param2.key = "item_name"
AND param3.key = "quantity"
AND param4.key = "total_price"
GROUP BY
item_name
ORDER BY
total_price DESC
Your query seems to be created for the old database schema, it has been flattened a bit since then so you no longer need to unnest user_dim (or event_dim) to access user_properties.
Check out the new schema here: https://support.google.com/firebase/answer/7029846?hl=en
I am trying to build sqlite view vwCOMPLETE_INFO from 2 tables (CUSTOMERS and CITIES)
but i have problem in merging the FIRST_NAME and LAST_NAME to build FULL_NAME Column in the new view
CREATE VIEW vwCOMPLETE_INFO AS SELECT (CUSTOMERS.FIRST_NAME || CUSTOMERS.LAST_NAME AS FULL_NAME), CUSTOMERS.AGE, CITIES.NAME FROM CITIES JOIN CUSTOMERS ON CUSTOMER.CITY_ID = CITY.ID;
what could fix that ?
The issue is that you are including AS FULL_NAME as part of the term that builds the column's data, rather than where it should be as after the term so that it names the column accordingly.
In short AS FULL_NAME should be the other-side of/outside/after the parenthesis's. i.e. ....CUSTOMERS.LAST_NAME) AS FULL_NAME..... rather than .....CUSTOMERS.LAST_NAME AS FULL_NAME).....
Try
CREATE VIEW vwCOMPLETE_INFO AS SELECT (CUSTOMERS.FIRST_NAME || CUSTOMERS.LAST_NAME) AS FULL_NAME, CUSTOMERS.AGE, CITIES.NAME FROM CITIES JOIN CUSTOMERS ON CUSTOMER.CITY_ID = CITY.ID;
I'm trying to convert the following SQL statement into DBFlow method calls:
SELECT t.SSID, t.BSSID, t.Latitude, t.Longitude, t.Timestamp
FROM wlan_events t
INNER JOIN (SELECT BSSID, MAX(Timestamp) AS MaxTimestamp FROM wlan_events GROUP BY BSSID) groupedt
ON t.BSSID = groupedt.BSSID AND t.Timestamp = groupedt.MaxTimestamp
What I got so far:
SQLite.select(WifiEvent_Table.SSID, WifiEvent_Table.BSSID, WifiEvent_Table.latitude,
WifiEvent_Table.longitude)
.from(WifiEvent.class)
.as("t")
.innerJoin(WifiEvent.class) // ????
;
How do i create that inner join's select statement using dbflow?
This is what I found:
SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID
in DBFlow:
SQLite.select(Company_Table.EMP_ID, Company_Table.DEPT)
.from(Company.class)
.leftOuterJoin(Department.class)
.on(Company_Table.ID.withTable().eq(Department_Table.EMP_ID.withTable()))
.queryList();
Hope this helps: (Link updated)
https://agrosner.gitbooks.io/dbflow/content/
I have db with scheme
1. _id
2. word
And i have ArrayList with for example 5 words (a1, a2, a3, a4, a5)
What is the best way to construct query to get words from DB which don't contain words from ArrayList?
Sowthing like
Select * from MYTABLE where WORD not in "all words from
ArrayList"
Build a string representing the set of words, and use that as an argument to the query.
StringBuilder wordSet = new StringBuilder();
wordSet.append('(');
for( String word : wordsList )
{
if(wordSet.length() > 1)
wordSet.append(',');
wordSet.append(word);
}
wordSet.append(')');
Pseudo Query
Select * from mytable
Use the except operator against a selection containing words in array list
http://en.wikipedia.org/wiki/Set_operations_(SQL)#EXCEPT_operator
How to update table sqlite_sequence in Ormlite ? I just need update seq. How can I get that table via ORMLite ?
EDIT
I can't find ORLite tool to do this, so instead I use simple sqlite query. In my class extends OrmLiteSqliteOpenHelper I use SQLiteDatabase to make that update.
EDIT2 ;)
In my project I persist class Lesson and class WeekDefinition.
class Lesson{
#DatabaseField(generatedId=true)
private int id;
...
}
class WeekDefinitions{
#DatabaseField(generatedId=true)
private int id;
#DatabaseField(foreign=true, columnName="lesson_id")
private Lesson lesson;
...
}
Now , when I add new lessons, id is increment. For example
id = 1 Math
id = 2 English
id = 3 Medicine
and in weekDefinition :
id = 1 lesson_id = 1 nr = 20
id = 2 lesson_id = 1 nr = 22
id = 3 lesson_id = 2 nr = 32
...
id = 12 lesson_id = 3 nr = 3
SQLite add this row into sqlite_sequence ( when use autoincrement )
rowId = 1 name = lesson seq = 3
rowId = 2 name = weekDefinition seq = 12
Now, I delete all rows from tables Lesson and WeekDefinition. Lesson and WeekDef are empty after that, but sqlite_sequence is still the same. And this is problem because id in table lesson start from value 4 ( seq from sqlite_sequence for lesson and add 1 ) :
id = 4 Math
id = 5 English
id = 6 Medicine
and weekDefinition
id = 13 lesson_id = 1 nr = 20
id = 14 lesson_id = 1 nr = 22
id = 15 lesson_id = 2 nr = 32
and for lesson id = 4 , Math i should get weekDefinitios, but in weekDefinitions lessons_id has value only from 1 to 3
And this is my problem. I need "reset" sqlite_sequence table ( or there is better solution ?)
Building on Marcos Vasconcelos' answer:
UPDATE sqlite_sequence SET seq = (SELECT MAX(col) FROM Tbl) WHERE name="Tbl"
This query will set seq to the largest value in the col identity column in the Tbl table, so there is no risk of violating constraints.
Inside your .db file there's an table called sqlite_sequence
Each row has two columns
name which is the name of the table
seq a integer indicating the current last value at this table
You can update it to 0
But beware if your table use this id as the unique identifier.
UPDATE SQLITE_SEQUENCE SET SEQ= 'value' WHERE NAME='table_name';
If you want to issue general database commands in ORMLite, you can use the updateRaw method. See the javadocs. There is also executeRaw for other commands.
lessonDao.updateRaw("delete from 'lesson';");
lessonDao.updateRaw("delete from sqlite_sequence where name='lesson';");
weekDefinitionDao.updateRaw("delete from 'weekdefinition';");
weekDefinitionDao.updateRaw(
"delete from sqlite_sequence where name='weekdefinition';");
You could also drop and recreate the table as well:
TableUtils.dropTable(WeekDefinition.class);
TableUtils.dropTable(Lesson.class);
TableUtils.createTable(Lesson.class);
TableUtils.createTable(WeekDefinition.class);
I think the real question is why is your application depending on this database internal number? It really shouldn't care.
How about not displaying the number at all so it can be 1 or 1001 and your application won't matter?
You could also never remove the lessons at all but maybe add a hidden boolean field. So if they get re-added, the hidden field could be set to false and Math would still be at id #1.
This worked for me in my database : (I Set the id before one, then after deleting one row, when I add new data row again, the auto increment serial remain ok ) :
public void updateSerialNumber ( long memberId){
String query = "UPDATE SQLITE_SEQUENCE SET SEQ= '"+(memberId-1)+"' WHERE NAME='"+ReportBigHelper.TABLE_MEMBER+"'";
database.execSQL(query);
}