Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Given a table named OrderInfo like below, I would like to sum the quantity and price for each order.
Table:
OrderID | Cost | Qty
0001 | 1.1 | 1
0001 | 2.2 | 3
0001 | 3.3 | 2
0002 | 10 | 5
0003 | 4.5 | 1
0003 | 9.9 | 1
...
Result:
OrderID | Cost | Qty
0001 | 6.6 | 6
0002 | 10 | 5
0003 | 14.4 | 2
...
Attempt:
SELECT OrderID, Sum(Cost) AS TotCost, Sum(Qty) AS TotQty
FROM OrderInfo
GROUP BY OrderID
My end goal is to use the resulting table to get the average of each order, so if there is an easier way to get that without summing cost and qty, then that solution will be nice!
End Goal:
OrderID | Average
0001 | 1.1
0002 | 2.00
0003 | 7.2
To get the values you want, just divide your calculations:
SELECT OrderID, Sum(Cost) AS TotCost, Sum(Qty) AS TotQty,
Sum(Cost) / Sum(Qty) AS AvgPerUnit
FROM OrderInfo
GROUP BY OrderID;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I am a having a table with columns id, name, and date_time. I need to sort the table according to the date_time column.
Actual data in database:
id | name | date_time
----+----------------+----------------------------
1 | The flat | 2022-07-06 09:17:07.990454
2 | The Smetha | 2022-08-08 09:17:07.990454
3 | Kingston | 2022-07-02 04:17:08.990454
4 | The Oxford U | 2022-08-08 04:14:08.990454
5 | Studio Element | 2022-08-08 01:14:08.990454
6 | The Davils | 2022-08-02 04:14:08.990454
7 | Latitude | 2022-09-08 04:14:08.990454
8 | Star Eight | 2022-08-08 06:14:08.990454
9 | Cottages one | 2022-07-08 05:14:08.990454
I want to sort the date_time column in ASC order. Can I use the RANK() function?
Ex:
id | name | date_time
----+----------------+----------------------------
1 | Kingston | 2022-07-02 04:17:08.990454
2 | The flat | 2022-07-06 09:17:07.990454
3 | Cottages one | 2022-07-08 05:14:08.990454
4 | The Davils | 2022-08-02 04:14:08.990454
5 | Studio Element | 2022-08-08 01:14:08.990454
6 | The Oxford U | 2022-08-08 04:14:08.990454
7 | Star Eight | 2022-08-08 06:14:08.990454
8 | The Smetha | 2022-08-08 09:17:07.990454
9 | Latitude | 2022-09-08 04:14:08.990454
I want to sort the date_time column in ASC order. But here we should not use [...] ORDER BY
That's like saying "I want so get the sum of A and B, but we should not use A+B"
ORDER BY is the way to sort tables in SQL. Tables in relational databases (as explained in comments) are not excel sheets, they are a set of records, and they lack an implicit order.
Like others have said that the way to be sure that it will sort in a specific order is by specifying order by column, but in your case I did find something that does order by date_time without saying explicitly, but it's not guaranteed, and you need to add a new column, so if you really need to sort it without using order by this can be a work around.
select row_number() over(partition by date_time), id, name, date_time
from table
See db<>fiddle.
The reason why this would work is because it looks on each date and time in date_time column, and returns you groups of each date_time, so it really sorts it per date_time. So if you need to do the sort without using order by, you can use row_number(). But the real way to sort is by using order by.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Does anyone know how to take always the first row after group by in SQL Server? Look on the screenshot for better explanation:
Result after select:
+------+---------+------+
| NAME | CAR | AGE |
+------+---------+------+
| Alex | BMW | 5 |
+------+---------+------+
| Alex | Audi | 2 |
+------+---------+------+
| Tom | VW | 10 |
+------+---------+------+
| Tom | Renault | 4 |
+------+---------+------+
| Tom | Peugeot | 2 |
+------+---------+------+
Expected result after group by:
+------+-----+
| NAME | CAR |
+------+-----+
| Alex | BMW |
+------+-----+
| Tom | VW |
+------+-----+
You can try to use the ROW_NUMBER() window function with PARTITION_BY clause. This function assigns a sequential integer to each row within the partition of a result set. The row number starts with 1 for the first row in each partition.
After that, you can use the where clause to select rows that have row numbers as 1.
You can follow this article for a better understanding.
Below is just an example (As I don't know how your query works):
select *
from
(
SELECT
ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) row_num,
*
from(
-- your main group by query
)
)
where row_num=1
you should be able to get the top 1 record for each person using the below query, let me know if this works for you.
SELECT * FROM car_owners GROUP BY person_name;
let me know if you want to order the records in alphabetical order ASC or DESC and then GROUP BY them
Thank You
enter image description here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to sum some columns by id. But I'm retrieving 1 raw.
For Example
Id | C1| C2|
---------------------------------
1 | 2 | 1 |
2 | 5 | 4 |
3 | 3 | 1 |
4 | 5 | 2 |
Result that I trying to get:
Id |Total
---------------------------------
1 | 3 |
2 | 9 |
3 | 4 |
4 | 7 |
How can I combine columns into one?
You just need addition operation + as follows:
select id, c1+c2 as total
from your_table
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
-------------------------------------------
| CustomerID | Action | Amount |
|-------------------------------------------|
| 111 | deposit | 100 |
| 111 | withdrawl | 25 |
| 222 | deposit | 500 |
| 222 | deposit | 100 |
| 333 | withdrawl | 100 |
| 333 | deposit | 100 |
-------------------------------------------
Write a query that for each customerID would display the total Amount for that customer, where Deposit Actions are added to the total and Withdrawls are substracted from total
Using a CASE WHEN, you can treat the AMOUNT column differently in calculation based on the value of another field(ACTION).
SELECT CUSTOMERID,
SUM(CASE WHEN ACTION = 'DEPOSIT' THEN AMOUNT
WHEN ACTION = 'WITHDRAWL' THEN -AMOUNT ELSE 0 END) AS TOTAL_AMOUNT
FROM
TABLE1
GROUP BY CUSTOMERID;
You can directly use IF - ELSE logic ( if withdrawl or other "there's only one alternative") to write a simple query by using DECODE pseudo-code :
select CustomerID "Customer ID", sum(decode(Action,'withdrawl',-Amount,Amount)) "Total Amount"
from inventory
group by CustomerID
order by CustomerID;
D e m o
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have following registration table
| EMP_ID | START_DATE | END_DATE | PNUM |
| 1 | 2014-10-20 | 2014-10-25| 10 |
| 2 | 2014-10-20 | 2014-10-30| 30 |
And i want following result in view
| START_DATE | END_DATE | TOTALNUM |
| 2014-10-20 | 2014-10-25| 40 |
| 2014-10-20 | 2014-10-30| 40 |
And i have tried to create view with sum query but no success .
create view EMP
as
select START_DATE ,END_DATE,(select SUM(PNUM) from s) TOTALNUM
from s
group by [START_DATE],END_DATE
Assuming that there is no grouping but just selecting every row and show it's start & end date and the sum of PNUM of all the rows:
SELECT START_DATE, END_DATE, SUM(PNUM) FROM TableX