First record in SQL statement - sql

I need to link and additional table to filter by the plant in doing so create multiple records for each entry. I am looking to just get the first entry for one.
SELECT
OrderHead.CreditOverride,
OrderHead.OpenOrder, Customer.CreditHold,
OrderHead.OrderNum, Customer.Name,
OrderHead.EntryPerson, OrderHead.OrderDate,
Customer.TermsCode, OrderHead.ShipToCustNum, OrderRel.Plant
FROM
Customer
INNER JOIN
OrderHead ON Customer.Company = OrderHead.Company
AND Customer.CustNum = OrderHead.BTCustNum
INNER JOIN
OrderRel ON OrderHead.OrderNum = OrderRel.OrderNum
WHERE
(OrderHead.CreditOverride = 0)
AND (OrderHead.OpenOrder = 1)
AND (Customer.CreditHold = 1)
AND (OrderRel.Plant = 'mfgsys')
Trying to grab the first unique record from orderhead.

Use window functions. You can do this entirely in the FROM clause:
FROM Customer c INNER JOIN
(SELECT oh.*,
ROW_NUMBER() OVER (PARTITION BY Company, BTCustNum ORDER BY OrderDate ASC) as seqnum
FROM OrderHead oh
) oh
ON c.Company = oh.Company AND
c.CustNum = oh.BTCustNum AND
oh.seqnum = 1 INNER JOIN
OrderRel orr
ON oh.OrderNum = orr.OrderNum
Note that I replaced the table names with simpler table aliases, which you should repeat in the rest of the query.

Related

Oracle SQL - how to NOT SHOW athlete name that apears only once

created a view called winners, it contains the columns: athlete_name,year,medal_won
its basicly athletes that won olympic medal and the year,
it look like that,
data base is in live sql: https://livesql.oracle.com/apex/f?p=590:1000:0
select distinct year,athlete_name,medal
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
order by athlete_name
as you can see some name show only once and some names are showing more than once, i want to get rid off all lines of those who show ONLY ONCE, please help me.
thank you!
if i have understand your problem, must group your data,
select year,athlete_name,medal, count(*) "number of Medals"
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
group by year,athlete_name,medal;
If I followed you correctly, you can use window functions:
select *
from (
select og.year, oa.athlete_name, om.medal, count(*) over(partition by oa.id) cnt
from olym.olym_medals om
join olym.olym_athlete_games oag on oag.id = om.athlete_game_id
join olym.olym_nations ona on ona.id = oag.nation_id
join olym.olym_games og on og.id = oag.game_id
join olym.olym_athletes oa on oa.id = oag.athlete_id
) t
where cnt > 1
order by athlete_name
Notes:
I am unsure why you were using distinct in the first place, so I removed it (I suspect it is actually not needed)
I added table aliases to shorten the query, and prefixed the columns in the select clause with the table they belong to (you might want to review that) - these are best practices when dealing with multi-table queries
Use GROUP BY and HAVING COUNT(*) > 1:
SELECT year,
athlete_name,
medal
FROM olym.olym_medals
INNER JOIN olym.olym_athlete_games
ON olym_athlete_games.id = olym_medals.athlete_game_id
INNER JOIN olym.olym_nations
ON olym_nations.id = olym_athlete_games.nation_id
INNER JOIN olym.olym_games
ON olym_games.id = Olym_athlete_games.game_id
INNER JOIN olym.olym_athletes
ON olym_athletes.id = olym_athlete_games.athlete_id
GROUP BY
year,
athlete_name,
medal
HAVING COUNT(*) > 1
ORDER BY athlete_name

SELECT statement where rows are omitted based on another table

Table with orders has another table with positions. I want the orders table to show but then only have the most up to-date position on it. Below is a picture of the 3 rows I want showing. Omit the rest.
SELECT DispatchTable.ordernumber, DispatchTable.truck,
DispatchTable.driver, DispatchTable.actualpickup,
DispatchTable.actualdropoff, orders.pickupdateandtime,
orders.dropoffdateandtime, Truck002.lastposition,
Truck002.lastdateandtime
FROM DispatchTable
INNER JOIN orders ON DispatchTable.ordernumber = orders.id
INNER JOIN Truck002 ON DispatchTable.truck = Truck002.name
WHERE (orders.status = 'onRoute')
Assuming that you want the row having the latest lastdateandtime for the truck name, this should work:
SELECT DispatchTable.ordernumber,
DispatchTable.truck,
DispatchTable.driver,
DispatchTable.actualpickup,
DispatchTable.actualdropoff,
orders.pickupdateandtime,
orders.dropoffdateandtime,
TruckLatest.lastposition,
TruckLatest.lastdateandtime
FROM DispatchTable
INNER JOIN orders ON DispatchTable.ordernumber = orders.id
INNER JOIN (SELECT name,
lastposition,
lastdateandtime
FROM Truck002 Truck1
WHERE lastdateandtime =
(SELECT MAX(lastdateandtime)
FROM Truck002 Truck2
WHERE Truck2.name = Truck1.name)) TruckLatest
ON DispatchTable.truck = TruckLatest.name
WHERE (orders.status = 'onRoute')
If I understand correctly, you can get the most recent record for a truck using ROW_NUMBER():
SELECT dt.ordernumber, dt.truck,
dt.driver, dt.actualpickup,
dt.actualdropoff, o.pickupdateandtime,
o.dropoffdateandtime, t.lastposition,
t.lastdateandtime
FROM DispatchTable dt INNER JOIN
orders o
ON dt.ordernumber = o.id INNER JOIN
(SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY t.name ORDER BY t.lastdateandtime DESC) as seqnum
FROM Truck002 t
) t
ON dt.truck = t.name
WHERE o.status = 'onRoute' AND seqnum = 1;
Firstly, why are you using Truck002's name field rather than its id field as the link to DispacthTable? This is considered a less efficient way of doing it than using id (which is either a numerical field or a shorter string than name).
Secondly, you should mention in your Question that each Order can have many DispatchTable's and that each DispacthTable can have many Truck002's, otherwise many people will start by assuming that it is the other way round between DispatchTable and Truck002.
Thirdly, please try...
SELECT DispatchTable.ordernumber,
DispatchTable.truck,
DispatchTable.driver,
DispatchTable.actualpickup,
DispatchTable.actualdropoff,
orders.pickupdateandtime,
orders.dropoffdateandtime,
Truck002.lastposition,
Truck002.lastdateandtime
FROM DispatchTable
INNER JOIN orders ON DispatchTable.ordernumber = orders.id
INNER JOIN Truck002 ON DispatchTable.truck = Truck002.name
WHERE (orders.status = 'onRoute')
GROUP BY ordernumber
HAVING lastdateandtime = MAX( lastdateandtime )
If you have any questions or comments, then please feel free to post a Comment accordingly.
Further Reading
https://msdn.microsoft.com/en-us/library/bb177906(v=office.12).aspx (on HAVING)
https://www.w3schools.com/sql/sql_having.asp (on HAVING)
https://msdn.microsoft.com/en-us/library/bb177905(v=office.12).aspx (on GROUP BY)
https://www.w3schools.com/sql/sql_groupby.asp (on GROUP BY)

How can i select the most recent purchase by the Clients?

In a database with several tables and relationships between them, I want to select only the most recent payment by the clients last year.
My query goes like this:
SELECT
P.Name,
EV.EventName,
FN.Installments,
FN.PurchaseValue,
FN.DueDate
FROM ClientPrivate PF
JOIN Client P ON P.PesControle = PF.PesControle
JOIN ClientClass CP ON P.PesControle = CP.PesControle
JOIN EVENT EV ON CP.EveControle = EV.EveControle
JOIN Class cc ON cc.CurControle = EV.CurControle
JOIN Finance FN ON FN.PesControle = P.PesControle
It returns the values I need, only I'd like to get only the most recent purchase by each client, instead of all of them.
I edited to help clarify. The 'Controle' columns are the keys.
Whatever you date column is put that in the ORDER BY Clause of the ROW_NUMBER() function and you are good to go.
;WITH CTE AS
(
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto,
ROW_NUMBER() OVER (PARTITION BY P.PesNome ORDER BY [DateColumn] DESC) rn
FROM PessoaFisica PF
JOIN Pessoa P ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP ON P.PesControle = CP.PesControle
JOIN EVENTO EV ON CP.EveControle = EV.EveControle
JOIN Curso cc ON cc.CurControle = EV.CurControle
JOIN Financeiro FN ON FN.PesControle = P.PesControle
)
SELECT *
FROM CTE
WHERE rn = 1
I'm guessing Financeiro.FinDiaVencto is your purchase date, and that Pessoa.PesControle is a unique identifier for a person. If not, you'll need to modify the query. I'm using Google to translate Portuguese, and it's still not clear to me.
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto
FROM PessoaFisica PF
JOIN Pessoa P
ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP
ON P.PesControle = CP.PesControle
JOIN EVENTO EV
ON CP.EveControle = EV.EveControle
JOIN Curso cc
ON cc.CurControle = EV.CurControle
JOIN Financeiro FN
on FN.PesControle = P.PesControle
WHERE EXISTS (
SELECT 1
FROM PessoaFisica PF_s
JOIN Pessoa P_s
ON P_s.PesControle = PF_s.PesControle
JOIN CursoPessoa CP_s
ON P_s.PesControle = CP_s.PesControle
JOIN EVENTO EV_s
ON CP_s.EveControle = EV_s.EveControle
JOIN Curso cc_s
ON cc_s.CurControle = EV_s.CurControle
JOIN Financeiro FN_s
on FN_s.PesControle = P_s.PesControle
WHERE P_s.PesControle = P.PesControle
HAVING MAX(FN_s.FinDiaVencto) = FN.FinDiaVencto
)
That should return the record with the most recent Financeiro.FinDiaVencto for each Pessoa.PesControle. If you need it by course or by event(?) you'll need to modify the WHERE clause to join on those fields, too.
I'm not sure if that join to PessoaFisica is necessary either, but I included it because it could be eliminating records. You can include a GROUP BY in the subquery, but it should be redundant.

Order BY on column generated by select statement

i'm running this query:
SELECT
claims.rID
,claims.client
,clients.cName
,cur.currency
,carriers.scac
,carriers.cName
,claims.client+claims.counter
,claims.dateOn
,(SELECT top 1 errorCode FROM entries WHERE entries.rID=claims.rID) as errorCode
,(SELECT SUM(refundDue) FROM entries WHERE entries.rID=claims.rID) as amount
,auditors.initials
FROM claims
INNER JOIN clients ON clients.code = claims.client
INNER JOIN currency cur ON claims.currency = cur.currencyID
INNER JOIN entries ON claims.rID = entries.rid
INNER JOIN carriers ON carriers.carrierID = claims.carrierID
INNER JOIN auditors ON claims.auditorID=auditors.auditorID
GROUP BY
claims.rID
,claims.client
,claims.counter
,claims.dateOn
,carriers.scac
,carriers.cName
,clients.cName
,cur.currency
,auditors.initials
,errorCode
ORDER BY errorCode ASC
The focus should be on the orderBy errorCode. For some reason, it's not actually ordering Alphabetically by errorCode. Any ideas as to why?
Most databases don't allow you to use aliases created in the SELECT-list in the GROUP BY, ORDER BY and WHERE clauses. Instead, you have to repeat the expression used in the SELECT-list:
SELECT
claims.rID,
...,
(SELECT top 1 errorCode FROM entries WHERE entries.rID=claims.rID) AS errorCode,
...
FROM
claims INNER JOIN ...
GROUP BY
claims.rID,
...,
(SELECT top 1 errorCode FROM entries WHERE entries.rID=claims.rID)
ORDER BY
(SELECT top 1 errorCode FROM entries WHERE entries.rID=claims.rID) ASC
But I think that is better to use a sub-select as data source in the FROM-clause:
SELECT
claims.rID,
...,
E.errCode, E.amount,
...
FROM claims
INNER JOIN clients ON clients.code = claims.client
INNER JOIN currency cur ON claims.currency = cur.currencyID
INNER JOIN (SELECT rID, MIN(errorCode) AS errCode, SUM(refundDue) AS amount
FROM entries
GROUP BY rID) AS E
ON claims.rID = E.rid
INNER JOIN carriers ON carriers.carrierID = claims.carrierID
INNER JOIN auditors ON claims.auditorID=auditors.auditorID
GROUP BY
claims.rID,
...,
E.errCode
ORDER BY
E.errCode ASC
Note that I included the SUM(refundDue) AS amount as well. If the error codes can be NULL within entries, use MAX(errorCode) instead of MIN(errorCode).

Joining top records in T-SQL

SELECT MD.*, Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
JOIN (SELECT TOP 1 * FROM Location WHERE Location.BusID = MD.BusID) L ON L.BusID=MD.BusID
AND L.Deleted = 0
JOIN Contact ON Contact.ContactID = L.PrincipalID
I am using SQLSERVER 2008 and trying to write this SQL statement. There is some times multiple locations for a busid and I want to join in only the first found. I am getting an error on the part "Location.BusID = MD.BusID" as MD.BusID cannot be bound. Is it possible to use the MD table in the nested select statment in this join or is there another way of accomplishing this?
I am contiplating putting the data using nested querys in the column list to grab the contact data driectly there.
It would be simpler I think to have a subquery of the full result set:
SELECT MD.*, Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
JOIN (SELECT BusID, MAX(PrincipalID)
FROM Location
WHERE Deleted = 0
GROUP BY BusID) L ON L.BusID=MD.BusID
JOIN Contact ON Contact.ContactID = L.PrincipalID
You still get one record per BusID in the JOIN but it's not correlated.
SELECT MD.*, Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
CROSS APPLY (SELECT TOP 1 * FROM Location WHERE BusID = MD.BusID AND DELETED = 0) L
JOIN Contact ON Contact.ContactID = L.PrincipalID
This is a case of the "top n per group" problem. This question will guide you:
SQL Server query select 1 from each sub-group
You'll want to be doing something like this:
SELECT MD.* ,
Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
JOIN ( select * ,
seq = rank() over( partition by BusID order by BusID , ... )
from Location
where L.Deleted = 0
) L on L.BusID = MD.BusID
and seq = 1
JOIN Contact ON Contact.ContactID = L.PrincipalID
The virtual table expression should return at most 1 Location per BusID (0 if the BusID has no non-deleted Locations).
To try and isolate out the error I would try. See if it can match Location.BusID = MD.BusID.
SELECT MD.*, Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
JOIN Location On Location.BusID = MD.BusID
You do not use the * so use
SELECT TOP 1 Location.BusID FROM Location WHERE Location.BusID = MD.BusID
Once you get the syntax working.
You do know that once you get this working it will only match if the "first" row and then check if it is deleted. The problem is that without an order by the "first" row is arbitrary. Even with a clustered index on a table there is no guaranteed sort without an order by clause. To get a repeatable answer you need a sort. But if you are sorting and only want the top row then a MAX or MIN and a group be more straight forward.
If you want just business that have one or more deleted locations then the following should work but you need to break out the columns for the group by. If two deleted locations have differenct contact name then it will report each. So, this may not be what you are looking for.
SELECT MD.col1, MD.col2, Contact.FirstName
FROM MerchantData MD
JOIN Merchant M ON M.MerchID = MD.MerchID
JOIN Location L
ON L.BusID = MD.BusID
AND L.Deleted = 0
JOIN Contact ON Contact.ContactID = L.PrincipalID
GROUP BY MD.col1, MD.col2, Contact.FirstName