How should i write this Query [closed] - sql

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
i m trying to write a query. The result of query is some rows from the rows containf table on the basis of their type and latest entry.
Currently the table is sorted on the basis of FK and Dates and the result should be the latest
dates according to FK

How about using RANK,
WITH [Ranked] AS (
SELECT
RANK() OVER ( PARTITION BY [FK] ASC, ORDER BY [DATES] DESC ) [Rank],
[PK],
[FK],
[DATES]
FROM [YourTable])
SELECT
[PK],
[FK],
[DATES]
FROM [Ranked]
WHERE [Rank] = 1;
You'll have to tell us what happens when two rows have the same [FK] and [DATES].

Related

How to filter records in sql server? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a table called "Status" as shown below in sql server.
How can I filter the records where the timestamp is less then 5 minutes for the same Level and same Status?
Eg: For the above records my output should be like
Use lag():
select s.*
from (select s.*, lag(date) over (partition by level, status order by date) as prev_date
from status s
) s
where prev_date is null or
prev_date > dateadd(minute, -5, date);

how to do the count for if student count is greate then some number? [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 a table called teacher_student_course with teacher_id, student_id, and course_id how would I return a course_id for a course where the student count is lets say above 50?
please help my mind is shot its midnight!
You can group by the course_id and get all group having more than 50 records like this.
SELECT course_id
FROM teacher_student_course
GROUP BY course_id
HAVING COUNT(*) > 50
If you want to check if a course has more than 50 students or not, you need to use a similar query but with a JOIN as shown below.
SELECT tsc.course_id
FROM teacher_student_course tsc
INNER JOIN course ON course.id = tsc.course_id
WHERE course = 'course name'
GROUP BY tsc.course_id
HAVING COUNT(tsc.course_id)>50;
Demo for count greater than 4

SQL QUERY for finding set of highest numbers [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
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

SQL Server SELECT first [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
I am trying to create a JOIN with 2 tables and having ce first element of the joined table.
My tables look like this :
Product
id
name
Sales
idProduct
prices
date
I want to have the last sales price for each product but the function FIRST doesn't exist in SQL Server.
Someone have an idea ?
Thanks,
You can use a ranking function like ROW_NUMBER:
WITH CTE AS(
SELECT id, name, idProduct, prices, date,
RN = ROW_NUMBER() OVER (PARTITION BY idProduct ORDER BY date DESC)
FROM dbo.Product p INNER JOIN dbo.Sales s on p.id = s.idProduct
)
SELECT * FROM CTE WHERE RN = 1
Ranking Functions (Transact-SQL)
The CTE is a common-table-expression similar to a sub-query but more readable and maintainable.
If it's SQL Server, simply use:
SELECT TOP 1 *
FROM Product p
JOIN Sales s ON p.id = s.idProduct
ORDER BY s.Date DESC

Get user with most upvotes from Data Explorer [closed]

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