GROUP BY / HAVING CLAUSE Failing in Sqlite - android

Inside a list, I have an item : shop.
Shop have the same id, but different group_id.
The primary key is shop_id, group_id
The first time I display the list, I want to group those 2 ( As it is the same shop)
In the next screen.
So, my query is :
SELECT * FROM TABLE_SHOP GROUP BY ID
But then, When I process it, I need to separate the shops with field "done" = 0/1
if done = 1, I will disable the element.
I tried to add a clause :
SELECT * FROM TABLE_SHOP GROUP BY ID HAVING DONE =0
But then, when one is done, the shop doesn't appear anymore in the list. ( instead of showing one disabled, and one enabled)
Any idea of what is failing in the list?
EDIT :
I changed fields name to match picture.
In this case, I want to display the list.
The first time I display it ( all done fields in 0), I want the shop 25975 ( 2 records ) appears just once ( that's why I used GROUP BY)
Then, when I process a shop, it will update his done field to 1.
So in this moment, I don't wan't to group anymore the 2 rows. It it clearer???

The purpose of HAVING clause is to filter using aggregate functions.
Try this instead:
SELECT * FROM TABLE_TIENDA WHERE TIENDA_DONE = 0 GROUP BY TIENDA_ID

I got it working.
The query was :
SELECT * FROM TABLE_TIENDA WHERE TIENDA_DONE = 0 GROUP BY TIENDA_ID, DONE
Tx for your help

Related

Issue in get duplicate value in sum query sqlite

hello friends i have 2 tables like below
tab_a --> "unit_info_id" ,"unit_id" ,"p_id" ,"p_bathroom" ,"p_bedroom" ,"p_size" ,"p_rent" ,"p_isrent" ,"u_note" ,"r_id" , "isactive"
tab_b--> "rent_id" ,"unit_info_id" ,"t_id" ,"rent_amount" ,"rent_note" ,"rent_date" ,"rent_time" , "r_id"
and my query is as below
SELECT a.* ,
(SELECT SUM(rent_amount)
FROM tab_b
LEFT JOIN tab_a ON tab_b.unit_info_id = tab_a.unit_info_id
WHERE tab_b.rent_date
between '2015-02-03' and '2015-12-03'
GROUP BY tab_b.unit_info_id
) AS "TotalIncome"
FROM tab_a a ,tab_b b
WHERE a.r_id=1 group by a.unit_info_id
when i run above query it gave me duplicate value in totalIncome alias like below image
as per my current data i have rent_amount only for unit_id =1 , in unit_id=2 and unit_id=3 there are no any data in my tab_2 table but when i run above query it gave me output something like above image i need totalIncome as per particular unitwise so any idea how can i solve it?
EDIT
Tab_a content
Tab_b contant
It's because you add SUM as an additional row so the SUM always appears at the end as TotalIncome since you GROUP BY by ID. What behavior do you expect?
OK, I didn't get you at first. You have to rewrite that SELECT. Can you post the content of tab_b as you did with tab_a?
Check if this works
SELECT tab_a.*, SUM(tab_b.rent_amount) AS TotalIncome FROM tab_a INNER JOIN tab_b ON tab_b.unit_info_id=tab_a.unit_info_id WHERE tab_b.rent_date between '2015-02-03' AND '2015-12-03' AND tab_a.r_id='1' GROUP BY tab_a.unit_info_id
Also keep in mind you have typos in what you have originally posted.

Create updatable VIEW from two tables with Triggers

I'm creating an android application that uses from sqlite database and ...
I have problem with logic of my database. I have two tables as below:
TBL_First
id (int), first_name (text), first_qty (int)
TBL_Second
id (int), second_name (text), second_qty (int)
Each table will be used in separate activity.
Qty columns of each table has 0 default value and users can change it at run-time.
Also, I have an final activity that will shows items of TBL_First & TBL_Second where qty columns
of these tables are bigger than 0.
I've use TBL_Final for my new activity as below:
INSERT INTO "TBL Final"(final_name, final_qty)
SELECT first_name,first_qty FROM "TBL_First" WHERE first_qty > 0
UNION ALL
SELECT second_name,second_qty FROM "TBL_Second" WHERE second_qty > 0
I can fetch data of TBL_final and show its data to users, but if users want to update final_qty, first_qty or
second_qty doesn't update!
I think that I can use VIEW instead of TBL_Final, but I know that it would not be updated!
I searched around this issue and found out that Triggers can be helpful.
Unfortunately, I'm not familiar with VIEW and Trigger at all !
Just I know I can use VIEW as below but I don't know how can I use Triggers to update view_qty
and first_qty and second_qty !!!
create or replace view final_view (view_name,view_qty) as
select first_name,first_qty where first_qty > 0
union all
select second_name,second_qty where second_qty > 0
Any suggestions would be appreciated ...
The trigger has access to the old and new values of the view's row with the OLD and NEW table aliases; it must use them to look up the row(s) of the base tables:
CREATE TRIGGER final_view_update
INSTEAD OF UPDATE OF view_qty ON final_view
FOR EACH ROW
BEGIN
UPDATE first_qty
SET first_qty = NEW.view_qty
WHERE first_name = NEW.view_name;
UPDATE second_qty
SET second_qty = NEW.view_qty
WHERE second_name = NEW.view_name;
END;
The trigger always executes two UPDATEs, but only one WHERE clause will match.

MySQL relation and entities

I have two tables: Place and Discount with an N<->N relation. I also have another table called place_discount which has two foreign keys (idPlace and idDiscount).
When I add a discount to a place, e.g. discount for a bar, I add it in place_discount table.
So my issue is: I have an android app which queries on that database. when I want to list all the places that I have loaded, I want to show a star on those who have a discount. To list that I do a select * from place. Is there a way to do that query?
Tip: I can not use triggers because I do not have super privileges.
Why not use a join?
SELECT
Place.*,
IF(IFNULL(MIN(place_discount.idDiscount),0)>0,1,0) AS hasDiscount
FROM place
LEFT JOIN place_discount ON Place.id=place_discount.idPlace
GROUP BY Place.id
will give you a 1 in hasDiscount if a discount is available, a 0 if not.
On a side note: You do not need super privilege for a trigger on a table you own.

Database table design for Restaurant Ordering System

I need help to design DB/table for Restaurant Ordering System.
1) Captain will take order on Android device and after Placing an order from Customer, he (captain) will Print Order.
2) Captain can alter/add order based on Customer's demand
3) Finally based on customer's demand captain will finalised bill and Print the bill
Some Cases for Placing Order
1) Place Order and not cancelled and finalised bill
2) Place Order - say 4 Burgers , after finish, again customer Place order - say 3 Burgers, not cancelled, and finalised bill
3) Place Order - say 4 Burgers , after finish , again Place order - say 3 Burgers, and Customer asks to Cancel 1 Burger, then finalised bill
4) Place Order - say 4 Burgers, then, Cancel 2 Burger, then after sometime, Place Order 4 Burgers then finalised bill
5) Place Order - 4 Burgers, after sometime, Cancel 2 Burgers, after sometime, Place Order 4 Burgers, after sometime Cancel 2 and then finalised bill
i have already created , menu_card (i.e item), table_info
Billing Table :
id
table_no
bill_no
item_name
quantity
repeat_quantity
print_status (Printed/Not Printed)
ongoing (Yes/No(if no -then finished) )
date_time
Which field should i add in case 3,4,5 for Placing an order
I am confused what if Customer ask for "Cancel Order" , after finish current Order, he asks for same Item.
New EDIT :
I am showing the List of Ordered Items, where captain can touch to update the order,
I m thinking to add cancelled_items_quantity in table and for every new order i will make new entry to database to add those order,
E.g. Customer has ordered 4 Burgers, then it will add to the DB, now after finishing it off, if he asks for 3 burgers, again new entry will be made rather to modify existing 4 to 7, and even if he cancels the order, suppose 2 from 3 Burgers, then update will be made on same entry i.e. it will update cancelled_items_quantity to 2 from default 0
Pls Help
I'd start with making a separate table for items, orders and bills. Since one bill can contain multiple orders and an order multiple items.
As for your example cases, only write finalised data to the database. Keep non-finalised orders in memory on the app. Once the user clicks 'Ok' or 'Finish' the order is finalised and you can write it to the DB.
If you would change the order every time in the database, you'd do way too much transactions which isn't good.
You probably will want separate tables.
1) billing_info
2) table_info
3) item_info
Then correlate to each other as your requirements. Add a 'status' field or similar in item_info so you can easily update its value whenever user request changes.
EDIT
How about for item_info table, for each individual item that customer has ordered, create an entry. Then any changes, update the status.
I visualize on the android tablet, i can see a whole list of every items that customer has ordered.
Eg.
1 steak
1 ice tea
1 steak
1 coffee
1 coffee
So for each item there can be 2 options.
1) to remove the entry before confirmation
2) to change the status after confirmation
Before confirmation, changes are just on the UI only. So we can use option1 to update the UI. I supposed the complications come after confirmation and customer wants to change the order. So we use option2.
2 steak ordered. Now customer wants to cancel 1. So waiter just change the status of 1 steak to 'cancel'.

SQLite count members in a group

Hey im having problems counting members of a group. The query i have right now returns one row for each group a given user is part of. But i want the amount of members to show up aswell. The three tables im using looks like this:
Here is the query im working with:
Look in edit!
If i leave out the COUNT part of the code, i receive the 2 groups im expecting. But when i add in the COUNT it only returns one group with members = 2. So it seems like it counts, but im not sure it's the correct answer.
Thanks in advance!
Assuming you're after the total number of participants within each group with which user 1 is affiliated...
SELECT gu1.group_id_foreign
, COUNT(*) ttl
FROM group_participants gu1
JOIN group_participants gu2
ON gu2.group_id_foreign = gu1.group_id_foreign
WHERE gu1.user_id_foreign = 1
GROUP
BY gu1.group_id_foreign;
http://sqlfiddle.com/#!2/821ad/1
Incidentally, the _id column in the group_participants table appears to serve no purpose, and, to my way of thinking, you're naming convention is demented! ;-)

Categories

Resources