Grouping of values in sql - sql

My dataset looks like this:
Id MaxSpeed Distance
1 112 33
1 89 56
2 100 34
3 125 10
For each Id, I need to set up the count as 1.
Output has to be
Id count
1 1
2 1
3 1
I tried with group by on Id, it does not fetch me this result.
Any help would be really appreciated !!

Perhaps you are overthinking it.
Select Distinct ID,1 as Count From YourTable

Does the count always need to be 1? If so, could this query work?
SELECT DISTINCT ID, 1
FROM your_table;

Try following sql code:
Select distinct Id,1 as count From Table_Name;

Related

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

Window function in SQL

I'm trying to get the column 'Desired Result' from the data presented. I'm not sure of the correct syntax for the windows function to do this. I've tried row_number and versions of RANK but just can seem to get it correct. Any help would be appreciated. thanks
ID TransactionID Desired Result
33 2681998458900 1
44 2682232081872 1
44 2682232081872 1
44 2682232081872 1
44 2683269923140 2
44 2683269923140 2
44 2683269923140 2
60 2345620067722 1
Select ID, TransactionID, Dense_rank() over (partition by ID order by transactionID)
Rank
from table1
This would give your expected results. Dense rank will not skip ranking and will give the same rank if the ordering column has the same value.

Count total from multiple row results using SQL

say I have a table like this
user, count
1 5
2 3
3 20
4 13
5 10
I want to get the total from all the count row, so basically 5+3+20+13+10=51, I want to get the 51
How to do that?
Thanks.
select sum(count) as Total from tablename;
select sum(count) from yourtablename

Best way to get next row based on primary key of any row in a table with sequence

I have a table like this
ID Name Sequence
--------------------------
45 Alex 1
22 John 2
2 Philip 3
65 Shine 4
356 Stephy 5
35 Tom 6
OK.. Here when I pass the ID 2 I should get the row of Shine since that's the next row based on the sequence after Philip (2)
Which is the best solution?
Does this do what you want?
select top 1 t.*
from table t
where sequence > #id
order by sequence;
select top 1
t.*
from
myTable t
where
sequence > #param
order by sequence
I found an answer to my own question. It may be helpful for me such beginners but only available in SQL 2012
select * from (SELECT id,name,
LAG(id,1) OVER (ORDER BY sequence ) PrevID,LEAD(id,1) OVER (ORDER BY sequence ) NextID,sequence
FROM tbl) as mytable where previd=3
This will return
ID Name Sequence
--------------------------
65 Shine 4

Difficulty in creating update totals query on same table

Consider the following table:
ID nonUniqueID value total
--------------------------
1 12345 5 x
2 12345 10 x
3 789 20 x
4 789 5 x
I need to make a query something like this (psuedo SQL), which will work within Access 2007:
UPDATE table
SET total = SUM(value)
WHERE nonUniqueID IS SAME;
The result should be as follows:
ID nonUniqueID value total
--------------------------
1 12345 5 15
2 12345 10 15
3 789 20 25
4 789 5 25
I've tried group bys, but I got odd results that quite frankly, I could not interpret. Does anybody know how I could achieve something like this?
Not sure if this works in Access or not, but give it a try:
update table t1
inner join (
select nonUniqueID, sum(value) as SumValue
from table
group by nonUniqueID
) t2 on t1.nonUniqueID = t2.nonUniqueID
set t1.total = t2.SumValue
Update: Based on this question, it looks like it is not going to work. But give it a shot! If it doesn't, you can use the approach suggested in that question.
Another possible option:
update t
set total = (select SUM(value) from table where nonUniqueID = t.nonUniqueID)
from table t