Display only maximum value in query with column meno - SQL - sql

As mentioned in the title I want to display only row with maximum number, for this case it´s number 4 and nothing less.
select divak.MENO,count(divak.MENO)
from sledovanost
natural join tv_stanice
natural join divak
group by divak.meno
order by count(divak.MENO)desc;
My query

You can achieve this by a having-clause, in which you compare each MENO count with the maximum MENO count (retrieved by a subquery):
select divak.MENO,count(divak.MENO)
from sledovanost
natural join tv_stanice
natural join divak
group by divak.meno
having count(divak.MENO) = (
select (max(count(divak.MENO))
from sledovanost
natural join tv_stanice
natural join divak
group by divak.meno)
BTW: I'd change the query to join .. ON .. syntax; natural join, which connects tables on equally named attributes, bears the danger of "unintended connections" whenever the db schema changes.

Don't use natural join. It uses the names of the columns to match tables, and doesn't show the names in the query. It is a bug waiting to happen. Use USING or ON.
In Oracle, you would normally do this using row_number() or rank() depending on whether or not you wanted duplicates:
with t as (<your query here but name the second column>)
select t.*
from (select t.*, rank() over (order by cnt desc) as seqnum
from t
) t
where seqnum = 1;

Related

Can we select first row of data from column in sql?

I have a table with multiple data for same ID. I want to get the first row data for the ID.
I have added the below SQL that I have tried.
SELECT
"client"."id",
"client"."company_name",
"client_details"."address"
from Client
LEFT OUTER JOIN "client_details" ON ("client"."id" = "client_details"."client_id")
Since I have multiple address for the same ID, can we get only the first id?
Currently the output I get is 2 rows with different addresses.
You can add to your SQL LIMIT 1 and in case you want to be sure the order you can also add to your SQL ORDER BY...
You can use distinct on:
select distinct on (c.id) c.id, c.company_name, cd.address
from Client c left join
client_details cd
on c.id = cd.client_id
order by c.id, ?;
The ? is for the column that specifies the ordering (the definition of "first"). I am guessing that cd.id is what you want.
Note that this query removes the double quotes and introduces table aliases. This is easier on both the eyes (to read) and the fingers (to type).
use row_number()
select * from
(
SELECT
"client"."id",
"client"."company_name",
"client_details"."address",row_number() over(partition by "client"."id" order by "client_details"."address") as rn
from Client
LEFT OUTER JOIN "client_details" ON "client"."id" = "client_details"."client_id"
)A where rn=1
If there is a field you can order the results by you could use a lateral join e.g.
SELECT
"client"."id",
"client"."company_name",
"client_details"."address"
from Client
left join lateral (
select *
from client_details cd
where cd.client_id = client.id
order by [some_ordering_field]
limit 1
) "client_details" on true

Finding max - SQL

I needed to print ( show ) the max of the table created by the following code:
SELECT name, SUM(cost)+SUM(DISTINCT stock*cost) AS result
FROM publishers
NATURAL JOIN editions
NATURAL JOIN shipments
NATURAL JOIN stock
GROUP BY name
I have tried using DESC but am not allowed to use it
SELECT MAX(result) from (SELECT name, SUM(cost)+SUM(DISTINCT stock*cost) AS result FROM publishers NATURAL JOIN editions NATURAL JOIN shipments NATURAL JOIN stock group by name )
This nested query should do if you only need max of result.

Subquery in join can't reach object outside of the subquery

The following query selects a students names and their highest score for a particular row. The problem is that the subquery in the left join can't be used.
I get the Teradata data error: "Object Date_View doesn't exist'". How can this be fixed?
Select name, max_score_today.max_score From Student_View
Left Join Date_view
ON Date_View.date=Student_View.date
Left Join (
Select MAX(score) as max_score FROM
Score_View
Where Date_View.start_date=Score_View.date
) max_score_today
ON max_score_today.name=Student_View.name
Move your correlated subquery up into the select statement, such as:
select name,
(select max(score) from score_view where date_view.start_date) = score_view.date) as max_score
from student_view
left join
date_view
on date_view.date = student_view.date;
To get the row with the highest/lowest value you better use RANK or ROW_NUMBER like this:
Select *
From Student_View
Left Join Date_view
on Date_View.date=Student_View.date
Left Join Score_View
on Date_View.start_date=Score_View.date
and Score_View.name=Student_View.name
QUALIFY -- get the highest score for each student/date
ROW_NUMBER() -- maybe RANK
OVER (PARTITION BY Student_View.name, Score_View.date
ORDER BY Score_View.score DESC) = 1
I'm not sure if this the correct result, you might have to change the PARTITION expression...

How to find the most frequent value in a select statement as a subquery?

I am trying to get the most frequent Zip_Code for the Location ID from table B. Table A(transaction) has one A.zip_code per Transaction but table B(Location) has multiple Zip_code for one area or City. I am trying to get the most frequent B.Zip_Code for the Account using Location_D that is present in both table.I have simplified my code and changed the names of the columns for easy understanding but this is the logic for my query I have so far.Any help would be appreciated. Thanks in advance.
Select
A.Account_Number,
A.Utility_Type,
A.Sum(usage),
A.Sum(Cost),
A.Zip_Code,
( select B.zip_Code from B where A.Location_ID= B.Location_ID having count(*)= max(count(B.Zip_Code)) as Location_Zip_Code,
A.Transaction_Date
From
Transaction_Table as A Left Join
Location Table as B On A.Location_ID= B.Location_ID
Group By
A.Account_Number,
A.Utility_Type,
A.Zip_Code,
A.Transaction_Date
This is what I come up with:
Select tt.Account_Number, tt.Utility_Type, Sum(tt.usage), Sum(tt.Cost),
tt.Zip_Code,
(select TOP 1 l.zip_Code
Location_Table l
where tt.Location_ID = l.Location_ID
group by l.zip_code
order by count(*) desc
) as Location_Zip_Code,
tt.Transaction_Date
From Transaction_Table tt
Group By tt.Account_Number, tt.Utility_Type, tt.Zip_Code, tt.Transaction_Date;
Notes:
Table aliases are a good thing. However, they should be abbreviations for the tables referenced, rather than arbitrary letters.
The table alias qualifies the column name, not the function. Hence sum(tt.usage) rather than tt.sum(usage).
There is no need for a join in the outer query. You are doing all the work in the subquery.
An order by with top seems the way to go to get the most common zip code (which, incidentally, is called the mode in statistics).

GROUP BY not working in left join query

I m trying to use group by clause in left join sql query and it is not working.
Please help me out, thanks in advance.
SELECT Cust_Mst_Det.Cust_Hd_Code,
Cust_Mst_Det.First_Name,
SL_HEAD20152016.vouch_date AS invoice_2,
SL_HEAD20142015.vouch_date AS invoice_1,
Cust_Mst_Hd.EMail
FROM Cust_Mst_Det
LEFT JOIN SL_HEAD20142015 ON Cust_Mst_Det.Cust_Hd_Code=SL_HEAD20142015.Member_Code
LEFT JOIN SL_HEAD20152016 ON Cust_Mst_Det.Cust_Hd_Code=SL_HEAD20152016.Member_Code
LEFT JOIN Cust_Mst_Hd ON Cust_Mst_Det.Cust_Hd_Code=Cust_Mst_Hd.Cust_Hd_Code
WHERE cust_mst_det.first_name!='NIL'
GROUP BY Cust_Mst_Det.Cust_Hd_Code
ORDER BY SL_HEAD20152016.vouch_date DESC,
SL_HEAD20142015.vouch_date
I'm not sure which DBMS you are using, but on an Oracle your query will not work at all.
First issue: The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. You do not have any aggregating function in your SELECT statement (count, max, etc.)
Second issue: you must specify all columns from SELECT statement in your GROUP BY statement (excluding columns that represents results of aggregation).
As I said I don't know which DB is used by you, but those two points should be applicable for the most of SQL standards.
It appears that it is impossible to use an ORDER BY on a GROUP BY summarisation. My fundamental logic is flawed. I will need to run the following subquery.
ex :
SELECT p.*, pp.price
FROM products p
LEFT JOIN ( SELECT price FROM product_price ORDER BY date_updated DESC ) pp
ON p.product_id = pp.product_id GROUP BY p.product_id;
This will take a performance hit but as it is the same subquery for each row it shouldn't be too bad.