I just wanted to add different columns from different tables... Has anyone any idea on how to do that?
Consider I have 3 tables as below
tv sales
AC sales
cooler sales
And the tables data as follows
1)Tv Sales
Id Date NoOfSales Totalamount
1 03/05/2014 10 10000
2 04/05/2014 20 20000
3 05/05/2014 30 30000
2)Ac Sales
Id Date NoOfSales Totalamount
1 03/05/2014 10 50000
2 04/05/2014 20 60000
3 05/05/2014 30 70000
3)cooler Sales
Id Date NoOfSales Totalamount
1 03/05/2014 10 30000
2 04/05/2014 20 60000
3 05/05/2014 30 70000
Now I want to add the "Totalamount" from all the tables for a particular "date"
for example I need totalamount on 03/05/2014 as 90000
In MySQL, the easiest way to do this is with union all and aggregation:
select date, sum(totalamount) as TotalSales
from ((select date, totalamount from TvSales
) union all
(select date, totalamount from AcSales
) union all
(select date, totalamount from CoolerSales
)
) t
group by date;
The reason you want to use union all is in case the dates are different in the various tables. A join makes it possible to lose rows.
Second, having three tables with the same format is an indication of poor database design. You should really have one table with the sales and a column indicating which type of product it refers to.
You could solve your problem by making a union of the information you want to aggregate on the different tables and them sum the amounts. This would look like:
SELECT t.Date,SUM(t.Totalamount)
FROM
(
SELECT Date,Totalamount
FROM tvSales
UNION ALL
SELECT Date,Totalamount
FROM acSales
UNION ALL
SELECT Date,Totalamount
FROM coolerSales
) t
WHERE t.Date='03/05/2014'
GROUP BY t.Date
It is important that the fields of the union have the same name and type. In case they haven't the same name you should create common aliases for the 2 columns across the 3 select queries and then work with these aliases on the main query. Also the UNION should be performed including the ALL keyword in order to avoid eliminating duplicate records across the three tables.
Related
I am still getting started learning Access.
I have 3 tables. Table one has Date as primary key and will have all dates. Tables 2 and 3 (Table 3 is mislabeled in the example image as a second Table 2) will both have 2 columns, Date and Amount. Tables 2 and 3 could have multiple rows with the same date (different amounts) and some may miss dates. I am looking for an output query that would have 1 row for every date in table 2 & 3 that has an amount (some dates may not have an amount in either table) and sums all those amounts for that date in 1 row. Below are example tables and the desired output query. Thanks so much for the newbie help!
I now have this code (Note that I have eliminated Table 1):
SELECT Table2.Dat, Sum(Table2.Amount) AS [Sum Of Amount], Sum(Table2.Tax) AS [Sum Of Tax]
FROM Table2
GROUP BY Table2.Dat;
UNION ALL SELECT Table3.Dat, Sum(Table3.Amount) AS [Sum Of Amount], Sum(Table3.Tax) AS [Sum Of Tax]
FROM Table3
GROUP BY Table3.Dat;
This sums the amounts from same dates for each seperate table, but does not sum the dates for both tables. I imagine it is another GROUP function but I have not been successful in forming it correctly.
Current Results from code above
Try below query.
SELECT tt.mDate AS TransactionDate, Sum(tt.SumOfAmount) AS AmountTotal
FROM (SELECT Table2.tDate as mDate, Sum(Table2.Amount) AS SumOfAmount
FROM Table2
GROUP BY tDate
UNION
SELECT Table3.tDate As mDate, Sum(Table3.Amount) AS SumOfAmount
FROM Table3
GROUP BY tDate) AS tt
GROUP BY tt.mDate;
I have a table that contains random data against a key with duplicate entries. I'm looking to remove the duplicates (a projection as it is called in relational algebra), but rather than discarding the attached data, sum it together. For example:
orderID cost
1 5
1 2
1 10
2 3
2 3
3 15
Should remove duplicates from orderID whilst summing each orderID's values:
orderID cost
1 17 (5 + 2 + 10)
2 6
3 15
My assumption is I'd use SELECT DISTINCT somehow, but I don't know how I'd go about doing so. I understand GROUP BY might be able to do something but I am unsure.
This is a very basic aggregation:
SELECT orderId, SUM(cost) AS cost
FROM MyTable
GROUP BY orderId
This says, for each "orderId" grouping, sum the "cost" field and return one value per group.
You can use the group by clause to get one row per distinct values of the column(s) you're grouping by - orderId in this case. You can the apply an aggregate function to get a result of the columns you aren't grouping by - sum, in this case:
SELECT orderId, SUM(cost)
FROM mytable
GROUP BY orderId
I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!
Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo
This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);
I have a table with 2 dates in it and a product, and I need to get the average days difference between them considering just the last 3 rows for each product.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121
This gives me the average of all the date differences for product 121
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121 LIMIT 3
Still gives me the average off all the records, ignoring the LIMIT argument.
Also when I try a different approach, it also does ignore the last argument and shows the average off all the rows.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product =121 && date1 > 2015-01-01
Any idea on how to fix this or what I'm doing wrong?
When you have problems like this, I recommend breaking it up and putting it back.
Before doing any calculations, you know that you need the last three rows for each product. So, if you want for example the rows with the latest date2 you can select them by doing the following:
SELECT *
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3;
That will select the 3 latest rows you want. Then, just use that as a subquery to preform the aggregation. This way, the calculations are only made on the rows you are concerned with:
SELECT product, AVG(DATEDIFF(date2, date1))
FROM(
SELECT product, date1, date2
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3) tempTable;
Got some problem after using inner join, this is my query
> insert into total(ID,Grade) select midsemester.ID,(midsemester.grade +
> endsemester.grade) as total from midsemester inner join endsemester on
> midsemester.ID = endsemester.ID
This is the table:
Table name: midsemester
ID Grade
1 10
2 30
3 40
Table name: endsemester
ID Grade
1 30
2 40
3 20
and i need to sum these table to new table called total. This is the results that i was hoping.
Table name: total
ID Grade
1 40
2 70
3 60
I actually just need to sums up the grade's value using the id for the 3rd table. And tried several times using inner join, it's working. But when I try to insert a new data, the table of total can't sum a new data. Would appreciate any help, thanks! :)
I think you wang union all with an aggregation:
insert into total(ID,Grade)
select ID, sum(grade) as total
from (select id, grade from midsemester union all
select id grade from endsemester
) me
group by id;
However, this is a bad structure for the grades. You should store them all in one table, with a column indicating whether the grade is "midsemester" or "endsemester".