Using SUM() without grouping the results
SELECT a.id, b.amount
FROM table1 a
CROSS JOIN
(
SELECT SUM(amount) amount FROM table1
) b
You need to perform a cartesian join of the value of the sum of every row in the table to each id
. Since there is only one result of the subselect (49
), it basically just gets tacked onto each id
.
SQL Sum Multiple Rows Without Group By
Well looks like my comment already solve it, but I was already doing this in case the problem is you didn't know what are the
event_id
for each date
The trick is select up to 5 events from each loc_name, eventDate
In this case I select the ones with more teams. You are free to add any filter you want.
with cte as (
SELECT *, ROW_NUMBER() OVER (PARTITION BY loc_name, eventDate
ORDER BY Number_of_Teams DESC) as rn
FROM tblEventDate
WHERE eventDate >='09/01/2013'
)
SELECT loc_name,
eventDate,
COUNT(t_ID) as Number_of_Teams,
SUM(NbrOfPeople) as Number_of_People
FROM cte
WHERE rn <= 5
GROUP BY loc_name, eventDate
ORDER BY eventDate, loc_name
How to make a SUM without group by
You need a to use a window function - http://www.postgresql.org/docs/9.3/static/tutorial-window.html
Something like:
(Sum(Auction) OVER ()) - actual AS Remaining
How to sum two columns in sql without group by
SELECT
*,
(ConvertedPages + ChangedPages) as PageCount
FROM Table
Get sum without group by but with subquery
To get your current SQL to work using GROUP BY you would need the following (ie the SUM aggregate function):-
select NAME, SUM(BAL)
from VS, BALANCE
WHERE VS.V_ID = BALANCE.V_ID
group by NAME
Note that this is just your original SQL minimally modified to work and it still uses the implicit join you coded. Implicit joins should generally be avoided and better to use explicit INNER JOIN....ON syntax:-
select NAME, SUM(BAL)
from VS
INNER JOIN BALANCE
ON VS.V_ID = BALANCE.V_ID
group by NAME
If you really want to avoid the GROUP BY then it is possible using a sub query, but is likely to be slower (as it effectively has to perform an extra query for every row tha main query returns):-
select NAME, (SELECT SUM(BAL) FROM BALANCE WHERE BALANCE.V_ID = VS.V_ID)
from VS
EDIT, in response to your comment on sub queries.
Correlated sub queries effectively force MySQL to get a result set, and then for each row on the result set to perform another query. Most of the time they are used to get an aggregate value (such as the max value of a field related to a row on the returned row, but where GROUP BY on the main query would not be viable).
For example if you had a list of comments for a user, but on each row you wanted to know the date of the latest comment from that user you might do the following:-
SELECT users.user_name, comments.comment_date, (SELECT MAX(comment_date) FROM comments WHERE comments.user_id = users.id) AS latest_comment_date
FROM users
INNER JOIN comments
ON users.id = comments.user_id
This could be written to do a non correlated sub query using GROUP BY which is then joined:-
SELECT users.user_name, comments.comment_date, latest_comment_date
FROM users
INNER JOIN comments
ON users.id = comments.user_id
INNER JOIN
(
SELECT user_id, MAX(comment_date) AS latest_comment_date
FROM comments
GROUP BY user_id
) sub1
ON users.id = sub1.user_id
If you are dealing with a large number of records on users this would likely be faster.
However if you were only dealing with a tiny number of records on users (and determining that number was quite complex), getting ALL the max comment dates would be an unnecessary overhead, and it forces a join against a sub query which isn't likely to use indexes.
How to user PARTITION BY without GROUP BY clause
You can aggregate your value normally and then aggerate again using a window function.
SUM(Premium) OVER (Partition by PolicyNumber)
becomes
SUM(SUM(Premium)) OVER (Partition by PolicyNumber)
LINQ | How do I get SUM without grouping?
Would this work:
var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1 );
var answer2Sum = SurveyAnswers.Sum( survey => survey.Answer2 );
var answer3Sum = SurveyAnswers.Sum( survey => survey.Answer3 );
Related Topics
Understanding the Differences Between Cube and Rollup
Any Detailed and Specific Reasons for Why Mongodb Is Much Faster Than SQL Dbs
Query Grants for a Table in Postgres
Delete Rows with Foreign Key in Postgresql
In MySQL, How to Copy the Content of One Table to Another Table Within the Same Database
Finding the Data Types of a SQL Temporary Table
Most Recent Record in a Left Join
Difference Between Stored Procedures and User Defined Functions
How to See the SQL That Will Be Generated by a Given Activerecord Query in Ruby on Rails
Select Top N Rows for Each Group
SQL Queries on String Columns - Sorting According to Language
Difference Between Inner Join and Left Semi Join
Finding All Children in a Hierarchy SQL
Oracle Insert via Select from Multiple Tables Where One Table May Not Have a Row