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

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

Related

Find exact match or first bigger number in Access database [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 months ago.
Improve this question
I have next problem.
There is a table with four columns:
|ID | X | Y | VAL |
|:--|:--:|:--:|----:|
|1 | 1 | 1 | 1110|
|2 | 1 | 2 | 1720|
|3 | 1 | 3 | 2330|
|4 | 1 | 4 | 2940|
|5 | 1 | 5 | 3550|
...
When user enter some value in text field e.g. 2370 i need function to find is there exact match in VAL field and if not to find very first bigger than 2370 (2940) and return ID value.
In some other language I can do it trough dictionaries and so one but in VBA I simply don't have idea.
Any idea or help will be appreciated.
You can use a query to get this answer, using TOP 1 to just return 1 record:
SELECT TOP 1 tblData.ID, tblData.VAL
FROM tblData
WHERE (((tblData.VAL)>=2370))
ORDER BY tblData.VAL ASC;

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 insert parent table all ids into child table using foreign key reference in sql server? Update all foreign key values? [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 4 years ago.
Improve this question
How to insert parent table primary keys ids into Child table using foreign key references in sql server?
As your question is not clear, this answer still can be edited according to the information you provide.
The only thing I can imagine you are trying to do is:
UPDATE Child
SET ParentID = CASE WHEN ChildID IN (1, 2) THEN 1 ELSE 2 END;
SELECT *
FROM Child;
Which will return:
+---------+-----------+----------+
| ChildID | ChildName | ParentID |
+---------+-----------+----------+
| 1 | CollPad | 1 |
| 2 | MicroMax | 1 |
| 3 | Dell | 2 |
| 4 | Mac | 2 |
+---------+-----------+----------+
If this is not the case, please edit your question to include more information and description to the issue you are trying to acheive.

SQL create view with sum query [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 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

writing an SQL statement with JOIN [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
so I have 3 tables
table 1: team
| team_id | name |
-----------------------
| 1 | alpha |
| 2 | beta |
| 3 | gamma |
table 2: buildings
| building_id | name |
---------------------------
| 1 | Baxter |
| 2 | LexCorp |
table 3: team location
| team_id | building_id |
-------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
What I need now is an sql query that will list the names of the teams located in the baxter building and I cant for the life of me even think where to begin on this one, im quite new to SQL
try this one
SELECT team.name FROM team_location
INNER JOIN buildings ON buildings.building_id = team_location.building_id
INNER JOIN team ON team.team_id = team_location.team_id
WHERE buildings.name = 'Baxter'
SELECT t.NAME
FROM team as t
INNER JOIN teamLocation AS tl ON t.team_id=tl.team_id
INNER JOIN buildings As b ON tl.building_id=b.building_id
WHERE b.name='Baxter'
Please check this sql fiddle
http://sqlfiddle.com/#!6/45e2e/1