SQL QUERY for finding set of highest numbers [closed] - sql

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

Related

How can I extract both column from View named "Max_part_supplied'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have created a view named 'Max_part_supplied' which consists of two column as
Number_of_parts and Supplier_name.
I am trying to access the Supplier_name who supplies maximum number of parts, as
select MAX(Number_of_parts) as max_part, Supplier_name
from Max_part_supplied;
This is the view Max_part_supplied
Number_of_parts Supplier_name
1 Ambani Traders
3 Lalu Traders
2 Paltu Traders
1 Sunil Traders
But I am getting error message as
Msg 8120,Level 16,State 1,line 84
Column Max_part_supplied.Supplier_name is invalid in the select list because it is not containted
in either aggregate function or the GROUP BY clause.
It looks like your view already has the max per supplier, you just want the maximum of these?
select top (1) Number_of_parts, Supplier_name
from Max_part_supplied
order by Number_of_parts desc
If you want all suppliers in the event two or more have the same max, use top (1) with ties

How should i write this Query [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 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].

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 Clarification Required [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 8 years ago.
Improve this question
I have a table consisting of the following columns:
billid, patientid, doctorid, fees
How do i display the doctors who treat more than one patient?
TRIED THE FOLLOWING CODE and got it.
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
Thanks :)
SELECT doctorID
FROM YourTable
GROUP BY doctorID
HAVING COUNT (DISTINCT patientid) > 1
These are basic SQL queries. If you have trouble with something like this, you should really get to some SQL tutorial or book first.
select doctorid, count(patientid) from table1 group by doctorid having COUNT (DISTINCT patientid) > 1 ;
This will show you the doctor list having more than 1 distinct patient
on the given information if you will just select doctorid who is treating more than one patient with this query
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
and then you can use that doctorid in rest of your operations

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