Write an Sql query to display the top selling model of each manufacturer for each year [closed] - sql

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 2 years ago.
Improve this question
Given below is the table of Car sales (of all models from different manufacturers) . ( only few rows are shown)
Date Name Model Units_sold
Dec-2020 Audi A4 300
Dec-2020 BMW 3Series 164
Jan-2020 Maruti Alto 33118
Jan-2020 Toyota Fortuner 1103
Feb-2020 skoda Superb 391

On SQL Server we can try:
SELECT TOP 1 WITH TIES Date, Name, Model, Units_sold
FROM yourTable
ORDER BY
ROW_NUMBER() OVER (PARTITION BY YEAR(Date), Name ORDER BY Units_sold DESC);

Related

Aggregation depends from value [closed]

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 1 year ago.
Improve this question
I have a sample table
ID
Invoice_ID
docType
1
100
email
2
100
sms
3
200
email
4
200
email
5
300
sms
and I have to get results with only rows that have invoices with email and sms docType. For this example its only invoice_id 100
One approach is to query those type and count the distinct number of different types that appear:
SELECT invoice_id
FROM invoices
WHERE doctype IN ('sms', 'email')
GROUP BY invoice_id
HAVING COUNT(DISTINCT doctype) = 2
One method uses aggregation:
select invoice_id
from t
where docType in ('email', 'sms')
group by invoice_id
having count(distinct doctype) = 2;

Query to find man of the tournament [closed]

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 6 years ago.
Improve this question
I have a table which has the below data:
PLAYER RUN
-----------------
Kohli 100
Kohli 200
Sachin 200
Yuvi 300
Yuvi 300
Yuvi 300
I need to return the Player's name who scored the maximum number of runs.
How can I achieve this by a SQL query ? In this case the query should return "Yuvi" .
Could you please use the query given below
SELECT TOP 1 PLAYER, MAX(RUN) AS MAXRUN FROM <TABLE> GROUP BY PLAYER ORDER BY MAXRUN DESC;
select player from (select player , sum(runs) from test group by player,runs order by sum(sal) desc) where rownum=1;
You should use Max function in the column RUN. The query will be,
Select PLAYER, MAX(RUN) from TABLE;

Hive output query for selecting particular value based on groupby clause [closed]

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 6 years ago.
Improve this question
I have a table with data like:
cust_id, acct_no, ind
123111, 1233, Y
123111, 2311, N
222111, 1112, N
222111, 2111, N
I have to get output as cust_id, 1 (a binary indicator if any of the acct under that customer is Y)
so from the above table I have to get below output.
123111 1
222111 0
A simple way to achieve this is something like:
select cust_id, max(case when ind = 'Y' then 1 else 0 end) as flag from customers group by cust_id;

Assign values to sorted result in SQL [closed]

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 9 years ago.
Improve this question
I have got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo

Counting frequency of customer [closed]

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 8 years ago.
Improve this question
I have this table on sql sever
cstomer |No_Nota
CUS000 | 98342
CUS000 | 98343
CUS000 | 98343
CUS001 | 98355
CUS001 | 98355
I would like to count the frequency of each customer. For similar number of no_nota the value is 1.
I'd like a result like this:
cstomer |Frequent
CUS000 | 2
CUS001 | 1
You want the distinct count of the column no_nota, so that's what you should select...
select customer, count(distinct no_nota) as frequent
from my_table
group by customer
You want to count the result of your select query:
SELECT COUNT(expression)
FROM tables
WHERE predicates;
Example:
SELECT COUNT(No_Nota) FROM your_table WHERE No_Nota > 0;
Sounds like you just want a group by statement to get a count of all individual customers. Something like:
SELECT cstomer, SUM(1) as Frequent FROM table GROUP BY cstomer, No_Nota