SQL Subquery issue 3 - mysql

I have a issue with my SQL code.
When I run this code I'm getting such error mesage:
**
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
**
I don't know how to correct this code to run it?
Can somebothy look at it and help me with it?
Thank you for your help,
SELECT dbo.FRA$.[GUID], dbo.FRA$.[Last Name],
dbo.FRA$.[First Name], dbo.FRA$.[HR Manager Last Name],
dbo.FRA$.[HR Manager First Name], (SELECT dbo.FRA$.[Business Email Address] FROM dbo.FRA$ t WHERE t.[HR Manager ID] = t.[Emplid]) AS [HR Manager E-Mail],
dbo.LOC.Descr AS [COMPANY NAME], dbo.FRA$.[Location Code],
dbo.LOC.[ADDRESS], dbo.LOC.Postal, dbo.LOC.City, dbo.FRA$.[National ID], dbo.FRA$.[Current Employee Class Desc] AS [Current Empl Class],
dbo.FRA$.[Contract type description] AS [CONTRACT TYPE], dbo.FRA$.[Business Title],
CASE dbo.FRA$.[Current Employee Class Desc]
WHEN 'Operator' THEN 10
WHEN 'Clerical worker' THEN 22
WHEN 'Technician' THEN 32
WHEN 'Supervisor' THEN 33
WHEN 'Manager&Engineer' THEN 54
END AS [Salary brutto],
FORMAT(dbo.FRA$.[Birthdate],'yyyy-MM-dd') AS [Birth Date], FORMAT(dbo.FRA$.[Last Start Date],'yyyy-MM-dd') AS [Hire Date]
FROM dbo.FRA$
INNER JOIN dbo.LOC
ON dbo.FRA$.[Location Code] = dbo.LOC.[Location]
WHERE [HR Status] IN ('International Assignment','Active','Leave of Absence');

Try to replace
SELECT dbo.FRA$.[Business Email Address] FROM dbo.FRA$ t WHERE t.[HR Manager ID] = t.[Emplid]
with
SELECT TOP 1 t.[Business Email Address] FROM dbo.FRA$ t WHERE [HR Manager ID] = t.[Emplid]
it will take only one row from rows returned by your subquery

Related

Combining rows on a query in access 2010

First off I know nothing about VB and the post I have seen on here refer to that, I am sure that this is a very simple thing to do so please help
I am trying to hve my TN Route be my primary key/no duplicates and have date driver 1 driver 2 and driver 3 all in diff cells and all side by side, and even though the tbl that it is pulling from the TN route is primary, it keeps adding additional rows for each driver.
See attached screenshot:
Thank you in advance.
Stan
data sheet view of query
This query will do it for you. Again, it's not preferable because in Access SQL tends to be a little messy on account of it not having features that more robust SQL platforms do - but it will work. You'll need to change the name of your Date column to Route Date in order for this to work, and replace my tblTest with whatever your table name is:
SELECT DISTINCT tblTest.[TN Route], tblTest.[Route Date], tA.[Driver 1], tB.[Driver 2], tC.[Driver 3]
FROM ((tblTest LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 1] FROM tblTest WHERE [Driver 1] <> '') AS tA ON (tblTest.[Route Date] = tA.[Route Date]) AND (tblTest.[TN Route] = tA.[TN Route]))
LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 2] FROM tblTest WHERE [Driver 2] <> '') AS tB ON (tblTest.[Route Date] = tB.[Route Date]) AND (tblTest.[TN Route] = tB.[TN Route]))
LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 3] FROM tblTest WHERE [Driver 3] <> '') AS tC ON (tblTest.[Route Date] = tC.[Route Date]) AND (tblTest.[TN Route] = tC.[TN Route])
In Access 2010, go Create > Query Design > add your table, and then paste the SQL into the SQL view of the query designer. You can even make a standalone table out of the result.

Grouping by same column / variable using two separate imported views and separate calculations

So basically my code involves a created a table which has the LAST COLUMN as the SUM of the previous two columns, with the first of the two coming from table X and the second of the two columns coming from the 'INNER JOIN OF table X with table Y'.
HOWEVER, I want to group ALL THREE of these columns by the 'COUNTERPARTY', a variable which is present BOTH in 'table X' AND in 'INNER JOIN OF TABLE X WITH TABLE Y.'
The tricky part is that there will be some COUNTERPARTIES for which we have data set 1 (coming from TABLE X) and some COUNTERPARTIES for which we have data set 2 (coming from INNER JOIN OF table X with table Y) and SOME FOR WHICH WE HAVE BOTH DATASETS! I WANT TO INCLUDE THE INTERSECTION AS WELL AS THE OUTLIERS BUT ONLY AFTER CHECKED AGAINST A MASTER KEY OF COUNTERPARTIES called c.COUNTERPTY.
Please carefully go through the code below to see how the above explanation is relevant to the problem at hand. I apologize for the length of the code.
select
p.Name as ENTITY, t.[Counterparty Code], c.CNTRPTY_DS as COUNTERPARTY, cs.Tier,
... irrelevant code removed
sum((t.[Current value decimal] - t.[Trade price decimal])/100 * case when t.[Buy Sell Code] = 'B'
then 1 else -1 end * t.[Open Amount]) as [OPEN MTM ($)],
sum((t2.[Weighted Average Settled Pair Off Price] - t2.[Trade price decimal])/100 * case when t2.[Buy Sell Code] = 'B'
then 1 else -1 end * ISNULL(PO.[Pairoff Amount],0)) as [Unsettled Pairoffs/ AOTs ($)],
sum((t.[Current value decimal] - t.[Trade price decimal])/100 * case when t.[Buy Sell Code] = 'B'
then 1 else -1 end * t.[Open Amount]) + sum((t2.[Weighted Average Settled Pair Off Price] - t2.[Trade price decimal])/100 * case when t2.[Buy Sell Code] = 'B'
then 1 else -1 end * ISNULL(PO.[Pairoff Amount],0)) as [TOTAL MTM Exposure ($)]
from
[la-w08-qrm-db-1].qrmprod.dbo.vw_QRM_Trades t2
inner join
[la-w08-qrm-db-1].qrmprod.dbo.VW_QRM_TRADE_PAIROFFS PO
ON
PO.[In Ticket Number] = t2.[Ticket number]
and PO.[Portfolio ID] = t2.[Portfolio ID]
and t2.[derivative type] = 'F' -- note repeat below
and t2.[forward type] ='MBS'
and t2.[Counterparty Code] not in ('PLS', 'PNCO')
and t2.[Portfolio ID] in (1,7)
and t2.[Settlement date] > GETDATE(),
prod.dbo.vw_QRM_Trades t,
prod.dbo.portdesc p,
prod.dbo.cptyall c,
prod.dbo.VW_MB_ACTIVE_RUN r,
pulsar.dbo.CntrPrtySetup CS,
pulsar.dbo.CntrPrtyTiers CT
where
r.mrktid = 1
And r.asmpid = 1
And r.cyclid = 1
and r.compid = t.[Company ID]
and r.portid = t.[Portfolio ID]
and p.PORTID = t.[Portfolio ID]
and c.COUNTERPTY = t.[Counterparty Code] --key piece of code
and cs.CNTRPTY_NO = c.CNTRPTY_NO
and cs.PortID = t.[Portfolio ID]
and cs.Tier = ct.Tier
and t.[derivative type] = 'F' -- note repeat above
and t.[forward type] ='MBS'
and t.[Counterparty Code] not in ('PLS', 'PNCO')
and t.[Portfolio ID] in (1,7)
and t.[Open Amount] > 0
group by
p.Name, c.CNTRPTY_DS , t.[Counterparty Code], cs.Tier -- yes this
order by
p.Name, c.CNTRPTY_DS , t.[Counterparty Code], cs.Tier -- and this
The way to do is was to actually use UNION in order to join three tables together in the order and given the properties I wanted from each of them. Eventually I did manage to finish the whole stored procedure!

The recordsource [query here] specified on this form or report does not exist

I got error in populating the result of my query to access form but it gives me "The recordsource [query here] specified on this form or report does not exist
Here is my code:
Me.RecordSource = "SELECT Division, Station, EmployeeNumber, BranchCode, AccountNumber, LedgerSetNumber, EmployeeName, EffectDate, TermDate, Amount, BillingType, DateBilled, PolicyNumber, BillingStatus, UpdateStatus, DateUpdated, SequenceNumber FROM DepEdTempBilling WHERE (UpdateStatus<>'Billed') ORDER BY Division, Station, EmployeeNumber, BranchCode, AccountNumber, LedgerSetNumber, EmployeeName"
screenshot:
Your logic is correct. For some reason that doesn't work. I did the same thing with my code. Here it is, hopefully this helps.
Me.RecordSource = "SELECT tblQuoteMaster.[Quote Number], tblQuoteMaster.Status, tblQuoteMaster.Prefix, tblQuoteMaster.[Rev Number], tblQuoteMaster.[AWC Rep], tblQuoteMaster.Estimator, tblQuoteMaster.Customer, CUSLST.Name1 AS Company, tblQuoteMaster.CustomerID, tblQuoteMaster.[Approx Value], tblQuoteMaster.Currency, tblQuoteMaster.Decription, tblQuoteMaster.[Date Received], tblQuoteMaster.[Date Sent], tblQuoteMaster.[Follow Up Date], tblQuoteMaster.[Contact for Quote], tblQuoteMaster.ProductCode FROM (tblQuoteMaster INNER JOIN CUSLST ON tblQuoteMaster.CustomerID = CUSLST.[Account Code]) INNER JOIN tblContact ON CUSLST.[Account Code] = tblContact.[Customer Number] WHERE (" & strSQL & ");"
I have seen some people assign their recordset to the RecordSource. That would always give me your error. For example:
Dim rcd As RecordSet
Set rcd = db.OpenRecordset("SELECT.....")
Me.RecordSource = rcd.Name
Have re-made you table and form and tested, This error occurs when the Property Sheet (Access window not VBA editor) Form/Record Source: has “[query here]” in it clear this line out and try again, if that does not work try a clean new form with this statement. The command itself and SQL appear to be ok.

How do I find the last entry in a table, by date

My query keeps returning the last date for ONRENT and CLOSED for the same piece of equipment when I add the column "AVAILABILITY". I only want the last entry for each piece of equipment.
Here is my query:
select max([No_]), [Start Date], [Availability Status]
from [Rental Line]
group by [No_], [Start Date], [Availability Status]
If I'm understanding your question correctly, you want only the last availability status for each piece of equipment defined by No_, where "last" would be defined by the latest start date. If so, this should work:
Try this:
select RL.[No_], RL.[Start Date], RL.[Availability Status]
from [Rental Line] RL
join
(
select
No_,
MAX([Start Date]) AS [LastStartDate]
from [Rental Line] RL1
group by No_
) X on RL.No_ = X.No_ AND RL.[Start Date] = X.[LastStartDate]
select max([No_]), [Start Date], [Availability Status]
from [Rental Line]
group by [No_], [Start Date], [Availability Status]
order by [AVAILABILITY] DESC

display only sql fields having values in asp

i am having a asp web page that retrieves value from sql...it shows the fields without having values..is there anyway to display only the fields having values..this is my table
[] [][] "Mfg Dt and Exp Dt as per Annexure enclosed"
this square brackets contain null values..if the field is empty then this square bracket will not come
in first row i am having 2 sets of square brackets and in second row i am having the values in 2 sets..i need to show only the square bracket having values like in the second row..if the value is empty then dont want to show that empty brackets.
sql query
select I.[Old Product Code], I.[Trade Name], I.[Short Name], SUM(Quantity) as Qty,I.[Base Box UOM], SIH.[Customer Marks - 1] ,SIH.[Bill-to Name],SIH.[Bill-to Address], SIH.[Ship-to Name], SIH.[Ship-to Address], SIH.[Ship-to Address 2] ,SIH.[Ship-to City], SIH.[Ship-to Post Code],SIH.[Country of Origin of Goods],SIH.[Bill-to Address 2] ,SIH.[Bill-to City] , SIH.[Bill-to Post Code], CR.Name ,C.Contact,SIH.[Total Boxes], CASE when SIL.[Dimension Group Code] = 'IOL' then I.[Group Description]
END ,
CASE
when I.[EPCG License] = 1 then 'EPCG'
end,
case
when SIL.[Dimension Group Code] = 'IOL' then 'DEEC'
when I.[DEEC Lic_] = 1 then 'DEEC'
end as abcd
from [Sales Invoice Header] SIH ,[Customer] C, [Country_Region] CR,
[Sales Invoice Line] SIL,[Item] I where I.No_ = SIL.No_ and SIL.[Document No_]='PEXP1213-110' and SIH.No_ = SIL.[Document No_] and C.No_ = SIH.[Bill-to Customer No_] and C.[Country_Region Code] = CR.Code group by I.[Old Product Code], I.[Trade Name], I.[Short Name], SIH.[Sell-to Customer Name],SIH.[Sell-to City],SIH.[Customer Marks - 1],SIL.[Dimension Group Code],I.[Group Description], SIH.[Country of Origin of Goods], SIH.[Ship-to Name],SIH.[Ship-to Address], SIH.[Ship-to Address 2] , SIH.[Ship-to City], SIH.[Ship-to Post Code],SIH.[Bill-to Name],SIH.[Bill-to Address],SIH.[Bill-to Address 2],SIH.[Bill-to City] , SIH.[Bill-to Post Code],SIH.[Total Boxes],C.Contact ,CR.Name,I.[Base Box UOM], I.[EPCG License],I.[DEEC Lic_]
You need to solve it programmatically like
if cstr(rs("Old Product Code"))="[]" then
response.write("")
else
response.write(rs("Old Product Code"))
end if