display the same column twice with different results - sql

I have a column that contains Enable and Disable values, which is associated by an event id, in this event table I have the user datatime, I would like to create a query and display the time the enabled event was triggered and in the same row. the time the disable was triggered
SELECT
valv.valvOf, valv.cam,
cod.descOf Habilita, cod.descof Desabilita
FROM
{oj (aspersao.dbo.events events LEFT JOIN aspersao.dbo.valv valv ON events.valv_id = valv."id") LEFT JOIN aspersao.dbo.cod cod ON events.cod_id = cod.id}
WHERE
valv.cam = '1'
and
Habilita = 'Habilitou valvula'
and
Desabilita = 'Desabilitou valvula'

Even after correcting your code so that you are not trying to refer to columns by their aliases in the WHERE clause, your code is nonsense. No row will have a column equal to two different values.
Likely you want to pull in the table twice. I'm also getting rid of the ODBC specific curly brace syntax.
SELECT
valv.valvOf, valv.cam,
cod1.descOf AS Habilita, cod2.descof AS Desabilita
FROM
aspersao.dbo.events AS events
LEFT JOIN aspersao.dbo.valv AS valv ON events.valv_id = valv."id"
LEFT JOIN aspersao.dbo.cod AS cod1
ON events.cod_id = cod1.id
AND cod1.descOf = 'Habilitou valvula'
LEFT JOIN aspersao.dbo.cod AS cod2
ON events.cod_id = cod2.id
AND cod2.descOf = 'Desabilitou valvula'
WHERE
valv.cam = '1'
This is pretty much a guess because there really isn't enough info in your question.
EDIT: I had the wrong test string in the cod2.descOf = line; I've fixed that.

Related

OracleAPPS- Can not get correct results for Suppliers - Bank query

I am trying to fetch suppliers and bank details in Oracle apps. I am able to write a simple query where each supplier has a supplier site attached to it. Few of my sites doesn't have a record in table "iby_pmt_instr_uses_all". But even then I want to show them . So I am using the outer join on this table. But the issue is when I am putting this outer join condition, I am getting double the records in the query. I believe I am missing on some condition, but can not figure out which one.
SELECT *
FROM apps.iby_pmt_instr_uses_all instrument,
apps.iby_account_owners owners,
apps.iby_external_payees_all payees,
apps.iby_ext_bank_accounts ieb,
apps.ap_supplier_sites_all asa,
apps.ap_suppliers asp,
apps.ce_bank_branches_v cbbv
WHERE owners.ext_bank_account_id = ieb.ext_bank_account_id
AND owners.ext_bank_account_id = instrument.instrument_id(+)
AND payees.ext_payee_id = instrument.ext_pmt_party_id(+)
AND cbbv.branch_party_id = ieb.branch_id
AND payees.payee_party_id = owners.account_owner_party_id
AND payees.supplier_site_id = asa.vendor_site_id
AND asa.vendor_id = asp.vendor_id
AND asp.vendor_name = 'PANALYTICAL'
--and ieb.bank_account_num = asa.VENDOR_SITE_CODE
If I add and ieb.bank_account_num = asa.VENDOR_SITE_CODE this condition I get the correct record, but actually this is not the right join, because there is no relationship between these 2 columns, so this will fail for other suppliers.
Can you please suggest what additional join I can put , so that I get the right result.
Thanks

Microsoft Access won't show query if date is empty?

--SOLVED--
I'm having a problem with a query in Microsoft Access.
I need this query to show a list of entries where a specific Date field is empty. So it must sort and still show all the data, but only for entries where that specific date is missing.
There are a lot of dates in the table.
When I create a filter or just add criteria to the specific field in "Design View", it runs the query but it's empty without fail every time.
The field format is naturally a "Date" type field in the table.
I hope anyone can help me.
Thank you.
SQL BELOW:
SELECT rptClaimDateEmpty.Claim_Date, *
FROM (SELECT tblContainers.*, tbluCommodities.Commodity,
tbluContainerSizes.ContainerSize, tbluCountryOfLoading.CountryOfLoading,
tbluCountryOfOrigin.CountryOfOrigin, tbluShippers.Shipper,
tbluShippingLines.ShippingLine, tbluPortOfLoading.PortOfLoading,
tblBOLDates.BOLNumber FROM tblBOLDates INNER JOIN (tbluPortOfLoading RIGHT
JOIN (tbluShippingLines RIGHT JOIN (tbluShippers RIGHT JOIN
(tbluCountryOfOrigin RIGHT JOIN (tbluCountryOfLoading RIGHT JOIN
(tbluContainerSizes RIGHT JOIN (tbluCommodities RIGHT JOIN tblContainers ON
tbluCommodities.CommodityID = tblContainers.CommodityID) ON
tbluContainerSizes.ContainerSizeID = tblContainers.Container_SizeID) ON
tbluCountryOfLoading.CountryOfLoadingID =
tblContainers.Country_of_LoadingID) ON tbluCountryOfOrigin.CountryOfOriginID
= tblContainers.Country_of_OriginID) ON tbluShippers.ShipperID =
tblContainers.ShipperID) ON tbluShippingLines.ShippingLineID =
tblContainers.Shipping_LineID) ON tbluPortOfLoading.PortOfLoadingID =
tblContainers.Port_of_LoadingID) ON tblBOLDates.BOLDateID =
tblContainers.BOLDateID) AS rptClaimDateEmpty;
I just need it to sort via the the "Claim Date" field where "Claim Date" is empty.
Wouldn't it just be:
SELECT *
FROM (...) AS rptClaimDateEmpty
WHERE rptClaimDateEmpty.Claim_Date Is Null;

Long Text Field over 255 Characters gets truncated

Not sure why my field in my query is getting truncated upon the return of the result. The value is being stored in the field, but gets truncated by access to help with "performance". I have reviewed multiple forums and SO posts to no avail.
Problems listed at link do not apply, Aggregation, Uniqueness, Union, Format Property, Row Source
What is wrong with my query? Instructions field in the Customer table is the one that is getting truncated.
Here is the raw query generated by access:
SELECT Task.ID, Task.TaskID, Task.TaskName, Task.TypeID, TaskType.TaskTypeName, Task.CustomerID, Customer.CustomerName, Customer.OnHold, Customer.Blacklisted, Customer.CustomerEngagementRecieved, Customer.AutoEmail, Customer.SpecialInstructions, Customer.Instructions, Task.QuoteRequired, Task.PriorityID, Priority.Priority, Task.Min, Task.Max, Task.Projected, Task.DeadlineDate, Task.ResourceID, Resource.ResourceName, Resource.Email, Resource.Extension, Task.Description, Task.StatusID, Status.Status, Task.DeveloperLog, Task.TaskPOCID, POC.Phone, POC.Email, Task.OtherPOC, Task.OtherPOCPhone, Task.OtherPOCEmail, Task.FolderPath, Task.StopBilling, Task.Premium, Task.EntryDate, Task.CompleteDate, Task.AssignedBy, Task.SettingsID, Settings.AutoEmail
FROM TaskType
INNER JOIN (Status
INNER JOIN (Settings
INNER JOIN (Resource
INNER JOIN (Priority
INNER JOIN (Customer
INNER JOIN (Task
INNER JOIN POC ON Task.TaskPOCID = POC.POCID)
ON Customer.CustID = Task.CustomerID)
ON Priority.PriorityID = Task.PriorityID)
ON Resource.ResourceID = Task.ResourceID)
ON Settings.SettingsID = Task.SettingsID)
ON Status.StatusID = Task.StatusID)
ON TaskType.TTID = Task.TypeID;
`
Have a close read of this - http://allenbrowne.com/ser-63.html something in your set up will causing the truncation.
If it's when you cut and paste the query results that can also be mis-leading. When you say a Long Text are these linked tables?
I'd also rename your Min and Max fields as they are reserved words and may cause access to think you are aggregating your data.
So from the sounds of it, Access just sometimes will ALWAYS truncate the field no matter what the settings. There is a way to force access to show the entire field though, by using the DLOOKUP() function instead of using a Control Source.
Here is the Answer to my current Issue for reference,
=DLOOKUP("Instructions", "Customer", "CustID=" & [CustomerID])

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

SumIF in SQL Access

Hello I am a beginner in VBA/SQL. I have 2 tables [Broker] and [OPT]. [Broker] have 2 fields BRKR_CODE, Status and [OPT] Have 5 fields BRKR1, BRKR2, Date, COM_BRK1, COM_BRKR2
OPT.BRKR1 and OPT.BRKR2 are linked to Broker.BRKR_CODE and can be equal.
I want to write this query.... If Broker.Status="Active" then Select Broker.BRKR_CODE
and then for each Broker.BRKR_CODE (for example "CB") do the Sum of OPT.COM_BRKR1 when OPT.BRKR1="CB" + Sum of OPT.COM_BRKR2 when OPT.BRKR2="CB"
I wrote this code.... but it doesnot work at all... as I said I am a newbie...
SELECT IIF(Broker.Status="Active",Broker.BRKR_CODE), Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot
FROM Broker INNER JOIN OPT ON (Broker.BRKR_CODE = OPT.BRKR2) AND (Broker.BRKR_CODE = OPT.BRKR1)
GROUP BY Broker.BRKR_CODE;
Thanks a lot
If I understand correctly, you need to change two things in your query. The first is to move the condition to the where clause. You seem to only want "Active" things. The second is to change the AND in the on clause to OR. You want matches on either code.
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot
FROM Broker INNER JOIN
OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE;