This is what I am trying to do:
Get the sum of all rows grouping by date
Get all the rows individually
I have successfully done this... The problem is I want the order to change overall
Here is my table columns
[id][date][amount]
Example being:
1, 2013-10-01, 102.10
2, 2013-10-01, 256.15
3, ...
The output I get in Laymen's terms,
the first x of rows are the totals grouped by date
the rest of the rows are the individual amounts
The output I want in laymen's terms,
The first row is the total of the following rows by date, then rinse and repeat.
i.e.
2013-10-01, 200.00 <-- Total
2013-10-01, 150.00
2013-10-01, 50.00
2013-10-02, 300.00 <-- Total
2013-10-02, 150.00
2013-10-02, 150.00
Here is my query:
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t;
See my example at SQLFiddle!
Thanks in advance for any assistance on this.
Assuming that you mean you want to change the order of your results by date (and I typically don't make such an assumption), you would simply need to add an "ORDER BY" to your sql. The below should do the trick.
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t
ORDER BY date
Given your edits, it appears you want to sort by date, and then total amount descending:
SELECT
date,
round(sum(amount), 2) as total
FROM invoice
GROUP BY date
UNION ALL
SELECT
date,
round(amount, 2)
FROM invoice
ORDER BY date, total DESC
Updated Fiddle Demo
Related
Initially I have a data sheet like this
What I want is that after using the SQL the SQL will produce a table like this
I have just learned SQL, have tried count () and group by but not yet.
The TOTAL column will be counted based on the SIZE and NAME columns.
First you want to format your date accordingly using DATE_FORMAT(date, "%Y-%m") to get the format 2021-03. Then you can use GROUP BY along with COUNT(*).
SELECT
name,
size,
DATE_FORMAT(date, "%Y-%m") as month,
COUNT(*) as Total
FROM test
GROUP BY
name,
size,
DATE_FORMAT(date, "%Y-%m")
ORDER BY
COUNT(*) DESC,
name ASC;
name
size
month
Total
A
1
2021-03
2
B
2
2021-03
2
A
2
2021-03
1
C
3
2021-03
1
Working example at https://www.db-fiddle.com/f/7edM78Qhp8wLZ1JJZoE3bH/1
It's not clear what order you want the results returned in, so you can change the ORDER BY clause to customize it how you want.
select Name,Size,Cast(date as date) as Date,count(1) from tablename
group by Name,Size,Cast(date as date)
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 2 tables.ms_expese and track_expense.Using this table generate a fact table
I want the expense_name in ms_expense,expense_amount from track_expense.
I want to get the sum of expense_amount for a particular expense_name based on date.The date in the order of 1,2...12 as month id
SELECT DATE_Format(a.date,'%b') as month_id,b.expense_name AS expense_type, sum(a.expense_amount) AS expense_amount FROM ms_expense b JOIN track_expense a on a.`expense_id`=b.`expense_id` group by DATE_Format(a.date,'%b')
how to put the month id in the order of 1,2,..12 and my date format y-m-d
I get the month in apr,aug and so on but i need jan as 1,feb as 2
I have 25 expenses(expense name).In this query i got the total expense amount of first expense only.I want the total expense of all expenses in every month
CREATE TABLE fact AS
(<your select query>)
Your select query can be in the following form
SELECT MONTH(date)as month_id,expense_name,sum(expense_amount)
FROM ms_expense JOIN track_expense using (expense_id)
group by expense_name,MONTH(date)
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;
Just like what I asked in the title. For example, I have a table with ID and Total. I wanna list all the IDs and Totals if they satisfy that Total < 100. Otherwise, I want to group these rows with Total >= 100 in one row as ID is 000, and Total is the sum of those Totals.
May I know how I can achieve that?
Thank you.
You can use UNION
Select id, total
From TableA
Where total < 100
Union
Select 0 as id , sum(total) as total
From TableA
Where Total >=100