Help creating crosstab query with Access - ms-access

I'm trying to make a crosstab query (with access tables), But I got lost writing the inner joins statements.
My end result suppose to be the "QueryResult".
Table1 holds the fund information,
Table2 are the type of data the funds have
Table3 is a conversion from the codes of the data to the type data in table2, and Table4 holds the data.
Table1
FundID FundName
1 Fund1
2 Fund2
3 Fund3
4 Fund4
5 Fund5
6 Fund6
7 Fund7
Table2
TypeID TypeName
1 Balance
2 Yield
3 Fees
4 Deposits
5 Withdraws
Table3
CodeID TypeID
KT111 1
KT112 2
KT113 3
KT115 3
KT116 4
KT117 4
KT118 5
KT119 5
Table 4
CodeID FundID DataVal
KT111 1 1000
KT116 2 40
KT118 3 30
KT119 3 30
KT118 2 10
KT119 2 50
KT111 2 3000
KT111 3 2000
KT112 1 1.5
KT112 2 1.0
KT112 3 0.5
P.S: Table4 holds much rows then shown here with codes which I do not need.
QueryResult
FundID Balance Yield Fees Deposits Withdraws
1 1,000 1.5 555 40 60
2 3,000 1.0 155 20 60
3 2,000 0.5 255 70 60
What is the right statement to get the query result? (I got lost on the inner joins...)
Is there also a way to sum some of the data, and show the value (without summing) of other data from table4?
Thanks!

while I am not exactly sure of all of the requirements you have, this might get you started:
TRANSFORM Sum(d.dataval) as DataValue
SELECT d.fundid
FROM
((Data d
INNER JOIN fund f ON d.fundid = f.fundid)
INNER JOIN code c ON d.codeid = c.codeid)
INNER JOIN type t on c.typeid = t.typeid
GROUP BY d.fundid
PIVOT T.Typename
Results:
fundid Balance Deposits Withdraws Yield
1 1000 1.5
2 3000 40 60 1
3 2000 60 0.5

Related

Mysql query to sum rows and columns based on column criteria

Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type

sql script left outer join issue

**Table 1**
Tbcode description Amount
2 debtors 40000
3 creditors 50000
4 share_capital 10000
5 reserve 20000
**Table 2**
Tbcode description Amount
1 debtors 3000
2 creditors 7000
3 share_capital 0
4 reserve 3000
4 reserve 2000
2 creditors 300
3 share_capital 3000
4 reserve 3000
1 debtors 2000
I have to put a left outer join on table 1
In my output table I need all the columns of table 1 and the sum of amount column group by TB code.
I have written the following script:
select openingtb.TBCODE,openingtb.DESCRIPTION,openingtb.AMOUNT,count(journals.AMOUNT)
from openingtb
left outer join journals
on openingtb.tbcode = journals.TBCODE
group by journals.TBCODE.TBCODE
;
Can someone guide me as to whats wrong with the code and what could be the correct code to get the required output
it you use aggregate function, then you should group all non-aggregate fields used in select clause...
SELECT openingtb.TBCODE, openingtb.DESCRIPTION, openingtb.AMOUNT
, Count(journals.AMOUNT)
FROM openingtb
LEFT JOIN journals
ON openingtb.tbcode = journals.TBCODE
GROUP BY openingtb.TBCODE, openingtb.DESCRIPTION, openingtb.AMOUNT
;

Mysql select result in one currency

I have to create a reports in one currency. I need to do query in MySQL without using PHP process. but unable to figure it out.
There is a table called currency_exchange_rate table as follows, (exchange rate in LKR to other currency).this table is updating like one record for each currency in LKR in every month
exchange_rates
id currency_id start_date exchange_rate
1 5 2017-01-2 155
2 4 2017-01-3 25
3 6 2017-01-3 53
4 5 2017-02-1 156
5 4 2017-02-1 24
6 6 2017-02-1 54
There is a project table as follows
pro_id name value currency_id status_id owner_id date
1 studio1 500 5 1 44 2017-01-20
2 lotus 120 5 1 42 2017-01-21
3 auro 300 4 2 45 2017-01-21
4 studio2 400 6 1 44 2017-01-22
5 holland 450 4 3 46 2017-02-05
6 studio3 120 4 3 47 2017-02-06
7 studio4 400 6 3 48 2017-02-06
how to generate reports in one currency(DKK but exchange rate in LKR) like status wise,monthly total, total by owner, etc..
and we have to consider currency id,currency to be convert and exchange rate for the month for those currency types to get relevant value for project row.
hope you are clear about my scenario. your help is much appreciated.
I don't need every report. just want a sql for convert values in project table using exchange rates table or status wise report as follows
status_id value_in_one_currency
1 xxxx
2 xxxx
3 xxxx
Try this:
SELECT A.status_id, A.`value` * B.exchange_rate `value_in_one_currency`
FROM project A JOIN exchange_rates B
ON A.currency_id=C.currency_id
AND DATE_FORMAT(A.`date`,'%m-%Y')=DATE_FORMAT(B.`start_date`,'%m-%Y');
See MySQL Join Made Easy for some insight.
This is what I finalize:
I took currency_id=5 as the final currency to be converted
SELECT A.*,C.exchange_rate AS DKK,D.exchange_rate AS LKR, (order_value * D.exchange_rate /C.exchange_rate ) AS `converted_value`
FROM projects A
LEFT JOIN exchange_rates C ON (DATE_FORMAT(C.start_date,'%Y-%m')=DATE_FORMAT(A.`date`,'%Y-%m') AND C.currency_id=5)
LEFT JOIN exchange_rates D ON DATE_FORMAT(D.start_date,'%Y-%m')=DATE_FORMAT(A.`date`,'%Y-%m') AND D.currency_id=A.currency_id

How to use multiple aggregate function with group by

I have three tables in advertiser,advertiser_clicks and advertiser_cost in which i have used join with multiple aggregate function.But i was unable to get the actual record from the tables.Please find below my table structure.
Table1:advertiser
................
id name
................
1 Your Survey
2 Vindale
Table2:advertiser_clicks
In this table adv_id is foreign key of advertiser table id
.............................................................
id adv_id survey_id survey_name clicks dt_datetime
.............................................................
1 1 101 survey1 1 2017-11-30
2 2 102 survey2 1 2017-11-29
3 1 103 survey3 1 2017-11-28
Table3:advertiser_cost
In this table a_click_id is the foreign key of advertiser_clicks table adv_id
.......................................................
ac_id a_click_id cpi conversion cost profit
......................................................
1 1 10 1 8 2
2 2 10 1 5 5
3 3 5 1 5 5
I want below output:-when i select start date:2017-11-29 and enddate :2017-11-30 in where condition then i want below output.Here total click,cpi,cost & profit is the sum of all the data which comes in between that daterange.
.............................................................
adv_id advertiser totalclick cpi cost profit
.............................................................
1 Your survey 2 20 13 8
2 vindale 0 0 0 0
I have write the below query which returns the exact data which comes
in between that date range but i it will not return the record of vindale
advertiser as a zero value.Please find my below query.
SELECT sum(advertiser_clicks.`clicks`) as totalclick,sum(advertiser_cost.cpi) as revenue,sum(advertiser_cost.`conversion`) as conversion,sum(advertiser_cost.`cost`) as cost,SUM(advertiser_cost.profit) as profit,advertiser.name as advertiser,advertiser.id as adv_id FROM `advertiser_clicks` LEFT JOIN advertiser_cost on advertiser_cost.a_click_id=advertiser_clicks.`id` inner join advertiser on advertiser.id=advertiser_clicks.`adv_id` where advertiser_clicks.`dt_datetime`>='2017-11-29' and advertiser_clicks.`dt_datetime`<='2017-11-30' group by advertiser.name,advertiser.id
My query return this output:-
.............................................................
adv_id advertiser totalclick cpi cost profit
.............................................................
1 Your survey 2 20 13 8
Hi i have resolved my issue:-
SELECT sum(advertiser_clicks.`clicks`) as totalclick,sum(advertiser_cost.cpi) as revenue,sum(advertiser_cost.`conversion`) as conversion,sum(advertiser_cost.`cost`) as cost,SUM(advertiser_cost.profit) as profit,advertiser.name as advertiser,advertiser.id as adv_id FROM `advertiser` left join advertiser_clicks on advertiser.id=advertiser_clicks.`adv_id` and (advertiser_clicks.`dt_datetime` between '$startdate' and '$enddate' ) LEFT JOIN advertiser_cost on advertiser_cost.a_click_id=advertiser_clicks.`id` group by advertiser.name,advertiser.id

Get the Sum of the multiplication from 2 different tables depending upon an id

I have to get the sum of all the bill for a defined client, the multiplication depend of the id of the sell
Tables 1
bill id Amount
1 KM 3
2 NUI 7
3 ETP 11
Tables 2
ID Price
ETP 110
KM 15
NUI 80
For example, in this situation, I have to got 3 * 15 + 7 * 80 + 11 * 110
the request have to return 1815.
someone can help me ? I really don't know how to do this ...
This is a join query with aggregation:
select sum(t1.amount * t2.price)
from t1 join
t2
using (id);