Skip to content

Cheap Windows Hosting ASP.NET

ASP.NET Hosting Cheap | Faster

X
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact
Menu

SQL Server 2014 Tutorial :: Make Report Easily

Posted on May 12, 2016June 16, 2016 by Mary Bacine

SQL Server 2014 Tutorial – Cheap Windows Hosting ASP.NET | In this article, I want to show you how to easily making report in SQL Server 2014. Before that, we need a list of users and the most recent widget each user has created. We have a users table and a widgets table, and each user has many widgets. users.id is the primary key on users, and widgets.user_id is the corresponding foreign key in widgets.

SQL Server 2014 Tutorial :: Make Report Easily

To solve this problem, we need to join only the first row. There are several ways to do this. Here are a few different techniques and when to use them.

SQL Server 2014 Tutorial

Use Correlated Subqueries when the foreign key is indexed

In SQL Server 2014, correlated subqueries are subqueries that depend on the outer query. It’s like a for loop in SQL. The subquery will run once for each row in the outer query:

select * from users join widgets on widgets.id = (
select id from widgets
where widgets.user_id = users.id
order by created_at desc
limit 1
)

Notice the where widgets.user_id = users.id clause in the subquery. It queries the widgets table once for each user row and selects that user’s most recent widget row. It’s very efficient if user_id is indexed and there are few users.

SQL Server 2014 Tutorial – Use a Complete Subquery when you don’t have indexes

In SQL Server 2014, correlated subqueries break down when the foreign key isn’t indexed, because each subquery will require a full table scan.

In that case, we can speed things up by rewriting the query to use a single subquery, only scanning the widgets table once:

select * from users join (
select distinct on (user_id) * from widgets
order by user_id, created_at desc
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id

This new subquery returns a list of the most recent widgets, one for each user. We then join it to the users table to get our list.

We’ve used Postgres’ DISTINCT ON syntax to easily query for only one widget peruser_id.

SQL Server 2014 Tutorial – Use Nested Subqueries if you have an ordered ID column

In this SQL Server 2014 tutorial, the most recent row always has the highest id value. This means that even without DISTINCT ON, we can cheat with our nested subqueries like this:

select * from users join (
select * from widgets
where id in (
select max(id) from widgets group by user_id
)
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id

We start by selecting the list of IDs repreenting the most recent widget per user. Then we filter the main widgets table to those IDs. This gets us the same result as DISTINCT ON since sorting by id and created_at happen to be equivalent.

Use Window Functions if you need more control

If your table doesn’t have an id column, or you can’t depend on its min or max to be the most recent row, use row_number with a window function. It’s a little more complicated, but a lot more flexible:

select * from users join (
select * from (
select *, row_number() over (
partition by user_id
order by created_at desc
) as row_num
from widgets
) as ordered_widgets
where ordered_widgets.row_num = 1
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id
order by users.id

The interesting part is here:

select *, row_number() over (
partition by user_id
order by created_at desc
) as row_num
from widgets
over (partition by user_id order by created_at desc

specifies a sub-table, called a window, per user_id, and sorts those windows by created_at desc.row_number() returns a row’s position within its window. Thus the first widget for each user_id will have row_number 1.

In the outer subquery, we select only the rows with a row_number of 1. With a similar query, you could get the 2nd or 3rd or 10th rows instead.

 

HostForLIFE.eu SQL 2014 Hosting
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.

SQL Server 2014 Tutorial :: Make Report Easily

 

Posted in Hosting TutorialTagged best europe sql 2014 hosting, best uk sql 2014 hosting, cheap europe sql 2014 hosting, cheap uk sql 2014 hosting, free mssql 2014 hosting, recommendation sql 2014 hosting, recommendation sql 2014 hosting europe, sql 2014 hosting austria, sql 2014 hosting belgium, sql 2014 hosting brazil, sql 2014 hosting canada, sql 2014 hosting denmark, sql 2014 hosting finland, sql 2014 hosting france, sql 2014 hosting germany, sql 2014 hosting greece, sql 2014 hosting hungary, sql 2014 hosting india, sql 2014 hosting ireland, sql 2014 hosting israel, sql 2014 hosting italy, sql 2014 hosting kuwait, sql 2014 hosting luxemburg, sql 2014 hosting netherlands, sql 2014 hosting norway, sql 2014 hosting poland, sql 2014 hosting portugal, sql 2014 hosting qatar, sql 2014 hosting russia, sql 2014 hosting saudi arabia, sql 2014 hosting south africa, sql 2014 hosting spain, sql 2014 hosting sweden, sql 2014 hosting switzerland, sql 2014 hosting turkey, sql 2014 hosting uk, sql 2014 hosting usa, sql 2014 hosting. europe sql 2014 hosting, uk sql 2014 hosting

Post navigation

Reliable ASP.NET MVC 5 Hosting :: Low Cost Unlimited Packages
Best & Cheap ASP.NET 5 Hosting in India – WindowsASPNetHosting

Related Post

  • Benefits of Social Marketing
  • ASP.NET MVC 6 Tutorial :: Handle Multiple Submit Buttons On The Same Form in MVC 6
  • ASP.NET Tutorial :: Remove XML Formatter from ASP.NET Web API Applications
  • ASP.NET MVC 6 Tutorial :: Migrate ASP.NET Web API 2 To MVC 6
  • ASP.NET 4.5 Tutorial – Timeout in ASP.NET 4.5 With Regex
  • SQL Server 2016 Tutorial :: Create a JSON Document Using SQL Server 2016
  • SQL Server 2014 Tutorial :: Make Report Easily ASP.NET 5 Hosting Tutorial :: How to Make an Empty Application?
  • Hosting Tutorial :: Multilingualism in ASP.NET MVC 4

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Best SQL 2016 Hosting

Categories

  • ASP.NET
  • ASP.NET Ajax
  • ASP.NET MVC
  • Entity Framework
  • Hosting Comparison
  • Hosting Review
  • Hosting Tutorial
  • IIS
  • Joomla
  • nopCommerce
  • Silverlight
  • SQL Server

Recent Posts

  • A2 Hosting vs ASPHostPortal ASP.NET Hosting – Who is Your Favourite?
  • Looking for A2 ASP.NET Hosting Alternatives?
  • Windows Dedicated Server with HostForLIFE.eu. Is it Good?
  • Let’s Move From Ionos ASP.NET Hosting
  • Choosing Your Best ASP.NET Core Hosting Provider: ASPHostPortal.com VS GoDaddy in Comparison

Our Partner

  • Best ASP.NET Hosting
  • Best Windows Hosting
  • Cloud ASP.NET Hosting
  • ECommerce Hosting Review
  • Recommended ASP.NET
  • Reliable Windows Hosting
  • Review ASP.NET Hosting
  • Windows Hosting Bulletin
  • Windows Hosting Leaders
  • Windows ASP.NET Hosting
  • Windows Hosting Review
  • Best Cheap Hosting ASP.NET
  • Hosting Review ASP.NET
  • Full Trust Hosting ASP.NET
  • Cheap Australia ASP.NET Hosting
  • Review Core ASP Hosting
  • Cheap Australia ASP.NET Hosting
  • Best Cloud ASP.NET Hosting
  • Creative Videos
  • Business Vendor
  • Reliable ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • European ASP.NET Hosting
  • UK ASP.NET Hosting
  • ASP.NET Hosting Review
  • India ASP.NET Hosting
  • Best India ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • Best ASP.NET Hosting Review
  • Easy Hosting ASP.NET
  • Cheap Windows Hosting
  • Reliable ASP.NET Hosting Review
Created by cheapwindowshostingasp.net
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact