I have the following query generated in a Cognos report.
My problem is I need it to work, with the same logic / filter in SQL Server. Can the filter below (COGNOS syntax) be generated to work in the same way, like a WHERE in SQL Server?
select
*
from
dbo.ia_code
group by
client__iacode.ia_code,
client__iacode.ia_short_descr
------ here my problem
filter
(rank() over ( at client__iacode.ia_code order by XCOUNT(client.client_code at client__iacode.ia_code,client.client_id for client__iacode.ia_code ) desc nulls last) <= 25) and
(RCOUNT(rank() over ( at client__iacode.ia_code order by XCOUNT(client_document.client_document_id for client__iacode.ia_code ) desc nulls last) at client__iacode.ia_code order by rank() over ( at client__iacode.ia_code order by XCOUNT(client_document.client_document_id for client__iacode.ia_code ) desc nulls last) asc,client__iacode.ia_code asc,client__iacode.ia_short_descr asc ) <= 25)
Any help would be appreciated.
Use where before group by for filter
Group by just use for aggregate-function call in query like below
select
Sum(id),client__iacode.ia_code,client__iacode.ia_short_descr
from
dbo.ia_code
group by
client__iacode.ia_code,
client__iacode.ia_short_descr
Use having for functionality filter in query
Read this statement for how to use :
Where : w3schools
Group by: w3shoolcs
Having : w3shoolcs
in finally :
I tried change your code to sql server but i don't now about out put of this code. please add table sample data and sample of out put.
First try changing the query's (or data source's) "Rollup Processing" property to "Database". That should help converting extended aggregate functions (XCOUNT etc) to native SQL. Also check out "Use SQL parameters" property and set it to "Literal" and see if that will helps with the parameters in native SQL. Once you do those, paste the new generated native query here.
Screenshot of the properties window
This is a snippet from my answer to the following Stackoverflow question. Read more details there and also follow that question as it may help with your question too.
Convert IBM Cognos SQL which contains a filter to Microsoft SQL Server Query
Related
I have developed an SQL query that uses sub-query based on a result set (i.e. a query queries the results of a sub-query). The query executes as expected in MS Access but will not execute in vb.net (including query builder). The error I receive in query builder is:
Error in WHERE clause near 'ORDER'. Unable to parse query text.
I understand that a typical sub-query selects based on the WHERE clause whereas my SQL is querying based on the FROM clause. Because Access doesn't support the LIMIT command, I can't figure out how structure this SQL so it will run in VB.NET. If the LIMIT command was supported I think I could structure the sub-query properly.
Here is my SQL:
SELECT AVG(Differential) * .96
FROM
(SELECT TOP 5 Differential
FROM
(SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = ?
ORDER BY GameDate DESC)
ORDER BY Differential ASC, GameDate DESC)
If I change PlayerID = ? to PlayerID = 1 (which is a valid value), query builder gives me the same error as above but I can execute the query in query builder.
I've also tried importing (?) the query from Access into VB.NET and I get the same error.
Any suggestions on how to structure my query so it will execute properly?
Adding more information:
I am implementing via a table adapter. See image.
Selecting the query and clicking configure displays the following
Then if I click on the Query Builder button I get this error:
Error Message
My code to implement the query looks like this:
Dim factor As Single
factor = ScoringHistoryTableAdapter.CalculateFactor(1)
i think you are missing alis name or your subquery
SELECT AVG(Differential) * .96
FROM
(SELECT TOP 5 Differential
FROM
(SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = 1
ORDER BY GameDate DESC) a
ORDER BY Differential ASC, GameDate DESC) b
It looks like your OrderBy clauses are reversed.
You're ordering by GameDate in the middle From clause, but GameDate isn't included in that Select, only Differential.
Try:
SELECT AVG(Differential) * .96
FROM (
SELECT TOP 5 Differential
FROM (
SELECT TOP 10 GameDate, Differential
FROM ScoringHistory
WHERE PlayerID = ?
ORDER BY Differential ASC,GameDate DESC
)
ORDER BY Differential DESC
)
As the original poster I thought I would close the loop for everyone on this.
A little more research indicates that this is a known problem(?) with sub queries and the table adapter wizard. Now knowing this, i learned how to execute SQL the old fashioned way and used ExecuteScalar as Charles had indicated and the SQL works fine.
I'm trying to follow a tutorial completed by SQLMag
http://sqlmag.com/t-sql/last-non-null-puzzle
SELECT id, col1, relevantid,
MAX(relevantid) OVER( ORDER BY id
ROWS UNBOUNDED PRECEDING ) AS grp
FROM dbo.T1
CROSS APPLY ( VALUES( CASE WHEN col1 IS NOT NULL THEN id END ) )
AS A(relevantid);
to get this to work on sql server 2008. Everytime i try to MAX(relevantid) OVER (ORDER BY id) I receive a syntax error near order . Is there a way around this if I partition it is not a problem but if I only include order I get problems.
it looks like some of your syntax will not work with SQL server 2008 after reading the Microsoft doc and finding this article from brent Ozar.
https://learn.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql
https://www.brentozar.com/sql-syntax-examples/window-function-examples-sql-server/
it would be better to use a later version of sql server for your own use if thats what you are trying to do.
In MS Access 2010 I have the following code:
SELECT
[Teile-LF-Beziehungen].Lieferant,
COUNT([Teile-LF-Beziehungen].Lieferant) AS [Anz Teile],
First([Teile-LF-Beziehungen].Name) AS Name
FROM
[Teile-LF-Beziehungen]
GROUP BY
[Teile-LF-Beziehungen].Lieferant
ORDER BY
COUNT([Teile-LF-Beziehungen].Lieferant) DESC;
I want to put that query into SQL Server, because MS Access should be only the frontend.
But in SQL Server I can't use the ORDER in a view. But why? I don't understand it. The code I want to use in SQL Server:
SELECT
[Lieferant],
COUNT([Lieferant]) AS [Anz Teile],
MIN([Name]) AS [Name]
FROM
[dbo].[VIEW_Teile-LF-Beziehungen]
GROUP BY
[Lieferant]
ORDER BY
COUNT([Lieferant]) DESC;
I know it don't work. But is there any way to incur a MS Access query 1:1 to a SQL Server query (view)?
Only the outermost select can use an order by (but you might state a TOP 100 percent to trick this out). Therefore it is perfectly OK, that at VIEW does not allow this.
Many people think, that tables have kind of an implicit order (as you see the result ordered), but this is random... The next call could lead to a different sorting.
There is another way using ROW_NUMBER with OVER(ORDER BY). The result is delivered in this order and the order is guaranteed as long the orderby is sorting after unique values.
EDIT
Sorry my first attempt was to quick. The ROW_NUMBER was not allowed due to the grouping
This should work:
SELECT tbl.Lieferant
,tbl.[Anz Teile]
,tbl.Name
,ROW_NUMBER() OVER(ORDER BY tbl.[Anz Teile] DESC) AS Sort
FROM
(
SELECT [Lieferant]
,COUNT([Lieferant]) AS [Anz Teile]
,MIN([Name]) AS [Name]
FROM [dbo].[VIEW_Teile-LF-Beziehungen]
GROUP BY [Lieferant]
) AS tbl;
EDIT2
This SELECT can be placed within a VIEW, just place your CREATE VIEW YourViewName AS before the SELECT and execute. After this you'll be able to do a SELECT * FROM YourViewName to get a sorted list.
BUT
As stated in many places: The best is the outermost ORDER BY in any case!
ORDER BY doesnt work inside the view. SQL server is free to return the rows anyway he want if you dont include the order by when calling the View
So you need
SELECT *
FROM yourView
ORDER BY yourField
EDIT: Im saying if your view is
CREATE VIEW yourView AS
SELECT
[Lieferant],
COUNT([Lieferant]) AS [Anz Teile],
MIN([Name]) AS [Name]
FROM
[dbo].[VIEW_Teile-LF-Beziehungen]
GROUP BY
[Lieferant];
Then you call your View like this
SELECT *
FROM yourView
ORDER BY [Anz Teile]
SQL Fiddle is currently down regarding MS SQL Server code, so here is a dropbox link to a .txt containing the DDL to create the schema I'm using:
https://www.dropbox.com/s/6si4r37449q3ajb/DDL.txt?dl=0
I'm studying for an exam and I know there's a more efficient way to code this, I just don't know what it is.
5. Find out the department which has the highest number of personal computers installed.
select top(1) pc.location, count(pc.location) as number_of_comps_in_dept
from pc
group by location
order by number_of_comps_in_dept desc
My code featured above works, but what if I wanted to just get the department name (labeled location in this case)? I can't really call a single value back via my current code - which isn't friendly to procedures, functions, and triggers down the road.
Thank you for your help.
You can just remove the other columns in your SELECT statement. However, you need to replace the column in the ORDER BY clause with the aggregate:
select top(1) pc.location
from pc
group by location
order by count(pc.location) desc
ONLINE DEMO
Use result from subquery to get depratment name only
SELECT Dept_with_Max_Computers
FROM
(
select top(1) pc.location Dept_with_Max_Computers, count(pc.location) as number_of_comps_in_dept
from pc
group by location
order by number_of_comps_in_dept desc
) Z
Actually, I have the task of finding the employees based on the salary rank.
So I used Dense_Rank() and got the answer.
Initially I was told to solve in SQL SERVER 2005.
Later on they changed the requirement and is saying that the query should run in SQL SERVER 2000 also.
I solved that using a while loop.
But they are saying that they will accept the solution using SET BASED approach.
How to do that?
Thanks in advance
Refer to this article, Ranking In SQL Server 2000
The author talks about how to implement Dense_Rank()
You may try something like this:
SELECT * ,
( SELECT COUNT(C1)
FROM T1
WHERE P1.C1>= T.C
) AS Rnk
FROM T
ORDER BY C DESC;