I have this query in my Access database:
SELECT t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE, t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC;
It works but I need it to extract only the first record (with the last date). How can I do it?
Just replace your initial SELECT by SELECT TOP 1
SELECT TOP 1 t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE, t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC;
SELECT t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE,
t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC LIMIT 1;
You should use TOP/LIMIT/ROWNUM depends on your RDBM.
For more info: http://www.w3schools.com/sql/sql_top.asp
Related
Here is my query and I want to add the "count of SalID group by OFID" and store the result in the same table.
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
T_OF_User.SalID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
I tried this:
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
Count (SalID) FROM T_OF_User GROUP BY OFID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
But I have an error message. Any help please?
I think you want a window function:
SELECT T_OF.OFID, T_OF.OFDateDPrev, T_OF.OFDateFPrev, T_OF_User.OFUserID,
Count(SalID) OVER (PARTITION BY T_OF.OFID) as cnt
INTO T_tracing
FROM T_OF JOIN
T_OF_User
ON T_OF_User.OFID = T_OF.OFID;
You also need to give the result of the expression a name for T_Tracing.
I have been trying to solve an Oracle SQL query but to no avail and was hoping i could get some assistance. The scenario is that am querying the database and i get multiple records with the same value and wanted to combine the multiple rows into one.What i want to achieve is have the record for customer with IDNO=22099575 in one row instead of three as it appears in the attached screen shot of my result from my query below
SELECT concat(cu.firstname,cu.secondname)
Customername,cu.customerno,l.idnumber Idno,l.branch_code
Branchcode,l.phonenumber
Phone,cu.gender,l.grade,l.arocode,l.loanaccount,l.duedate,l.interest,
l.outstandingamount Outstandingloanbal,
l.lien Lienamount,TO_CHAR(l.applicationdate,'DD-MM-YY')
applicationdate,l.lastpaymentdate Lastcreditdate,l.inarrears
Principalloaninarrears,
l.rebate_amount Rebatepayable, l.empcode, l.disbursaldate, lt.description
Producttype,sum(l.amountdisbursed) Disbursedamt,
l.loanamount Principalamount,l.interest
Interestamount,l.flexi_refno,l.active
FROM ((ebank.tbloanaccount l
INNER JOIN ebank.tbcustomers cu ON l.customerno = cu.customerno)
INNER JOIN ebank.tbloantype lt ON l.productcode = lt.productcode)
where l.DISBURSED = '1'
group by concat(cu.firstname,cu.secondname), cu.customerno, l.idnumber,
l.branch_code, l.phonenumber,
cu.gender, l.grade, l.arocode, l.loanaccount,
l.duedate, l.interest, l.outstandingamount, l.lien,
TO_CHAR(l.applicationdate,'DD-MM-YY'),
l.lastpaymentdate, l.inarrears, l.rebate_amount, l.empcode, l.disbursaldate,
lt.description, l.loanamount, l.interest, l.flexi_refno, l.active order by
l.disbursaldate desc;
Here is a screen shot of my results from the above query:
you can use dense_rank on queries, to get latest due_date, outstanding loan.
SELECT concat(cu.firstname,cu.secondname)
Customername,cu.customerno,l.idnumber Idno,l.branch_code
Branchcode,l.phonenumber
Phone,cu.gender,l.grade,l.arocode,l.loanaccount, max(l.duedate) keep ( dense_rank first order by l.duedate desc ) duedate,l.interest,
max(l.outstandingamount) keep ( dense_rank first order by l.duedate desc ) Outstandingloanbal,
l.lien Lienamount,TO_CHAR(l.applicationdate,'DD-MM-YY')
applicationdate,l.lastpaymentdate Lastcreditdate,l.inarrears
Principalloaninarrears,
l.rebate_amount Rebatepayable, l.empcode, l.disbursaldate, lt.description
Producttype,sum(l.amountdisbursed) Disbursedamt,
l.loanamount Principalamount,l.interest
Interestamount,l.flexi_refno,l.active
FROM ((ebank.tbloanaccount l
INNER JOIN ebank.tbcustomers cu ON l.customerno = cu.customerno)
INNER JOIN ebank.tbloantype lt ON l.productcode = lt.productcode)
where l.DISBURSED = '1'
group by concat(cu.firstname,cu.secondname), cu.customerno, l.idnumber,
l.branch_code, l.phonenumber,
cu.gender, l.grade, l.arocode, l.loanaccount,
l.duedate, l.interest, l.outstandingamount, l.lien,
TO_CHAR(l.applicationdate,'DD-MM-YY'),
l.lastpaymentdate, l.inarrears, l.rebate_amount, l.empcode, l.disbursaldate,
lt.description, l.loanamount, l.interest, l.flexi_refno, l.active order by
l.disbursaldate desc;
I have the following SQL query which I want to use the value of public.account.phone_number for the LIKE clause of the sub query.
Unfortunately something is not working right since the query is returning 0 for total_responses when I replace '%public.account.phone_number' which a phone number in the database I get the correct value.
SELECT
public.email_account.email,
public.account.password,
public.ad.city,
public.ad.state,
public.ad.age,
public.ad.hotel,
public.ad.insert_time,
public.ad.ad_url,
public.ad.active,
public.ad.on_page,
public.ad.paid,
public.account.phone_number,
(SELECT COUNT(*) FROM public.match where match_id LIKE '%public.account.phone_number') AS total_responses
FROM
public.account
INNER JOIN public.ad
ON public.account.id = public.ad.account_id
INNER JOIN public.email_account
ON public.account.email_account_id = public.email_account.id
ORDER BY
ad.active ASC,
on_page DESC
I guess you want to do concatenation with the outer field phone_number.
Try this:
SELECT
public.email_account.email,
public.account.password,
public.ad.city,
public.ad.state,
public.ad.age,
public.ad.hotel,
public.ad.insert_time,
public.ad.ad_url,
public.ad.active,
public.ad.on_page,
public.ad.paid,
public.account.phone_number,
(SELECT COUNT(*) FROM public.match where match_id LIKE '%' || public.account.phone_number) AS total_responses
FROM
public.account
INNER JOIN public.ad
ON public.account.id = public.ad.account_id
INNER JOIN public.email_account
ON public.account.email_account_id = public.email_account.id
ORDER BY
ad.active ASC,
on_page DESC
This is my query:
select
Sales.SaleID,
Sales.StartSaleDate,
Sales.EndSaleDate,
Sales.SalePercent,
COUNT(LessonID) as TotalLesson,
Sales.Status,
Sales.ExpiredStatus,
Sales.SalePrice,
Sales.IsSpecial
FROM
Sales
LEFT JOIN
SaleLessons ON SaleLessons.SaleID = Sales.SaleID
GROUP BY
Sales.Status, Sales.IsSpecial, Sales.StartSaleDate, Sales.EndSaleDate,
Sales.SalePercent, Sales.SaleID, Sales.ExpiredStatus, Sales.SalePrice
ORDER BY
Sales.StartSaleDate DESC
create view ViewSchema.ViewName
as
select Sales.SaleID,
Sales.StartSaleDate,
Sales.EndSaleDate,
Sales.SalePercent,
COUNT(LessonID) as TotalLesson,
Sales.Status,
Sales.ExpiredStatus,
Sales.SalePrice,
Sales.IsSpecial
from Sales
LEFT JOIN SaleLessons
ON SaleLessons.SaleID = Sales.SaleID
group by Sales.Status,
Sales.IsSpecial,
Sales.StartSaleDate,
Sales.EndSaleDate,
Sales.SalePercent,
Sales.SaleID,
Sales.ExpiredStatus,
Sales.SalePrice
You really don't need the ORDER BY clause, you can use it later when extracting data from the view.
Also, here is a very informative answer on this subject - https://stackoverflow.com/a/15188437/7119478
i need to group some data but because there are 4 store images , sql query return 4 result for every store. How can i get only one for a store by using sql query ?
select s.name,si.SHOP_IMG_PATH,count(*) amount from stab t
inner join shop s on (s.shop_id = t.shop_id)
inner join SHOP_IMG si on (s.shop_id= si.SHOP_ID)
where t.acct_id = 111 and t.CR_DATE >= sysDate - 1
group by s.name,si.SHOP_IMG_PATH
order by 3 desc,1 asc
As you see below image there a re 4 images so query can give random image
You are grouping by s.name, si.SHOP_IMG_PATH it will consider all possible combination of s.name, si.SHOP_IMG_PATH as separate you need to keep group by only s.name
Try this
SELECT a.NAME, a.PATH, a.AMOUNT
FROM (select
s.name AS 'NAME', si.SHOP_IMG_PATH AS 'PATH', count(*) AS 'AMOUNT',
ROW_NUMBER() OVER(PARTITION BY s.name
ORDER BY type si.SHOP_IMG_PATH) AS rk
from
stab t
inner join shop s on (s.shop_id = t.shop_id)
inner join SHOP_IMG si on (s.shop_id= si.SHOP_ID)
where t.acct_id = 111 and t.CR_DATE >= sysDate - 1
group by s.name
order by 3 desc,1 asc) a
WHERE a.rk = 1;
Alternative
You will get result but this is just a workaround and easy alternative to your problem but not a good one.
select s.name AS 'NAME', min(si.SHOP_IMG_PATH) AS 'PATH', count(*) AS 'AMOUNT',
from
stab t
inner join shop s on (s.shop_id = t.shop_id)
inner join SHOP_IMG si on (s.shop_id= si.SHOP_ID)
where t.acct_id = 111 and t.CR_DATE >= sysDate - 1
group by s.name
order by 3 desc,1 asc
This second query will return result as per your need
group by s.name, si.SHOP_IMG_PATH
You're telling it to differentiate them according to SHOP_IMG_PATH. Hence, it shows 4 results, one for each of those.
You'll have to drop SHOP_IMG_PATH from the select clause, if you won't let it use it.
Edit
Got your comment. What you're looking for is random aggregation. This is achieved diferently on different SQL engines. Google around for the one you're using.
If it's Oracle, as indicated by the question tag, here
I solved my problem by using below query,
select s.name,t.shop_id,(select min(SHOP_IMG_PATH) from SHOP_IMG si where shop_id =t.shop_id),count(*) amount from stab t
inner join shop s on (s.shop_id = t.shop_id)
where t.acct_id = 111 and t.CR_DATE >= sysDate - 1
group by s.name,t.shop_id
order by 4 desc,1 asc