SELECT a variable as condition and display another one - sql

My problem is the following.
I want to SELECT the minimum of a years list and display another row of my table.
An example:
SELECT MIN(Year)
FROM table -> Searching for the lowest year.
and then I want it to display the Winners of the first year.
Is there a way to do this in just one line?

select winner from table where year in (select min(year) from table)

You need to do a self-JOIN between table and itself (I suppose you want to do it in a single statement, not a single line):
SELECT A.*
FROM table AS A
JOIN ( SELECT MIN(Year) AS Year FROM table ) AS B
ON (A.Year = B.Year);
This assumes that there is only one record per minimum-year in every group of interest.

Try it
select t.columntoshow, min(t.year) from table t group by t.columntoshow

Related

SQL : Find Numbers of Rows in a Table according to Criteria

I want to get numbers of rows in a table according to certain criteria.
Please see the below table:-
Herein I want to get numbers of rows according to Column StationTo.
I want to get numbers of rows of each StationTo entries.
You could group by the StationTo and use the aggregate count(*) function:
SELECT StationTo, COUNT(*)
FROM mytable
GROUP BY StationTo
EDIT:
If you just want the number of rows for a single StationTo, you could use a where clause:
SELECT COUNT(*)
FROM mytable
WHERE StationTo = 'P11004400000'
Hi have you master table for stationTo records?
select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count.
or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'

How to include missing rows in sql return

I am currently trying to do a query like this:
(Psuedocode)
SELECT
NAME, SUM(VALUE), MONTH
FROM TABLE
WHERE MONTH BETWEEN 12 MONTHS AGO AND NOW
GROUP BY MONTH, NAME
The problem I am getting is that a name exists in a few of the months, but not all of the months, so if i filter this down to return the values for only one name, i sometimes get only 3 or 4 rows, rather than the 12 I expect to see.
My question is, is there a way to return rows, where it will still include the name, and month within the range, where the value would just be set to zero when I am missing the row from the previous result.
My first thought was to just union another select onto it, but I cant seem to get the logic to work to adhere to the group by, as well as the where clauses for limiting the names.
I you have data for all months, you can take the following approach. Generate all the rows (uses a cross join) then bring in the data you want:
select m.month, n.name, sum(t.value)
from (select distinct month from table) m cross join
(select distinct name from table) n left join
table t
on t.month = m.month and t.name = n.name
group by m.month, n.name;
This will return the missing sums as NULL values. If you want zero, then use coalesce(sum(t.value), 0).
you can use something like the following table to generate all the past 12 months as separate rows:
SELECT add_months(trunc(add_months(sysdate, -12), 'MONTH'), LEVEL - 1) AS month_in_range
FROM all_objects
CONNECT BY LEVEL <= 1 + months_between(add_months(sysdate, -12), TRUNC (sysdate, 'MONTH'));
and then do an outer join between you table and this.
I ended up implementing a left outer join similar to #paqogomez 's comment. As my team is already maintaining a time table, its very easy to get the month list for an outer join.
SELECT NAME, SUM(VALUE), TIME.MONTH
FROM (SELECT DISTINCT MONTH FROM TIME_TABLE
WHERE MONTH BETWEEN 12 MONTHS AGO AND NOW) TIME
LEFT OUTER JOIN TABLE ON (TIME.MONTH = TABLE.MONTH)
GROUP BY TIME.MONTH, NAME

Several MAX values based on another column value

I'm trying to write a SQL query in MS ACCESS and I've narrowed it down to the table below, but can't seem to get the last thing right without making several extremely large querys.
Here's the strucuture of thetable I'm trying to query:
The results I want: MemberId and year where memberId had most visits in that year.(That is which memberId had most visits 2014, which had most visits 2015 etc..and I also want the relevant year to be shown in the result)
Thanks!
Sounds like you need to determine MAX(Visits) by year in a subquery, then JOIN to that:
SELECT a.*,b.Max_Visits
FROM YourTable a
JOIN (SELECT Year,MAX(Visits) AS Max_Visits
FROM YourTable
GROUP BY Year
) b
ON a.Year = b.Year
AND a.Visits = b.Max_Visits
If you want to see all members and not just those that had the most visits per year, you can change from JOIN to LEFT JOIN
If there's a tie, this returns both members.

Group by one column and select more than one column Sql query

I need to group by more than one columns but in special case:
I have a table of (Payment_Type,Year,TotalMoney)
I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:
select Payment_Type,Year,SUM(TotalMoney)
from table
where 1=1
group by Payment_Type,Year
I get an error message as:
Year is not contained in either an aggregate function or the GROUP BY clause
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney)
from table
group by Payment_Type,Year(YourDateColumn)
if your column is named year then
select Payment_Type,[Year],SUM(TotalMoney)
from table
group by Payment_Type,[Year]

Group by like row filtering

Lets say I have a table describing cars with make, model, year and some other columns.
From that, I would like to get one full row of each make and model for latest year.
Or to put it in another way. Each row would have unique combination of make and model with other data from corresponding row with largest value of year.
Standard SQL solution would be great.
select t1.make, t1.model, t1.year, t1.other_cols
from table t1
where year = (select max(year) from table t2
where t2.make = t1.make
and t2.model = t1.model
);
MySQL solution:
SELECT * FROM cars GROUP NY CONCAT(make, "-", model) ORDER BY year DESC;
This should work most anywhere:
SELECT c1.*
FROM cars c1
INNER JOIN
(
SELECT Make, Model, Max(Year) AS Year
FROM cars
GROUP BY Make, Model
) c2 ON c1.Make=c2.Make AND c1.Model=c2.Model, c1.Year=c2.Year
The main caveat is that Year is often a reserved word (function name) and the means for escaping reserved words vary by platorm. To fix that, rename the year column to something like ModelYear.