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

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

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

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

Concatenate as A and choose either A or B according to their FKs [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 three tables:
IDcontacts | FirstName | LastName
----------------------------------
1 | Walt | Ne
IDcompany | CompanyName
------------------------
1 | Universe
IDowner | IDcontacts | IDcompany
---------------------------------
1 | 1 | NULL
2 | NULL | 1
3 | NULL | NULL
I need a query that will give me the following output:
IDoutput | Name
--------------------
1 | Walt Ne
2 | Universe
3 | NULL
To do a join and get half a result even if the other record does not exist, use an outer join.
To chose the first non-NULL value of a list, use COALESCE:
SELECT IDowner as IDoutput,
COALESCE(FirstName || ' ' || LastName, CompanyName) AS Name
FROM Owner
LEFT JOIN Contacts ON Owner.IDcontacts = Contacts.IDcontacts
LEFT JOIN Company ON Owner.IDcompany = Company .IDcompany