SQL Query to select top 2 for each value - sql

I have a table with 3 columns, the data in column1 has repeating values and column 3 has totals, what I'd like to do is to return the top 2 totals for each value in column 1.
My query to create this table is below:
SELECT service,name, total
FROM [test].[dbo].[TestTable]
join test1.dbo.service
on substring(servan,0,4)=servicebn
where substring(servan,0,4)=servicebn and name <> testname
group by service,name,total
order by service,total desc
any help would be much appreciated

if you are using SQL Server 2005+, you can use Common Table Expression and Window Function.
WITH recordsList
AS
(
SELECT service, name, total,
DENSE_RANK() OVER (PARTITION BY service
ORDER BY total DESC) rn
FROM [test].[dbo].[TestTable]
INNER join test1.dbo.servd
on substring(servan,0,4)=servicebn
where substring(servan,0,4) = servicebn and
name <> testname
)
SELECT service, name, total
FROM recordsLIst
WHERE rn <= 2
As a side note, this query has poor in performance because it requires FULL TABLE SCAN on every table. The reason is because of the join condition substring(servan,0,4)=servicebn. It doesn't use index.

Related

SQL Server, group by , aggregate into single result

I have an SQL query that returns the data into multiple records (the first 4 records).
How can I combine the 4 rows above into one single record result?
The product where Type is null has only V1, V1_Status, V2 and V2_Status
In my query I already do:
SELECT Product, Max(Type), Max(V1), Max(V1_Status), Max(V2), Max(V2_Status), Max(V3), Max(V3_Status), Max(V4), Max(V4_Status), Max(V5), Max(V5_Status)
FROM Table
WHERE condition
GROUP BY Product, Type
You just need to not GROUP BY Type i.e.
SELECT Product,
Max(V1),
Max(V1_Status),
Max(V2),
Max(V2_Status),
Max(V3),
Max(V3_Status),
Max(V4),
Max(V4_Status),
Max(V5),
Max(V5_Status)
FROM Table
WHERE condition
GROUP BY Product
use row_number()
select * from
(
select t.*,row_number()over(partition by product order by v1 desc) rn
from table_name t
) a where a.rn=1

SQL Oracle Find Max of count

I have this table called item:
| PERSON_id | ITEM_id |
|------------------|----------------|
|------CP2---------|-----A03--------|
|------CP2---------|-----A02--------|
|------HB3---------|-----A02--------|
|------BW4---------|-----A01--------|
I need an SQL statement that would output the person with the most Items. Not really sure where to start either.
I advice you to use inner query for this purpose. the inner query is going to include group by and order by statement. and outer query will select the first statement which has the most items.
SELECT * FROM
(
SELECT PERSON_ID, COUNT(*) FROM TABLE1
GROUP BY PERSON_ID
ORDER BY 2 DESC
)
WHERE ROWNUM = 1
here is the fiddler link : http://sqlfiddle.com/#!4/4c4228/5
Locating the maximum of an aggregated column requires more than a single calculation, so here you can use a "common table expression" (cte) to hold the result and then re-use that result in a where clause:
with cte as (
select
person_id
, count(item_id) count_items
from mytable
group by
person_id
)
select
*
from cte
where count_items = (select max(count_items) from cte)
Note, if more than one person shares the same maximum count; more than one row will be returned bu this query.

SQL Server - Update statement with max and group by

I have a SQL Server table with columns like this but with only the Salary_ID column populated:
Name: Salary
Salary_ID, TotalAmount
I have another SQL Server table with this format and all the columns populated:
Name: SalaryImport
Row_ID, Salary_ID, Amount
I am trying to update the Salary table with the maximum row_id for each salary_id. Currently, there are many instances of each Salary_ID in SalaryImport and I only care about the most recent one which is indicated by the higher number row_id.
Any tips on how this can be done? I thought I would run a SQL statement like but it is grouping too much (and I can remove amount from the group because it is in the select!):
select max(Row_ID), Salary_ID, Amount
from SalaryImport e
group by Row_ID, Amount
Use ROW_NUMBER window function
;with cte as
(
select row_number() over(partition by Salary_ID order by Row_ID desc) as rn,*
From yourtable
)
Update S
Set TotalAmount = c.Amount
From Salary s join cte c on s.Salary_ID = c.Salary_ID
Where RN = 1

Adding count in select query

I am trying to find a query that would give me a count of another table in the query. The problem is that I have no idea what to set where in the count part to. As it is now it will just give back a count of all the values in that table.
Select
ID as Num,
(select Count(*) from TASK where ID=ID(Also tried Num)) as Total
from ORDER
The goal is to have a result that reads like
Num Total
_________________
1 13
2 5
3 22
You need table aliases. So I think you want:
Select ID as Num,
(select Count(*) from TASK t where t.ID = o.ID) as Total
from ORDER o;
By the way, ORDER is a terrible name for a table because it is a reserved work in SQL.
You can do it as a sub query or a join (or an OVER statement.)
I think the join is clearest when you are first learning SQL
Select
ID as Num, count(TASK.ID) AS Total
from ORDER
left join TASK ON ORDER.ID=TASK.ID
GROUP BY ORDER.ID

One row of data for a max date only - transact SQL

I am trying to select the max dates on a field with other tables, to only give me one distinct row for the max date and not other rows with other dates. the code i have for max is
SELECT DISTINCT
Cust.CustId,
LastDate=(Select Max(Convert(Date,TreatmentFieldHstry.TreatmentDateTime))
FROM TreatmentFieldHstry
WHERE Cust.CustSer = Course.CustSer
AND Course.CourseSer = Session.CourseSer
AND Session.SessionSer = TreatmentFieldHstry.SessionSer)
This gives multiple rows depending on how many dates - i just want one for the max - can anyone help with this?
Thanks
You didn't specify exactly what database and version you're using - but if you're on SQL Server 2005 or newer, you can use something like this (a CTE with the ROW_NUMBER ranking function) - I've simplified it a bit, since I don't know what those other tables are that you have in your select, that don't ever show up in any of the SELECT column lists.....
;WITH TopData AS
(
SELECT c.CustId, t.TreatmentDateTime,
ROW_NUMBER() OVER(PARTITION BY c.CustId ORDER BY t.TreatmentDateTime DESC) AS 'RowNum'
FROM
dbo.TreatmentFieldHstry t
INNER JOIN
dbo.Customer c ON c.CustId = t.CustId -- or whatever JOIN condition you have
WHERE
c.CustSer = Course.CustSer
)
SELECT
*
FROM
TopData
WHERE
RowNum = 1
Basically, the CTE (Common Table Expression) partitions your data by CustId and order by TreatmentDateTime (descending - newest first) - and numbers every entry with a consecutive number - for each "partition" (e.g. for each new value of CustId). With this, the newest entry for each customer has RowNum = 1 which is what I use to select it from that CTE.