Query not executing, unable to run in Big - sql

Quer logs.type = apps.log_type
WHERE logs.datetime query
Quer logs.type = apps.log_type
WHERE logs.datetime
query enter code here : the sql
Query not executing, unable to run in sql server

For GROUP BY to work it should be used along with aggregate functions in the SELECT list
Something like below example:
#standardSQL
SELECT
MAX(logs.datetime) latest_datetime,
logs.type,
SUM(LENGTH(logs.message)) messages_length,
apps.name
FROM logs INNER JOIN apps
ON logs.type = apps.log_type
WHERE logs.datetime > "2017-07-01T00:00:00"
AND logs.datetime < "2017-07-02T00:00:00"
GROUP BY apps.name, logs.type
so the rule of thumb is - any field in SELECT statement should be either in GROUP BY list or (if not in GROUP BY) to be used with Aggregate Function

Related

how is the expression invalid in my simple query?

I'm new to SQL, creating a simple query from an identical example but not sure why 1 expression is invalid?
I tried this statement:
SELECT
A.AccountId,
A.Address1_City,
A.Address1_Country,
A.Address1_PostalCode,
A.Address1_StateOrProvince,
A.CreatedOn,
A.EMailAddress1,
A.mcs_ABN2,
A.mcs_AdminContact,
A.mcs_AdminContactName,
A.mcs_AreyouinterestedinInternationalDevelopmen,
A.mcs_BenefitsUtilised,
A.mcs_BusinessNameOrganisationName,
A.mcs_doyouprovideaccreditedtraining,
A.mcs_doyouprovidenonaccreditedtraining,
A.mcs_DoyouprovideservicesinanyNDISTrialSites,
A.mcs_DoyouprovideservicestoChildrenYoungPeople,
A.mcs_DummyRecord,
A.mcs_EntityStatusCode,
A.mcs_IndustryOperatingIn,
A.mcs_IsStreetAddresssameasPostalAddress,
A.mcs_MembershipNote,
A.mcs_MultiStateDepartment,
A.mcs_NotefromApplicant,
A.mcs_NumberofEmployeesDec,
A.mcs_OtherTradingNameOrganisationName,
A.mcs_PartnerDisabilityServiceOrganisations,
A.mcs_PrimaryProductsServicesToPromote,
A.mcs_RevenueDisabServiceProvisionTotalAnnual,
A.mcs_SageERPCustomerCode,
A.mcs_SageErpExportFlag,
A.ModifiedOn,
A.Name,
A.nds_Account_MembershipNumberCopy,
A.nds_IsMember,
A.nds_MainContact,
A.nds_MainContactName,
A.nds_MemberCategoryCopy,
A.PrimaryContactId,
A.Revenue,
A.Revenue_Base,
A.StatusCode,
A.WebSiteURL
FROM
account A
WHERE
A.statuscode = 1
GROUP BY
A.name
and got this error:
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Column 'account.AccountId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Group By is for aggregate functions (Count, Sum, etc), so if you use group by you need to group by at least every column being returned in the select SQL, otherwise it wouldn't be able to do the aggregation. This looks like you just want to order by Name though, so if you replace
GROUP BY A.name
with
ORDER BY A.name
it should work. If you don't care about order or aggregation just remove that part of the query entirely.

How to execute subquery in Oracle? My query gets parentheses erro

I'm trying to execute a query inside LabVIEW so I can informations stored in a Oracle Database, but when a try to execute a query with parenthesis it doesn't works and gives me this erro:
ADO Error: 0x80004005 Exception occured in Microsoft OLE DB Provider for ODBC Drivers: [Oracle][ODBC][Ora]ORA-00907: parêntese direito não encontrado
Here is the SQL query I'm trying to execute:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=(
SELECT
CODIGOFAIXAMODELO
FROM PROCEDS
WHERE
PROCEDS.CODIGOFAIXAMODELO=F.CODIGOFAIXAMODELO
LIMIT 1
)
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
The %CODIGOMODELO% is replaced with a value by LabVIEW.
When I try the following Query it works:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=P.CODIGOFAIXAMODELO
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
The problem with the second solution is that it returns me many P.CODIGOPROCEDIMENTO, and what I want is to get only one even when there are many.
there is no LIMIT function in Oracle
you need to use ROWNUM = 1 or OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
Also as it is stated by #APC, you shouldn't be joining your table on a subquery.
I would write it this way. It may be more efficient and more readable to avoid trying to evaluate a subquery inside an expression:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F
INNER JOIN (
SELECT
P1.CODIGOFAIXAMODELO,
MAX(P1.CODIGOPROCEDIMENTO) AS CODIGOPROCEDIMENTO
FROM PROCEDS P1
GROUP BY P1.CODIGOFAIXAMODELO
) P ON P.CODIGOFAIXAMODELO = F.CODIGOFAIXAMODELO
WHERE F.CODIGOMODELO = '%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;
MAX() is an Aggregate Function that will return only one value for each group - specified in the GROUP BY clause. Therefore, using a subquery and joining on CODIGOFAIXAMODELO ensures that only one row is filtered against the main query.
The results really depend on the key structures, datatypes and how many rows are available in PROCEDS. There are of course other, more complex methods to achieve the same result, such as using Analytic Functions.
I think you can write it this way:
SELECT
F.CODIGOFAIXAMODELO,
F.CODIGOMODELO,
F.INICIOESCALA,
F.FUNDOESCALA,
F.FAIXA,
F.DESCFAIXA,
F.ORDEM,
P.CODIGOPROCEDIMENTO
FROM FAIXAS F INNER JOIN PROCEDS P ON F.CODIGOFAIXAMODELO=P.CODIGOFAIXAMODELO and ROWNUM = 1
WHERE
F.CODIGOMODELO='%CODIGOMODELO%'
ORDER BY F.ORDEM ASC;

"Your query does not include the specified expression..."

I have tried endless things to get this to work and it seems to break over and over again and not work. I'm trying to GROUP BY product after I have calculated the field quantity returned/quantity ordered, but I get the error
your query does not include the specified expression 'quantity_returned/quantity_ordered' as part of an aggregate function.
I do not want to GROUP BY quantity_returned, quantity_ordered, and product, I only want to GROUP BY product.
Here's what my SQL looks like currently...
SELECT
quantity_returned/quantity_ordered AS percentage_returned,
quantity_returned,
quantity_ordered,
returns_fact.product
FROM
Customer_dimension
INNER JOIN
(
Product_dimension
INNER JOIN
(
Day_dimension
INNER JOIN
returns_fact
ON Day_dimension.day_key = returns_fact.day_key
)
ON Product_dimension.product_key = returns_fact.product_key
)
ON Customer_dimension.customer_key = returns_fact.customer_key
GROUP BY returns_fact.product;
When you use a group by you need to actually include everything in your select that isn't a aggregate function.
I have no idea how your tables are set up, but I am throwing a blind dart. If you provide fields in each of the 4 tables someone will be better able to help.
SELECT returns_fact.product, count(quantity_returned), count(quantity_ordered), count(quantity_returned)/count(quantity_ordered) as percentage returned

SQL COUNT FORM JOIN TABLES

I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).

How should I create a temporary table when Union is used?

I can't use Dynamic Value bcoz of Error stating
"Lookup Error - SQL Server Database Error: Cannot perform an aggregate function on an expression containing an aggregate or a subquery."
Here is the Scenario :
Query 1
select pr.PRDCT,sum(CASE when pr.DEFINITIONCD='NOP' and pr.PERIOD='D' then pr.PRAMOUNT else 0 END)
as 'NOP D' from PRODUCTWISE_REPORT pr group by pr.PRDCT
Query 2
select DEFINITIONTYPECD from REPORTKPIMAPTXN where DEFINITIONTYPECD='NOP' and REPORTSEQ = (select REPORTSEQ from report_m where REPORTCD='MIS_Product_Wise_Report')
Query 2 returns 'NOP'
so when I put Query 2 in Query 1 for 'NOP', it throws Error
How to resolve this when I've to User Dynamic Query 2 ?
Your second query looks it could be rewritten with a join instead of that subselect. something like this. Of course you are still going to have some issues because your first query has two columns and this has only 1 column. You will have to add another column (can be NULL) to this query before the UNION will actually work.
select r.DEFINITIONTYPECD
from REPORTKPIMAPTXN r
INNER JOIN report_m m on m.REPORTSEQ = r.REPORTSEQ
where DEFINITIONTYPECD = 'NOP'
and r.REPORTCD = 'MIS_Product_Wise_Report'