SQL create view with sum query [closed] - sql

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

Related

Take first row after group in SQL Server [closed]

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

fastest way to anti-join in postgres [closed]

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 1 year ago.
Improve this question
i have two tables. users and accounts.
accounts table :-
id | accountName | userId
--------------------
1 | natt | 1
2 | kelly | 2
3 | john | 3
users table :-
id | username |
--------------------
1 | natt#xyz.com |
2 | kelly#xyz.com |
3 | john#xyz.com |
4 | randy#xyz.com |
5 | jamie#xyz.com |
expected output: -
userId |
---------
4 |
5 |
as you can see id of users table act as foreign key in accounts table.
I want to fetch every user that does not have a account associated with it(4,5 in my example). i can do it via IN or NOT IN but thats not the fastest way. can some tell me the fastes way to do this? im using postgres.
SELECT T.ID,T.USERNAME
FROM USERS T
WHERE NOT EXISTS
(
SELECT 1 FROM ACCOUNTS A WHERE A.USERID=T.ID
)
You can try NOT EXISTS
SELECT U.userId FROM users U
LEFT OUTER JOIN accounts A ON U.id = A.userId
WHERE A.id IS NULL

How to combine columns into one column in SQL? [closed]

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

SQL total amount where deposits are added and withdrawls subtracted [closed]

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

Need help on SQL query (self join) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table like this
MAIN ID CONTENT SUB ID
ABCD ONE 888
ABCD TWO 888
which i would like the query result to be like this
MAIN ID SUB ID CONTENT1 CONTENT2
ABCD 888 ONE TWO
You can use the PIVOT function:
select
*
from (
select
[main id],
[sub id],
[content],
'content' + cast(
row_number() over (partition by [main id],[sub id] order by content)
as varchar(5)) as contentIX
from
table1
) T
pivot (max(Content) for contentIX in (content1,content2)) as content
The subquery first generates a field name for each result to pivot, content1, content2, etc. that looks like this:
| MAIN ID | SUB ID | CONTENT | CONTENTIX |
|---------|--------|---------|-----------|
| ABCD | 888 | ONE | content1 |
| ABCD | 888 | TWO | content2 |
Then the outer query performs a pivot over the CONTENTIX column to get the final result:
| MAIN ID | SUB ID | CONTENT1 | CONTENT2 |
|---------|--------|----------|----------|
| ABCD | 888 | ONE | TWO |
Demo: http://www.sqlfiddle.com/#!6/095bb/11