Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Using auto-increments to group data in a query

$
0
0

Morning chaps and chapettes,

Just working on a bit of SQL to feed a crystal report, but I can't get it to behave the way I'd like.

At the moment, I have this little snippet:

SQL
SELECTol.OrderLineID,isnull(olp2.SectionStartOrderLineID,olp.SectionStartOrderLineID)FROMOrderLineolWITH(NOLOCK)LEFTJOINOrderLinePriceInfoolpWITH(NOLOCK)ONol.OrderLineID=olp.OrderLineIDLEFTJOINOrderLinePriceInfoolp2WITH(NOLOCK)ONol.CreatedFromOrderLineID=olp2.OrderLineIDWHEREol.OrderID=69524ORDERBYol.LineNumber

The idea is that it returns an OrderLineID, then checks to see which section it belongs to. If it doesn't belong to a section, it checks the OrderLineID it was originally created from (CreatedFromOrderLineID) and checks whetherthatbelonged to a section.

What you end up with is a set of results like so:

Text
OrderLineID...

Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>