Access unmatch query - sql

I am trying to do an unmatch query in access: In Table 1 I have three columns
ID q ID Amt
1234411 999 5.00
1234411 996 -10.00
1234411 998 6.00
In the Table 2 I have two columns
ID amt
1234411 1.00
I need to make a query where it would look for an unmatch query by subtotaling the amt in Table 1 for similar ID number and compare it to Table 2. There would be multiple different ID in Table 1 and Table 2 with different amounts.
Optional Information:
What have you tried so far?: I have tried the simple unmatch query in Access but that does not work for this

SELECT T1.ID, Sum(T1.Amt) AS SumAmt
FROM Table1 AS T1
WHERE Sum(T1.Amt) <>
(
SELECT SUM(t2.amt)
FROM Table2 AS T2
WHERE T1.ID = T2.ID
)
GROUP BY T1.ID

Related

wants to pick most closest record in group of records in single table which input criteria

we have a table and there is possibility that one record can have multiple copies means same record can exist in table with multiple entries but their criteria will be different criteria is decided using three main parameters.income,score,no_months.these columns are integer.and we are grouping them by giving unique code to same records profile.
if one input is eligible for multiple profiles then we need to pick which is most matching to criteria.
Sample Data.
id
name
income
score
no_months
group_code
22
abc
1000
500
6
abccode
23
abc
900
600
12
abccode
24
bca
1000
600
12
bcacode
Desired Results
id
name
income
score
no_months
group_code
23
abc
900
600
12
abccode
24
bca
1000
600
12
bcacode
Note: id 23 row has 2 columns which values are greater than id 22 row that is why id 23 was picked although id 23 has less income
Only those records should be display which columns have more count of greater values than other row if group_code is same.
I have tried using multiple order by with cte as more columns needs to display like image city etc. but its not working
Select a single row for the Name or a winner of multiple rows. Winner is one with max score of wins when compared to others in a triangle join. Provided 2 rows has the same criteria, a row with the lesser id wins.
select *
from tbl t
where id in (
-- winners
select winid
from tbl t1
join tbl t2 on t1.name = t2.name and t1.id < t2.id
join lateral (
select case when sign(t1.income - t2.income) + sign(t1.score - t2.score) + sign(t1.no_months - t2.no_months) >= 0
then t1.id else t2.id end winid
) w on 1=1
group by winid
order by count(*) desc
limit 1)
or not exists(select 1 from tbl t3 where t3.name = t.name and t3.id <> t.id)

How do I compare two values in SQLite from different tables?

I'm trying to make a where clause from a different table with 2 values in match for example in SQLite.
Table 1:
Date|CID|text
1/1/90 22:22:22 1 hi
1/1/90 21:22:30 1 How are you
1/1/90 03:22:22 3 hey
Table 2:
ID|date|CID|text
100 1/1/89 11:22:11 1 hello
200 1/1/90 22:22:22 1 hi
300 1/1/90 21:22:30 1 How are you
400 1/1/90 03:22:22 3 hey
500 1/1/85 02:22:22 3 hey
600 1/1/90 03:22:22 80 hey
How to make the query give me the ID from table 2 matching the CID and date from table 1?
Note: If I use where in the result and some date matches in the case of ID 600 and 400, the result will be on the query and I'm not looking that.
Just matching CID and date from table 1 should be listed in the result.
If I understand correctly, you can want a join with two conditions
select t1.*, t2.id
from table1 t1 join
table2 t2
on t1.id = t2.cid and t1.date = t2.date;
If you want to keep all rows in table1, even those with no matches, then use a left join instead.

SQL query Splitting a column into Multiple rows divide by percentage

How to get percentage of a column and then inserting it as rows
Col1 item TotalAmount**
1 ABC 5558767.82
2 ABC 4747605.5
3 ABC 667377.69
4 ABC 3844204
6 CTB 100
7 CTB 500.52
I need to create a new column percentage for each item which is I have done as :-
Select item, (totalAmount/select sum(totalAmount) from table1) as Percentage
From table1
Group by item
Col1 item TotalAmount percentage
1 ABC 5558767.82 38
2 ABC 4747605.5 32
3 ABC 667377.69 5
4 ABC 3844204 26
6 CTB 100 17
7 CTB 500.52 83
Now, the complex part I have to calculate another amount by multiplying this percentage to an amount from another table say table2
ii) update the Total amount column by spilt the total amount column of table 1 into 2 rows – 1st row of the new Calculate PledgeAmount and 2nd row – (totalAmount – PledgeAmount)
*Select t1.percentage * t2.new amount as [PledgeAmount]
From table 1 join table2 where t1.item=t2.item*
. e.g. for col1 Amount of 5558767.82 will split into two rows.
Final Result sample for :-
Col1 item TotalAmount Type
1 ABC 363700.00 Pledge
1 ABC 5195067.82 Unpledge
....
I am using Temporary table to do calculations.
One of the way I think is to calculate the Pledged and Unpledged amount as new column and Pivot it but its huge table with hundreds of columns it will not perform fast.
Any other efficient way?
You can use a windowing function to solve this problem -- first in a sub-query calculate the total and then in the main query the percent:
Select *, (totalAmount/total_for_item)*100 as percent_of_total
from (
SELECT t.*,
SUM(totalAmount) OVER (PARTITION BY item) as total_for_item
FROM table t
) sub
First, let's get the total amount per item:
SELECT item, SUM( totalAmount ) as sumTotal
INTO #totalperitem
FROM table1
GROUP BY item
Now it's easy to get to the percentages:
SELECT t1.Col1,
t1.item,
t1.totalAmount,
t1.totalAmount/tpi.sumTotal*100 AS percentage
FROM table1 t1
INNER JOIN #totalperitem tpi on ...
Tricky part: Separate rows with/without match in table2. Can be done with a WHERE NOT EXISTS, or, my preference, with a single outer join:
SELECT t1.item,
CASE WHEN tpledged.item IS NULL
THEN "Unpledged"
ELSE "Pledged"
END,
SUM( t1.totalAmount ) AS amount
FROM table1 t1
LEFT OUTER JOIN table2 tpledged ON t1. ... = tpledged. ...
GROUP BY t1.item,
CASE WHEN tpledged.item IS NULL
THEN "Unpledged"
ELSE "Pledged"
END
The basic trick is to create an artificial column from the presence/absence of records in table2 and to also group by that artificial column.

How to select the most recent rows from oracle using hibernate?

lets say i have a table (Product) consisting of 4 columns, the last_update is a DATE field.
id prod_id last_update status
1 100 7/8/2014 built
2 100 9/10/2014 in process
3 210 7/8/2014 in process
4 210 9/10/2014 built
is it possible to write a query in hibernate to select the last updated rows for each product? in this case id 2 and 4?
appreciate your answer as always
Try:
select *
from my_table t1
where last_update = (select max(last_update) from my_table t2 where t1.prod_id = t2.prod_id;
Hibernate specific:
From a similar question:
select p1 from my_table t1 where
t1.last_update = max (
select t2.last_update from my_table t2 where
t2.prod_id=t1.prod_id
)

Add Column values in sql server query

I have result of two queries like:
Result of query 1
ID Value
1 4
2 0
3 6
4 9
Result of query 2
ID Value
1 6
2 4
3 0
4 1
I want to add values column "Value" and show final result:
Result of Both queries
ID Value
1 10
2 4
3 6
4 10
plz guide me...
select id, sum(value) as value
from (
select id, value from query1
uninon all
select id, value from query2
) x
group by id
Try using a JOIN:
SELECT
T1.ID,
T1.Value + T2.Value AS Value
FROM (...query1...) AS T1
JOIN (...query2...) AS T2
ON T1.Id = T2.Id
You may also need to consider what should happen if there is an Id present in one result but not in the other. The current query will omit it from the results. You may want to investigate OUTER JOIN as an alternative.
A not particularly nice but fairly easy to comprehend way would be:
SELECT ID,SUM(Value) FROM
(
(SELECT IDColumn AS ID,ValueColumn AS Value FROM TableA) t1
OUTER JOIN
(SELECT IDColumn AS ID,ValueColumn AS Value FROM TableB) t2
) a GROUP BY a.ID
It has the benefits of
a) I don't know your actual table structure so you should be able to work out how to get the two 'SELECT's working from your original queries
b) If ID doesn't appear in either table, that's fine