SQL Group By question SQL Server 2005 CE - sql-server-2005

I'm having a problem with an sql query that i use for my mobile application which uses sql server 2005 ce. I'm not so good with t-sql, so have a problem with this query
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
GROUP BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE
HAVING TP.ID_TASK_MASTER = #P_IDTASKMASTER
What i try to do is doing a group by on ds_problem field, getting an error like this :
{"In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = TP,SW_HASOK ]"}
So what i made wrong? Thanks..

I suppose what you meant to do was
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
WHERE TP.ID_TASK_MASTER = #P_IDTASKMASTER
ORDER BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM,TP.ID_TASK_PROBLE

Related

Sql to Linq. how can i translate? Is there a translation program?

SELECT Musteriler.MusteriAdiSoyadi,Sum(Odemeler.OdemeTutari) AS ToplamOdeme FROM Odemeler
Right JOIN Musteriler ON Odemeler.OdeyenId = Musteriler.Id
GROUP BY Musteriler.MusteriAdiSoyadi,Musteriler.KayitTarihi ORDER BY Musteriler.MusteriAdiSoyadi;
How do I convert the running sql query to linq

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;

SQL Server 2008 Issue With GROUP BY

SELECT
ETRN_MFTransactionGroup.FolioNumber,
ETRN_MFTransactionGroup.PrimaryApplicantContactID,
ETRN_MFTransaction.PK_TransactionGroupID AS Expr1,
ETRN_MFTransaction.PK_SchemeProductID,
ETRN_MFTransaction.Units,
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID,
PA_E_CUSTOMER.dbo.ECUS_Contact.UserID,
PA_E_CUSTOMER.dbo.ECUS_Contact.FName,
PA_E_CUSTOMER.dbo.ECUS_Contact.MName,
PA_E_CUSTOMER.dbo.ECUS_Contact.LName,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID AS Expr2,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.RegistrarCode,
ETRN_MFTransactionGroup.PK_TransactionGroupID
FROM
ETRN_MFTransactionGroup
INNER JOIN
ETRN_MFTransaction ON
ETRN_MFTransactionGroup.PK_TransactionGroupID = ETRN_MFTransaction.PK_TransactionGroupID
INNER JOIN
PA_E_CUSTOMER.dbo.ECUS_Contact ON
ETRN_MFTransactionGroup.PrimaryApplicantContactID =
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID
INNER JOIN
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct ON
ETRN_MFTransaction.PK_SchemeProductID =
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID
**GROUP BY
ETRN_MFTransactionGroup.FolioNumber,**
ETRN_MFTransactionGroup.PrimaryApplicantContactID,
ETRN_MFTransaction.PK_TransactionGroupID,
ETRN_MFTransaction.PK_SchemeProductID,
ETRN_MFTransaction.Units,
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID,
PA_E_CUSTOMER.dbo.ECUS_Contact.UserID,
PA_E_CUSTOMER.dbo.ECUS_Contact.FName,
PA_E_CUSTOMER.dbo.ECUS_Contact.MName,
PA_E_CUSTOMER.dbo.ECUS_Contact.LName,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.RegistrarCode,
ETRN_MFTransactionGroup.PK_TransactionGroupID
The query above works absolutely fine but i need to group only by FolioNumber (i.e - ETRN_MFTransactionGroup.FolioNumber). Grouping by rest of the fields not required at all!
If you don't want to group on the other fields, you need some aggregate function to tell SQL how to treat the multiple records. MAX, or MIN work well for varchar fields. SUM, AVERAGE work well for numerics.

How can I eliminate the subquery from this sql query?

I have a query which should work, but it seems I am a victim of a poor database technology. I need to run the query below on a Pervasive SQL database. The manufacturer of the product using Pervasive tells me the version they are using is 10 which should support subqueries, but I have yet to be able to run even a simple subquery. So, I am wondering if the following query can be rewritten to eliminate the subquery:
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
OuterTime.Units,
(
SELECT
SUM(SubQueryTime.Units)
FROM
PRT_NEW__TIME AS SubQueryTime
WHERE
SubQueryTime.Employee = OuterTime.Employee
GROUP BY
SubQueryTime.Employee
) AS TotalHoursForEmp
FROM
PRT_NEW__TIME AS OuterTime
In standard SQL you can do this, though I have no idea about PervasiveSQL specifically...
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
OuterTime.Units,
COALESCE(TotalHoursForEmp.TotalUnits, 0) AS TotalUnits
FROM
PRT_NEW__TIME AS OuterTime
LEFT JOIN
(
SELECT
Employee,
SUM(Units) AS TotalUnits
FROM
PRT_NEW__TIME
GROUP BY
Employee
)
AS TotalHoursForEmp
ON TotalHoursForEmp.Employee = OuterTime.Employee
Not a Pervasive SQL guy, but would this work? Not sure it accomplishes your query goals:
SELECT
OuterTime.Employee,
OuterTime.Date,
OuterTime.Pay_ID,
OuterTime.Description,
OuterTime.Equipment,
OuterTime.JC_Cost_Code,
OuterTime.JC_Category,
SUM(OuterTime.Units) AS TotalHoursForEmp
GROUP BY OuterTime.Employee, OuterTime.Date, OuterTime.Pay_ID,
OuterTime.Description, OuterTime.Equipment, OuterTime.JC_Cost_Code,
OuterTime.JC_Category
FROM
PRT_NEW__TIME AS OuterTime

How to use CONTAINS with inline queries in SQL Server 2008?

I have this sql query where I'm trying to use CONTAINS to search the title field.
But I get this error.
"Cannot use a CONTAINS or FREETEXT predicate on column 'Title' because it is not full-text indexed."
The Titles table has been indexed and a CONTAINS works fine with a simple search.
Does anyone know what I'm doing wrong? Are CONTAIN queries not supported with inline queries?
This query is being ran in SQL Server 2008.
SELECT pi.PublisherGUID, pi.Publisher, pi.TitleGUID, pi.Title,
pi.YearsPublished, pi.FrontImage, pi.IssueGUID, pi.IssueNumber,
pi.IssueVariation, pi.IssueNotes, pi.CoverDate, pi.IsForSale
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY PublicIssues.Title,PublicIssues.IssueNumber) AS RowNum,
PublicIssues.PublisherGUID, PublicIssues.Publisher,
PublicIssues.TitleGUID, PublicIssues.Title,
PublicIssues.YearsPublished, PublicIssues.FrontImage,
PublicIssues.IssueGUID, PublicIssues.IssueNumber,
PublicIssues.IssueVariation, PublicIssues.IssueNotes,
PublicIssues.CoverDate, PublicIssues.IsForSale
FROM (SELECT dbo.tblTitles.PublisherGUID, dbo.tblPublishers.Name AS Publisher,
dbo.tblTitles.TitleGUID, dbo.tblTitles.Title,
dbo.tblTitles.YearsPublished, dbo.tblIssues.IssueGUID,
dbo.tblIssues.IssueNumber, dbo.tblIssues.IssueVariation,
dbo.tblIssues.IssueNotes, dbo.tblIssues.CoverDate,
dbo.tblStockIssueImages.FrontImage,
ci_owner.IssueForSale(dbo.tblIssues.IssueGUID) AS IsForSale
FROM dbo.tblStockIssueImages RIGHT OUTER JOIN
dbo.tblIssues ON
dbo.tblStockIssueImages.StockIssueImageGUID = dbo.tblIssues.StockIssueImageGUID
LEFT OUTER JOIN
dbo.tblTitles INNER JOIN
dbo.tblPublishers ON dbo.tblTitles.PublisherGUID = dbo.tblPublishers.PublisherGUID
ON dbo.tblIssues.TitleGUID = dbo.tblTitles.TitleGUID
)
AS PublicIssues
WHERE 1=1 AND CONTAINS(Title,#xTitle)
) AS pi
WHERE RowNum BETWEEN (#xPageNum - 1) * #xPageSize + 1 AND
#xPageNum * #xPageSize ORDER BY pi.Title
Indeed, in the context of PublicIssues, Title is not full-text indexed.
It is indexed in the the table tblTitles.
I think it may be possible to move the CONTAINS predicate inside the expression which defines PublicIssues. Something like the following. However I suspect (with the hint of the 1=1) that the idea is to have various other criteria, and it may not be feasible to have all of them "inside". It being [apparently] dynamic SQL, it may be feasible to craft the query by placing the search criteria in one of the two locations as appropriate.
FROM (SELECT dbo.tblTitles.PublisherGUID, dbo.tblPublishers.Name AS Publisher,
dbo.tblTitles.TitleGUID, dbo.tblTitles.Title,
dbo.tblTitles.YearsPublished, dbo.tblIssues.IssueGUID,
dbo.tblIssues.IssueNumber, dbo.tblIssues.IssueVariation,
dbo.tblIssues.IssueNotes, dbo.tblIssues.CoverDate,
dbo.tblStockIssueImages.FrontImage,
ci_owner.IssueForSale(dbo.tblIssues.IssueGUID) AS IsForSale
FROM dbo.tblStockIssueImages RIGHT OUTER JOIN
dbo.tblIssues ON
dbo.tblStockIssueImages.StockIssueImageGUID = dbo.tblIssues.StockIssueImageGUID
LEFT OUTER JOIN
dbo.tblTitles INNER JOIN
dbo.tblPublishers ON dbo.tblTitles.PublisherGUID = dbo.tblPublishers.PublisherGUID
ON dbo.tblIssues.TitleGUID = dbo.tblTitles.TitleGUID
WHERE CONTAINS(Title,#xTitle) --- this lined moved
)
AS PublicIssues