Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to find the user with the most upvotes from SE's data explorer: https://data.stackexchange.com/stackoverflow/query/105658
SELECT
Users.DisplayName as TheUser,
COUNT(Votes.Id) as TheUpvotes
FROM
Votes,
Users
WHERE
Votes.VoteTypeId=2
AND
Votes.UserId=Users.Id
GROUP BY
Users.DisplayName
This query returns 0 results. I can't figure out why. Can you?
Actually you don't need to join some tables. All that you need are found on table User.
UPVOTES as well as DOWNVOTES are anonymous that is why you cannot count it. The value already exist on column UpVotes and DownVotes
Query
SELECT [User Link], UpVotes, rn
FROM
(
SELECT a.ID as [User Link],
a.UpVotes,
DENSE_RANK() OVER (ORDER BY a.UpVotes DESC) rn
FROM Users a
)x
WHERE rn <= 10
ORDER BY UpVotes DESC
Related
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 11 hours ago.
Improve this question
I have this table of employees that gets updated at the end of each month. I know how to detect changes (2 rows; one of old and one of new) but I need to summarize the changes as showing in the second table using sql query.
With JobHistory AS(
Select
*,
LEAD([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as NewDepartmentName,
LAG([DepartmentName]) over (Partition by [EmployeeNumber] order by [ASofDate]) as OldDepartmentName
FROM [EmployeeDirectory]
)
Select *
FROM JobHistory
WHERE (
(NewDepartmentName <> [DepartmentName])
OR
( OldDepartmentName <> [DepartmentName])
)
ORDER by AsOfDate, EmployeeNumber
[Table]
Required Results
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 2 years ago.
Improve this question
I have a purchase order table that has a list of order numbers and associated order lines with different dates. I want to a query to fetch the orders with orderlines that has the most recent date.
I want the fetch to result in something like below
Here is a way to do this using row_number
select * from (
select t.*
,row_number() over(partition by ordernumber,orderlinenumber order by date desc) as rnk
from <your_table> t
)x
where x.rnk=1
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 3 years ago.
Improve this question
How can I write this query without TOP and Limit only use the SQL
You just sort the data one way, grab the records you want, then sort it the other way:
SELECT SOME_FIELD
FROM (SELECT st.*
FROM SOME_TABLE st
ORDER BY SOME_FIELD DESC)
WHERE ROWNUM <= 7
ORDER BY SOME_FIELD ASC
dbfiddle here
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 4 years ago.
Improve this question
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);
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
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER