Issue in get duplicate value in sum query sqlite - android

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.

Related

Get Wordpress posts with images link

I want to get wordpress posts with specific category and link of images.
As you know images links save to database in guid column, when post_type = attachment.
and ID of post and post_parent are the same.
Now I want to get posts and join guid column to same ID.
When I added Inner join to combine attachment and post, I got error!
Please help me, if you know the way that I can get post with specific category and images link of each post.
Here is my code:
SELECT
*
FROM
wp_posts p,
wp_postmeta m,
wp_terms t,
wp_term_taxonomy tt,
wp_term_relationships tr,
wp_terms t2,
wp_term_taxonomy tt2,
wp_term_relationships tr2
LEFT JOIN wp_posts AS p2
ON
p.ID = p2.post_parent
WHERE
p.post_type = 'post' AND p.post_status = 'publish'
AND p.ID = tr.object_id
AND t.term_id = tt.term_id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'category'
AND tt.term_id = t.term_id
AND t.name = 'Fashion'
GROUP BY
p.ID
ORDER BY
id
DESC
MySQL said:
#1054 - Unknown column 'p.ID' in 'on clause'
I suspect that the problem is due to mixing the old school comma syntax with the newer JOIN keyword.
Relevant excerpt from MySQL Reference Manual:
INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).
However, the precedence of the comma operator is less than that of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.
The easiest way to avoid this problem is to ditch the old school syntax for the join operation, use the JOIN keyword instead.
(It's great that the comma syntax is still valid, to provide backwards compatibility with existing SQL. But there's no good reason new development should use the comma syntax.)
Aside from that, there's a couple of big rock issues that stick out to me.
Seems like there's a lot of join conditions missing
Using * for the SELECT list in development can be useful shortcut, but we usually list the expressions we need to return, especially if we want to return id column from multiple tables, where we like to assign a column alias to avid duplicate columns names.
Relying on the non-standard extension to GROUP BY (when only_full_group_by is omitted from sql_mode to return values from "some" row in the collapsed group
Those all look like serious problems to me.
We can re-write the OP query to use JOIN keyword in place of comma syntax, and relocating conditions to the ON clause, this highlights what looks like missing join conditions:
SELECT *
FROM wp_posts p
JOIN wp_postmeta m
-- ON ???
JOIN wp_terms t
ON t.name = 'Fashion'
JOIN wp_term_taxonomy tt
ON tt.term_id = t.term_id
AND tt.taxonomy = 'category'
JOIN wp_term_relationships tr
ON tr.object_id = p.id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_terms t2
-- ON ???
JOIN wp_term_taxonomy tt2
-- ON ???
JOIN wp_term_relationships tr2
-- ON ??
LEFT
JOIN wp_posts AS p2
ON p2.post_parent = p.id
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
GROUP
BY p.id
ORDER
BY p.id DESC
Where we are going to omit any join condition, and just match all rows to all other rows, then my preference is to include the (optional) CROSS keyword, as an aid the future reader, to signal that the omission of a join condition is by design, and not an oversight.

How to select data from a database, sort it and insert it in another database in SQLite android?

I have a table which contains values from 2 tree map. I couldn't merge them into one and pass as a single map because the input contains duplicates . In short, the data in my table looks like
APPLE 1
BANANA 4
ORANGE 5
GRAPES 1
POTATO 9
I want to sort the integers in ascending order. I tried using order by clause in select statement and used that select statement for creating another table in sorted manner but I couldn't do as there is some missing parenthesis in exec statement.
db.execSQL(INSERT INTO my_ordered_table (name, num) SELECT name,num FROM my_table ORDER BY name ;
I tried writing this but I don't know where to insert parenthesis, " " etc . The column values are dynamic ones and not hard coded values .
I even want INSERT IF EXISTS ELSE UPDATE condition in the insert statement.Any help would be great !! Thanks
db.execSQL(INSERT INTO my_ordered_table (name, num) SELECT name,num FROM my_table ORDER BY num ASC ;
this will work fine.......

Android Expandable Listview have header and child from different table

In one of my app i have database table like
Table 1 :Menu
column
ID,
MenuName,
MeuID
and Table 2 Menuproduct
column
menuId,
menuprice
I need it like
i header of expandble list
menu name
and in child price. like that
how to make it possible? as i have to pass MenuId to get menu price
Please help
Thanks
You need to use join query to get the detail from sqlite database
If you want the details based on MenuID then your query will be like following
SELECT Menu.MenuID, Menu.MenuName, menuprice
FROM Menu
LEFT OUTER JOIN Menuproduct ON Menu.MenuID = Menuproduct.menuId;
http://www.tutorialspoint.com/sqlite/sqlite_using_joins.htm
you need to store the result as custom object in array list.Checkout following
exapandible listview

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! ;-)

Android SQLite getting records from one table based on another

Please excuse me if its a repeated question. I tried searching here and at google but I couldn't exactly find what I wanted.
I have got two tables A & B.
Table A Fields : id, name, description, rating.
Table B Fields : id, aId (linked to table A), customerId, recommended.
Table A contains my data items for which I'm storing average cumulative ratings provided by users.
Table B stores another attribute for data of Table A. It stores the recommended bit (1 for recommended & 0 for non-recommended).
I want to list all the data from Table A but I want to sort them using recommended bit from Table B. So, if there are 10 records in Table A and 2 records in Table B, while listing all those 10 records, the two from Table B should come first and then the others from Table A. It doesn't matter whether the recommended bit value is a 0 or a 1. While listing the other 8 records from Table A, I want to list the records based on their rating in descending order.
Can someone please guide me in writing this sqlite query for Android app? Thanks in advance!
The left join adds the recommended field to the result set (with a value of NULL if there is no matching B record).
The expression recommended IS NULL or EXISTS(...) returns either 0 or 1:
SELECT DISTINCT A.*
FROM A LEFT JOIN B ON A.id = B.aId
ORDER BY B.recommended IS NULL,
A.rating DESC
Alternatively:
SELECT *
FROM A
ORDER BY NOT EXISTS (SELECT 1
FROM B
WHERE B.aId = A.id),
rating DESC

Categories

Resources