SQL with two subqueries - MS Access - sql

I have two tables (tblAAA and tblBBB). I am trying to get a query that will sum the total from one column based on criteria from another column (1) and the sum of that same column based on different criteria from the same column I got the first criteria from (2). Then I need to do the same one more time from another table (3). The last thing I need to do is perform a calculation that will yield one result (3 + 1 - 2). Here is what I have so far that I got somewhere else tailored to my situation. It works but only on two of the above. Any help is appreciated.
SELECT tblAAA.ID,
tblAAA.Type, SUM(tblAAA.Amount),
SUM(tblBBB.CurTotal),
(SUM(tblBBB.CurTotal)) - SUM(tblAAA.Amount)
FROM tblAAA INNER JOIN tblBBB ON tblAAA.ID = tblBBB.ID
GROUP BY tblAAA.ID, tblAAA.Type
HAVING tblAAA.ID=15 AND tblAAA.Type="Credit";
Table1
Type Amount
Debit 15.00
Debit 15.00
Debit 10.00
Credit 7.00
Credit 13.00
Table2
CurTotal
5.00
10.00
15.00
Expected Output (30.00 + 20.00 - 40.00)
10.00

Related

SQL Query to return all of the same numbers ignoring if the number is positive or not

I am looking for an SQL query to return all records based on a number. The trick is that the number can be positive or negative, but as long as the number is the same it should return.
So I have a table like this:
1 amt1 100.00
2 amt2 100.00
3 amt3 -100.00
4 amt4 120.00
A query such as this:
Select * from table where amount = '100.00'
Only returns the first 2 rows.
I want to return the first 3 rows, thus ignoring the minus sign but still matching the amount.
Any ideas?
Use in:
where amount in (100.00, -100.00)
Note the single quotes are not necessary.
You can also use abs():
where abs(amount) = 100.00
if you don't care much about indexes or query optimization.
If amount is a number:
select * from table where abs(amount) = 100.00;
If amount is text:
select * from table where amount in ('100.00', '-100.00');

How can I group by and sum without using all selected columns using SQL Server/MS Access?

SQL newbie here...
Here's a sample of my table named TEST in MS Access:
EFF_DATE BATCH ENTITY ACCOUNT TRANS_AMT
12/1/2017 TEST A 110 165105 (10.00)
12/1/2017 TEST A 120 278910 10.00
12/1/2017 TEST A 110 165105 (10.00)
12/1/2017 TEST A 120 165135 10.00
12/2/2017 TEST B 120 165135 15.00
12/2/2017 TEST B 110 278910 (10.00)
In my query, I'm trying to sum by EFF_DATE and BATCH only and exclude TRANS_AMT sums equaling 0.
So my query results should be the following:
EFF_DATE BATCH ENTITY ACCOUNT TRANS_AMT
12/2/2017 TEST B 120 165135 15.00
12/2/2017 TEST B 110 278910 (10.00)
I've tried the following query:
SELECT
EFF_DATE,
BATCH,
ENTITY,
ACCOUNT,
Sum(TRANS_AMT)
FROM TEST
WHERE ACCOUNT in ("165105","278910","165135")
AND ENTITY in ("120","110")
GROUP BY TEST.EFF_DATE, TEST.BATCH
HAVING (((Sum(TRANS_AMT))<>0));
I get the following error:
"You tried to execute a query that does not include the specified expression 'ENTITY' as part of an aggregate function."
When I googled the error I couldn't find any information other than stating that the other columns should be added to the GROUP BY clause but if I add them, then the transactions that should sum to zero by date and batch continue to show up. Any ideas on what I can do to get the desired result?
Thanks,
Dan
Amend you GROUP BY and include ENTITY, and ACCOUNT
GROUP BY TEST.EFF_DATE, TEST.BATCH, ENTITY, ACCOUNT
When you use something in a grouped select statement, it needs to be in the group by.
However, you can do this nesting.
You can all the effective date/batch combinations and are non-zero.
Then only select outer records pertaining to them.
SELECT * FROM TEST A
WHERE (A.EFF_DATE, A.BATCH) IN
(
SELECT B.EFF_DATE, B.BATCH
FROM TEST B
GROUP BY B.EFF_DATE, B.BATCH
HAVING Sum(B.TRANS_AMT)<>0
)

UPDATE with HAVING in duplicate values in Excel

Need help with this issue. I have a Develop, i need find the duplicate values in SQL, after need Sum the INVOICE_AMOUNT and Divide for individualy amount Example.
FA-0001 $25.00 BILL-0001
FA-0001 $75.00 BILL-0002.
I need SUM TOTAL of this invoice. SUM(AMOUNT_INVOICE)= $100.00, after divide this result with the individual amount. Example 100.00/25=0.25 , etc etc. and this percentage multiply for DET_SOL_AMOUNT.
I need apply this query in duplicate values.
I try with this query.
UPDATE [T4DET] SET [DET_SOL]=(([LOC_AMOUNT]/SUM([LOC_AMOUNT]))*[DET_SOL_CALC]) FROM [1WEB] WHERE [1WEB].[INVOICE] IN (SELECT [T4DET].[ASSIGNMENT] FROM [T4DET] GROUP BY [T4DET].[ASSIGNMENT] HAVING COUNT(*) > 1)
Thanks for your Help.
If I understood what you want to do correctly, it is easy with Excel. You need to write formulas in 2 columns only, for example:
Group Amount Bill No DET_SOL_CALC Sum of Group Result
FA-0001 $25.00 BILL-0001 2 100 0.5
FA-0001 $75.00 BILL-0002 2 100 1.5
FA-0002 $200.00 BILL-0001 5 600 1.666666667
FA-0002 $100.00 BILL-0002 5 600 0.833333333
FA-0002 $300.00 BILL-0003 5 600 2.5
Put your data in columns A, B and C
ColumnD: DET_SOL_CALC
Column E formula should be: =SUMIF($A$2:$C$6,A2,$B$2:$B$6)
Column F formula should be: =B2/E2*D2
Row 1 is headers of your data
put these formulas in row to and drag them down to the last row of your data, your numbers should be calculated correctly.
Please hit the check mark if this is your answer!
The alter Solution is, Create a Temporal Table with SUM and GROUP BY and agregate three columns for calculations
Example
DET4TEMP
ASSINGMENT NVARCHAR
DOC_AMOUNT MONEY
INSERT INTO 4DETTEMP (ASSINGNMENT,[TOTAL]) ASSIGNMENT, SUM(DOC_AMOUNT) FROM FBL5N GROUP BY ASSIGNMENT
and after query is+
Obtain DET SOL Amount in the other table.
UPDATE 4BET SET DET_SOL_CAL=T2.INCOMING_AMOUNT FROM FBL5N T2 WHERE ASSIGNMENT=T2.INV_CON
Obtain DOC AMOUNT TOTAL of the temporal table.
UPDATE 4BET SET DOC_AMNT_TOTAL=T2.[TOTAL] FROM 4DETTEMP T2 WHERE ASSIGNMENT=T2.ASSIGNMENT
Obtain the Calculation Percentage.
UPDATE 4BET PERC_CAL_AMNT=(DOC_AMNT_TOTAL/DOC_AMNT), DET_SOL=(PERC_CAL_AMNT*DET_SOL_CALC)
after delete temp tables and finish.
This is my solution. The question is Viable?

Hive: Create rows with summed data, by date (unknown number of dates)

I am currently working with a Hive Table which contains transactions data and I need to do some basic statistics on these data, and put the results in a new table.
EDIT: I'm using Hive 0.13 on Hadoop 2.4.1.
CONTEXT
First, let me try to present the input table: here's a table with 3 columns, an ID, a date (month/year), and an amount:
<ID> <Date> <Amount>
1 11.2014 5.00
2 11.2014 10.00
3 12.2014 15.00
1 12.2014 7.00
1 12.2014 15.00
2 01.2015 20.00
3 01.2015 30.00
3 01.2015 45.00
... ... ...
And the desired output consist of a table grouped by IDs, where in each line I sum the the amounts, for each corresponding months:
<ID> <11.2014> <12.2014> <01.2015> <...>
1 5.00 22.00 0.00 ...
2 10.00 0.00 20.00 ...
3 15.00 0.00 75.00 ...
... ... ... ... ...
Considering that the original table has >4 million IDs and > 500 million lines, on more then 2 years. It seems pretty hard to hardcode the table by hand since I don't know how many columns I should create.
(I know how many different dates I have, but if the original table grows over 5, 10, 15 years, there is going to be a lot to do by hand and that's risky.)
THE CHALLENGE
I know how to do some basic manipulations and GROUP BYs, I can even do some CASE WHEN, but the tricky part in my problem is that I can not create columns like this (as mentionned above)...
SUM (CASE WHEN Date = 11.2014 THEN Amount ELSE 0 END) AS 11.2014
SUM (CASE WHEN Date = 12.2014 THEN Amount ELSE 0 END) AS 12.2014
SUM (CASE WHEN Date = 01.2015 THEN Amount ELSE 0 END) AS 01.2015
SUM (CASE WHEN Date = ??? THEN Amount ELSE 0 END) AS ???
... because I don't know how many different dates I'll eventually have, so I would need something like this:
SUM (CASE WHEN Date = [loop over each dates] THEN Amount ELSE 0 END)
AS [the date selected in the loop]
THE QUESTION
Do you have something to propose in order to :
How can I loop over all the dates ?
And be able to create a colum for every dates I have without specifying myself the name of the soon to be created column ?
Is it doable in a single HiveQL script ? (not obligated but could be really nice)
I would like to avoid UDF but at this point I'm not sure it's preventable since I haven't find any case that ressemble mine.
Thanks in advance and don't hesitate to ask for more info.
This is too long for a comment.
You cannot do exactly what you want in Hive, because a SQL query has to have a fixed number of columns when it is defined.
What can you do?
The easiest thing is simply to change what you want. Product multiple rows instead of multiple columns:
select id, date, sum(amount)
from table t
group by id, date;
You can then load the data into your favorite spreadsheet and pivot it there.
Other alternatives. You can write a query that will write the appropriate query. This would go through the table, identify the possible dates, and construct a SQL statement. You can then run the SQL statement.
Or, you could use some other data types, such as a list or JSON to store the aggregated values in one row.

SQL/sqlite query to union tables with matching columns and merged columns

I'm working with a database that has 2 tables for each company with 4 companies (8 total DBs for thisquery) and for reasons outside of my control that can't be changed. This is also an sqlite DB.
My app currently has to do 8 round trips to get all the data. I want to consolidate that down to one table view query but I can't figure out how to combined the data in a way that would make it work. Here is an example of the tables.
Table 1 (Type A)
name zone
ABCD ABC1
DBAA CBA1
Table 2 (Type A)
name zone
ABCD 1234
DBAA 4321
Table 1 (Type B)
zone weight rate
ABC1 1 0.50
CBA1 2 0.88
Table 2 (Type B)
zone weight rate
1234 1 0.52
4321 2 0.80
Finally I want the view to look like this:
name weight Table 1 rate Table 2 rate
CABA 1 0.52 0.50
AEAS 2 0.80 0.88
I tried this for my SQL statement:
SELECT 1A.name, 1B.weight, 1B.rate as A from 1A, 1B WHERE 1A.zone = 1B.zone
UNION ALL
SELECT 2A.name, 2B.weight, 2B.rate as B from 2A, 2B WHERE 2A.zone = 2B.zone
I have also tried a couple joins statements after reading unions must have matching column counts but I can't seem to hit the right query. Any ideas what I'm doing wrong or how I can achieve this with a query?
Any help is greatly appreciated!
Updated with Fiddle example here: http://sqlfiddle.com/#!5/37c19/3/0
Here is a query that will produce something similar to your example:
SELECT
ZonesOne.name
, RatesOne.weight
, RatesOne.rate as Table1Rate
, RatesTwo.Rate AS Table2Rate
FROM ZonesOne, RatesOne, RatesTwo
WHERE
RatesOne.zone = ZonesOne.zone
AND RatesOne.Weight = RatesTwo.weight
UNION ALL
SELECT
ZonesTwo.name
, RatesOne.weight
, RatesOne.rate as Table1Rate
, RatesTwo.Rate AS Table2Rate
FROM ZonesTwo, RatesOne, RatesTwo
WHERE
RatesOne.zone = ZonesTwo.zone
AND RatesOne.Weight = RatesTwo.weight
However, your Table 1 Rate and Table 2 Rate seem to be switched around. Also, your data from ZonesTwo has two entries for "DBAA".