I have a query which I like to use for paging. With dapper I am able to take the query below and use SplitOn which allows me to separate the results on the left side of SplitOn column into one object and results of the right side of SplitOn column into a second object. What this allows me to do is get a total records count along with records for "specified" pages only
SELECT *
FROM (
SELECT
*,1 as SplitOn,
(ROW_NUMBER() OVER(ORDER BY Id )) AS RowNum, COUNT(*) over() as TotalRows
FROM (
SELECT Id, FirstName, LastName FROM Customer
) AS FirstQuery
) AS TEMP
WHERE RowNum BETWEEN 1 + ((#Page - 1) * #PageSize) AND #Page * #PageSize
If you run above in SQL for page 6 you will get:
Id FirstName LastName SplitOn RowNum TotalRows
61 John Luke 1 1 120
62 Jane Sky 1 2 120
63 Boby Doe 1 3 120
64 Sam Ace 1 4 120
65 Dany Table 1 5 120
etc...
I am trying to accomplish the same with EF6 but I cannot for the life of me figure out how to convert that query to an equivalent LinQ Expression.
I realize that SplitOn may not be possible with EF6, so I can work around that with a single object, but I still cannot figure out how to make my query with LinQ.
Related
My table contains 113 people.
48 of them are 20 years old. Now I am just selecting all people like
select * from persons
this will get me all persons, but 20 yr old are not the first 48 people.
I need the 20 yr old to be first 48 in 113 results.
something like
20 year ols ( 48 of them ), after that ..... all the rest in the table
How can I query this using PostgreSQL.
EDIT : there are age less than 20 too. after getting the first 48 , 20 yr olds, I dont care rest of the order I am getting the 48 to 113 people.
Just use order by :
select *
from persons
order by age
You can use asc or desc but because default is asc you do not need to put it in your example.
select *
from persons
order by age desc
After the comment from OP here is the new code(I do not know why but my firs assumption was that the value 20 is the lowest possible value... bad assumption):
select *
from persons
order by case when age = 20 then 1 else 2 end
OR
select *
from persons
order by (age = 20) desc
Here is a demo
If 20 is not your minimum age, you can use the CASE statement inside the ORDER BY clause, like this:
SELECT
*
FROM
persons
ORDER BY
CASE WHEN age = 20 THEN 0
ELSE 1
END ASC
I have a table as UserData which has strucure like
id category value
1 AR 100
2 WT 90
3 WT 12
4 AR 1000
5 AR 2005
6 WT 122
7 BP 112
8 BP 18
now I want to select all rows which has maximum value in the indiviual category. so my result set should be.
id category value
5 AR 2005
6 WT 122
7 BP 112
I want to have this in both MongoDB and SQL server query.
In SQL I tried this
select id,category,value from
(select id,
category,
value,
max(value)
over (partition by category) result
from UserData ) a
where a.result=a.value order by a.id
this is giving me desired result but somehow I feel that it is not good
so I want a better solution for this in SQL and corresponding equivalent solution for MongoDB
I'm not sure about a mongo solution, but if you don't care about Id - here is the way to get the highest value for a category.
SELECT DISTINCT
Category, MAX(Value)
FROM UserData
GROUP BY category
Excuse me if this is more simplistic than what you desire.
I have a SQL Server table called pluginProspects and I have the following columns..
FirstName
LastName
Email
AmonutOfEmployees
AnnualRevenue
In this table I have thousand of rows, and I wanted to create a SQL statement that allows me to retrieve rows 200 - 300 or even rows 300 - 400 or w/e I want.
I use to use this following code which worked for a while, BUT I want to add specific WHERE clause and its not showing me hundred results, its showing me like 50 or 70 or w/e depending on the results from those specific row numbers
SELECT *
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY demoID DESC) AS rownum,
demoID, RecordStatus, Email, FirstName, LastName, AmountOfEmployees,
AnnualRevenue
FROM
pluginProspects) AS Salaries1
WHERE
rownum BETWEEN 1 * {STARTNUMBERHERE} - 100 AND 1 * {STARTNUMBERHERE}
AND (RecordStatus = '2014 Lead')
AND (AmonutofEmployees > 10)
ORDER BY
AmountOfEmployees DESC
As you can see {STARTNUMBERHERE} I could put 100, 200, 300, 400 etc to create a paging effect which worked perfectly fine UNTIL I added (AmountofEmployees > 10)
Well in my table there tons of prospects that have less than 10 employees, so this will sometimes only show 10 or 20 results, instead of the first 100 results.
Anyone know the correct way I should be doing this? No I can't use a stored procedure for what I'm trying to do, it has to be pure SQL based
Matt,
Max meant to change it to look like this
SELECT *
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY demoID DESC) AS rownum,
demoID, RecordStatus, Email, FirstName, LastName, AmountOfEmployees,
AnnualRevenue
FROM
pluginProspects
WHERE (RecordStatus = '2014 Lead')
AND (AmonutofEmployees > 10) ) AS Salaries1
WHERE
rownum BETWEEN 1 * {STARTNUMBERHERE} - 100 AND 1 * {STARTNUMBERHERE}
ORDER BY
AmountOfEmployees DESC
I have a query that calculates points based on multiple criteria, and then orders the result set based on those points.
SELECT * FROM (
SELECT
dbo.afunctionthatcalculates(Something, Something) AS Points1
,dbo.anotherone(Something, Something) AS Points2
,dbo.anotherone(Something, Something) AS Points3
,[TotalPoints] = dbo.function(something) + dbo.function(something)
) AS MyData
ORDER BY MyData.TotalPoints
So my first stab at adding placement, rankings.. was this:
SELECT ROW_NUMBER() OVER(MyData.TotalPoints) AS Ranking, * FROM (
SELECT same as above
) AS MyData
ORDER BY MyData.TotalPoints
This adds the Rankings column, but doesn't work when the points are tied.
Rank | TotalPoints
--------------------
1 100
2 90
3 90
4 80
Should be:
Rank | TotalPoints
--------------------
1 100
2 90
2 90
3 80
Not really sure about how to resolve this.
Thank you for your help.
You should use the DENSE_RANK() function which takes the ties into account, as described here: http://msdn.microsoft.com/en-us/library/ms173825.aspx
DENSE_RANK() instead of ROW_NUMBER()
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 2 years ago.
I'm working with a table that has about 50 colums and 100,000 rows.
One column, call it TypeID, has 10 possible values:
1 thourgh 10.
There can be 10,000 records of TypeID = 1, and 10,000 records of TypeID = 2 and so one.
I want to run a SELECT statement that will return 1 record of each distinct TypeID.
So something like
TypeID JobID Language BillingDt etc
------------------------------------------------
1 123 EN 20130103 etc
2 541 FR 20120228 etc
3 133 FR 20110916 etc
4 532 SP 20130822 etc
5 980 EN 20120714 etc
6 189 EN 20131009 etc
7 980 SP 20131227 etc
8 855 EN 20111228 etc
9 035 JP 20130615 etc
10 103 EN 20100218 etc
I've tried:
SELECT DISTINCT TypeID, JobID, Language, BillingDt, etc
But that produces multiple TypeID rows of the same value. I get a whole bunch of '4', '10', and so on.
This is an ORACLE Database that I'm working with.
Any advise would be greatly appreciated; thanks!
You can use ROW_NUMBER() to get the top n per group:
SELECT TypeID,
JobID,
Language,
BillingDt,
etc
FROM ( SELECT TypeID,
JobID,
Language,
BillingDt,
etc,
ROW_NUMBER() OVER(PARTITION BY TypeID ORDER BY JobID) RowNumber
FROM T
) T
WHERE RowNumber = 1;
SQL Fidle
You may need to change the ORDER BY clause to fit your requirements, as you've not said how to pick one row per TypeID I had to guess.
WITH RankedQuery AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY TypeID ORDER BY [ordercolumn] DESC) AS rn
FROM [table]
)
SELECT *
FROM RankedQuery
WHERE rn = 1;
This will return the top row for each type id, you can add an order by if you want a specific row, not just any.