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;
Related
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
Please consider these data:
Id F1 F2 Ref_ID
-------------------------------------------
1 Nima 35 Null
2 Eli 33 Null
3 Arian 5 1
4 Ava 1 1
5 Arsha 3 2
6 Rozhan 30 1
7 Zhina 20 2
I want to sort this table like this result:
Id F1 F2 Ref_ID
-------------------------------------------
1 Nima 35 Null
3 Arian 5 1
4 Ava 1 1
6 Rozhan 30 1
2 Eli 33 Null
5 Arsha 3 2
7 Zhina 20 2
the refrenced records should place under the reference record based on Id ascending.
How I can do this using LINQ or SQL. Thanks
In SQL you could sort it by using a COALESCE or ISNULL for the Ref_id and the Id.
And an IIF or a CASE WHEN to make sure the parent id comes first in the same group of Ref_id.
SELECT Id, F1, F2, Ref_ID
FROM YourTable
ORDER BY COALESCE(Ref_ID, Id), IIF(Ref_ID IS NULL, 0, 1), Id;
A test on db<>fiddle here
Here is a more simple solution:
SELECT
Id,
F1,
F2,
Ref_ID
FROM
#Table
ORDER BY
ISNULL(Ref_ID,ID), ID
Result:
Using LINQ, you can do it like this:
from i in data
orderby i.Ref_ID ?? i.Id
select i;
Another solution is to add an extra column in the query, and sort on that column
select t.*
from ( select Id,
F1,
F2,
Ref_ID,
coalesce(Ref_ID, Id) as RefID_or_Id,
iif(Ref_ID is null, 0, 1) as Ref_ID_0_or_1
from YourTable
) t
order by t.RefID_or_Id,
t.Ref_ID_0_or_1,
t.Id
In case your table is large, you should test wich of the solutions here performs the best for you
It's hard to formulate, so i'll just show an example and you are welcome to edit my question and title.
Suppose, i have a table
flag id value datetime
0 b 1 343 13
1 a 1 23 12
2 b 1 21 11
3 b 1 32 10
4 c 2 43 11
5 d 2 43 10
6 d 2 32 9
7 c 2 1 8
For each id i want to squeze the table by flag columns such that all duplicate flag values that follow each other collapse to one row with sum aggregation. Desired result:
flag id value
0 b 1 343
1 a 1 23
2 b 1 53
3 c 2 75
4 d 2 32
5 c 2 1
P.S: I found functions like CONDITIONAL_CHANGE_EVENT, which seem to be able to do that, but the examples of them in docs dont work for me
Use the differnece of row number approach to assign groups based on consecutive row flags being the same. Thereafter use a running sum.
select distinct id,flag,sum(value) over(partition by id,grp) as finalvalue
from (
select t.*,row_number() over(partition by id order by datetime)-row_number() over(partition by id,flag order by datetime) as grp
from tbl t
) t
Here's an approach which uses CONDITIONAL_CHANGE_EVENT:
select
flag,
id,
sum(value) value
from (
select
conditional_change_event(flag) over (order by datetime desc) part,
flag,
id,
value
from so
) t
group by part, flag, id
order by part;
The result is different from your desired result stated in the question because of order by datetime. Adding a separate column for the row number and sorting on that gives the correct result.
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
I need to select from a single table where both id exists in the column having matching id in another column.
My query is like below which gives rows with even just single matching id.
select * from customer_appdata where appdata_id in(11,12)
id customer_id appdata_id
6 65 4
7 65 12
8 65 8
9 66 11
10 66 12
so here i just want last and second last rows(9,10) as they have both 11 and 12 with common id 66.
If I understand the problem correctly, this should work:
select * from customer_appdata where customer_id in (
select customer_id from customer_appdata
where appdata_id in (11,12)
group by customer_id
having count(distinct appdata_id) = 2
)
You find all customer_ids that are repeated specific number of times (that's the inner query) and then select all rows with those customer_ids. There is probably a faster way, but if performance is not critical this is a simple way to solve the problem.
This query selects rows with customer_appdata of 12 or 11 and uses exists to see if the other row is also in the table.
select * from customer_appdata c1
where appdata_id in (11,12)
and exists (
select 1 from customer_appdata c2
where c2.appdata_id in (11,12)
and c2.appdata_id <> c1.appdata_id
and c2.customer_id = c1.customer_id
)