Re-Organize Access Table by converting Rows to Columns - sql

I'm pretty new to access and SQL and need some help re-organizing a table. I have the following table (sorry for the table below - having trouble posting):
ID GroupID Distance Code Start_Finish
1 44 7 A S1
2 44 14 A F1
3 45 12 B S1
4 45 16 B F1
5 45 31 C S2
6 45 36 C F2
7 45 81 B S3
8 45 88 B F3
And need for the table to be transformed into:
GroupID Code Start_Distance Finish_Distance
44 A 7 14
45 B 12 16
45 C 31 36
45 B 81 88

try something like this
Select GroupID, Code, min(distance) as Start_distance, max(distance) as Finish_distance
from Table
group by GroupID, Code
If the min and max functions don't give you what you need, try it with First() and Last() instead.
Oops - just noticed you have 2 different entries in the output for GroupID 45 Code B - is that a requirement? With that data structure and requirement, the problem gets much more difficult.
Now I see the final column in the 1st table - I think that can be used to get the output you want:
Select GroupID, Code, mid(start_finish,2) as T, min(distance) as Start_distance, max(distance) as Finish_distance
from Table
group by GroupID, Code, T

You can use conditional aggregation for this.
select GroupID
, CODE
, max(case when Left(Start_Finish, 1) = 'S' then Distance end) as Start_Distance
, max(case when Left(Start_Finish, 1) = 'F' then Distance end) as Finish_Distance
from SomeTable
group by GroupID
, CODE

Related

Linking different ids through interactor

I want to link different ids through the common interactor. It is bit complex but i will try my best to frame the problem.
Here are the list of steps
1. Extract id from table A.
Table A
ID Interactor
1 30
2 40
Get the list of interactors corresponding to id from table B. for example,
select * from table B where id = 1
Table B
ID Interactors
1 30
1 32
1 33
1 36
1 38
1 39
Iterate through each interactor from the list and get the list of ids from table A.
Table A
ID Interactors
1 30
70 32
76 33
Null 36
89 38
75 39
2 45
2 40
2 43
4.Join these different ids so that when i select 1 i should get the below result.
Select * where id = 1
Result
ID Interactors
1 30
70 32
76 33
89 38
75 39
I want to achieve this using sql.
Try this:
select B.ID, B.Interactors
from A inner join B
where A.Interactors = B.Interactors
and A.ID = 1
From step 3 you have table A, and before that you have table B.
You can use simple inner join with some where condition to get your desired result.
Select Id, Interactors from
( select tableA.id, tableA.Interactors
from tableA
inner join tableB
on tableA.Interactors = tableB.Interactors
and tableA.Id is not null --- this is required since in your output record having NULL id correspond to tableA is not considered
) as db
where db.Id = 1 ---- you can apply any filter over there to get your desired result.

Sql getting MAX and MIN values based on two columns for the ids from two others

I'm having difficulties figuring a query out, would someone be able to assist me with this?
Problem: 4 columns that represent results for the 2 separate tests. One of them taken in UK and another in US. Both of them are the same test and I need to find the highest and lowest score for the test taken in both countries. I also need to avoid using subqueries and temporary tables. Would appreciate theoretical ideas and actual solutions for the problem.
The table looks like this:
ResultID Test_UK Test_US Test_UK_Score Test_US_Score
1 1 2 48 11
2 4 1 21 24
3 3 1 55 71
4 5 6 18 78
5 7 4 19 49
6 1 3 23 69
7 5 2 98 35
8 6 7 41 47
The desired results I'm looking for:
TestID HighestScore LowestScore
1 71 23
2 35 11
3 69 55
4 49 21
5 98 18
6 78 41
7 47 19
I tried implementing a case of comparison, but I still ended up with subquery to pull out the final results. Also tried union, but it ends up in a sub query again. As far as I can think it shoul be a case when then query, but can't really come up with the logic for it, as it requires to match the ID's of the tests.
Thank you!
What I've tried and got the best results (still wrong)
select v.TestID,
max(case when Test_US_Score > Test_UK_Score then Test_UK_Score else null end) MaxS,
min(case when Test_UK_Score > Test_US_Score then Test_US_Score else null end) MinS
FROM ResultsDB rDB CROSS APPLY
(VALUES (Test_UK, 1), (Test_US, 0)
) V(testID, amount)
GROUP BY v.TestID
Extra
The answer provided by M. Kanarkowski is a perfect solution. I'm no expert on CTE, and a bit confused, how would it be possible to adapt this query to return the result ID of the row that min and max were found.
something like this:
TestID Result_ID_Max Result_ID_Min
1 3 6
2 7 1
3 6 3
Extra 2
The desired results of the query would me something like this.
The two last columns represent the IDs of the rows from the original table where the max and min values were found.
TestID HighestScore LowestScore Result_ID_Of_Max Result_ID_Of_Min
1 71 23 3 6
2 35 11 7 1
3 69 55 6 3
For example you can use union to have results from both countries togehter and then just pick the maximum and the minimum for your data.
with cte as (
select Test_UK as TestID, Test_UK_Score as score from yourTable
union all
select Test_US as TestID, Test_US_Score as score from yourTable
)
select
TestID
,max(score) as HighestScore
,min(score) as LowestScore
from cte
group by TestID
order by TestID
Extra:
I assumed that you want to have the additional column with the previous result. If not just take the above select and replace Test_UK_Score and Test_US_Score with ResultID.
with cte as (
select Test_UK as TestID, Test_UK_Score as score, ResultID from yourTable
union all
select Test_US as TestID, Test_US_Score as score, ResultID from yourTable
)
select
TestID
,max(score) as HighestScore
,min(score) as LowestScore
,max(ResultID) as Result_ID_Max
,min(ResultID) as Result_ID_Min
from cte
group by TestID
order by TestID

how to get a distinct value when the columns have different data

Consider the below table
KEY WO# Parts Name
77 11 1 aa
77 11 2 aa
81 12 2 bb
82 9 3 cc
Now i want to write a query for my report so that whenever theres a duplicate RKEY i want it to ignore it even though the number of parts in the duplicate RKEY are different and proceed with the next RKEY. Is this possible
End result
KEY WO# Parts Name
77 11 1 aa
81 12 2 bb
82 9 3 cc
This has been asked and answered literally hundreds of times. But it is faster to type out a solution than find an existing answer.
select [KEY]
, [WO#]
, Parts
, Name
from
(
select [KEY]
, [WO#]
, Parts
, Name
, RowNum = ROW_NUMBER() over (partition by KEY order by Parts)
from YourTable
) x
where RowNum = 1
EatPeanutButter is correct: If you use the 'group by' command on 'Key', only the first instance of 'Key' will be shown in the end result.
select * from table group by Key;

MSSQL - Grouping Results using MAX()

I have this dataset;
dID Num
11 3
11 4
11 5
13 9
13 11
45 3
45 8
99 44
99 78
99 53
I want it to look like this.
dID Num
11 5
13 11
45 8
99 78
List all ID's and only show those ID's where the 'Num' is the Largest number for that group of ID's
my attempt here doesnt quite work out.
http://sqlfiddle.com/#!9/1a47f/1
You seem to just want an aggregation query:
select dId, max(num) as num
from data t
group by dId;
You need to aggregate by the first column, not the argument to the aggregation function.
If you have oversimplified the problem, and want other columns as well, then use row_number():
select t.*
from (select t.*, row_number() over (order by num desc) as seqnum
from data t
) t
where seqnum = 1;
You almost get it right, you just grouped by the wrong column:
select dID,
MAX(num) from data
group by dID
See it here: http://sqlfiddle.com/#!9/1a47f/3

Database Query Help required in MySQL

I am looking for help in writing a query of retrieving the values from 2 tables in MySQL.
The scenario is
Table A
ID Name Marks
===================
23 John 67
45 Mark 45
12 Ram 87
Table B has the following Structure
ID Name Evaluation Marks
==============================
45 Mark 34
78 Chris 09
98 Nancy 10
23 John 12
I am trying to write a query, where if I execute the following query
Select "SOMETHING" from Table A where Id=45
I should get Marks Column as 45+34=79, which should fetch and sum from the both the Tables A and Table B.
If I execute the query with the Id=12.
Since the Id=12, does not exists in the Table B, I should get the Marks as 87.
What would a query for the above?
I assume that the id occurs only once in your tables table a, but could be missing in both. If it always exists in table a, you can use a LEFT JOIN instead of the UNION.
SELECT COALESCE(SUM(marks), 0)
FROM
(
SELECT marks FROM a WHERE id = 45
UNION ALL
SELECT SUM(evaluation_marks) AS marks FROM b WHERE id = 45
) x
Edit
If you have all users in table a, then use
SELECT a.marks + COALESCE( SUM( b.evaluation_marks ), 0 )
FROM a
LEFT OUTER JOIN b ON ( b.id = a.id )
WHERE a.id = 45
GROUP BY a.id, a.marks
You should consider changing your table model though. Why do you store name and id twice? Can't you do it like that:
id name marks evaluation marks
=======================================
12 Ram 87 0
23 John 67 12
45 Mark 45 34
78 Chris 0 9
98 Nancy 0 10