-1

I have the following code:

IF (OBJECT_ID('tempdb..#Data') IS NOT NULL)
BEGIN
    DROP TABLE #Data
END

SELECT
    t.Name, x.Time, x.Date, x.Total,
    xo.DrvCommTotal, x.Name2, x.Street, x.Zip,
    r.Route1
INTO 
    #Data
FROM 
    table1 xo WITH(NOLOCK)
LEFT JOIN  
    Table2 t WITH(NOLOCK) ON t.ID = x.ID
LEFT JOIN 
    Route1 r ON r.RouteID = x.RouteID
WHERE 
    x.Client = 1
    AND x.Date = '9/13/2018'
GROUP BY 
    t.Name, x.Time, x.Date, x.Total, xo.DrvCommTotal, x.Name2,
    x.Street, x.Zip, r.Route1
ORDER BY 
    Route1

 SELECT DISTINCT 
     F.*, F2.NumOrders
 FROM 
     #Data F
 LEFT JOIN 
     (SELECT 
          Route1, COUNT(*) NumOrders
      FROM 
          #Data
      GROUP BY 
          Route1) F2 ON F2.Route1 = F.Route1
 LEFT OUTER JOIN 
     (SELECT 
          Street + ',' + Zip Stops, Time, RouteN1
      FROM 
          #Data
      GROUP BY 
          RouteNo1, street, Zip) F3 ON F3.Route1 = F.Route1
WHERE 
    F.Route1 IS NOT NULL
ORDER BY 
    F.Route1

and it provides me with a list of routes and stops. The column NumOrders lets me know how many orders are on each route. I need the stops to become individual columns I will label Stop1, Stop2, etc. so that each route is only one row and all the information is contained on the row for one route.

I'm currently using the temp table because the data is so large. I can play with my SELECT statement without having to re-run the entire code.

How do I move the stops for each route into columns?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user974061
  • 343
  • 1
  • 5
  • 17
  • Are you ok with dirty reads? What about randomly getting duplicate and/or missing rows? What about rows where some columns are correct and other columns aren't? If you answered NO to any of those you should stop using that NOLOCK hint. https://blogs.sentryone.com/aaronbertrand/bad-habits-nolock-everywhere/ – Sean Lange Sep 13 '18 at 16:17
  • 1
    For the question at hand we need some details. [Here](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) is a great place to start. – Sean Lange Sep 13 '18 at 16:17
  • Hi. You want to pivot. This is a faq. Eg google your title. Please always google many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using one variant search for your title & keywords for your tags. See [ask] etc & the downvote arrow mouseover text. – philipxy Sep 13 '18 at 18:18
  • Possible duplicate of [Convert Rows to columns using 'Pivot' in SQL Server](https://stackoverflow.com/q/15931607/3404097) – philipxy Sep 13 '18 at 18:29

1 Answers1

0

Hum.. Not quite sure I understand the question but it sounds that you want to pivot the data so that the routes break into columns. If so, I would use a sql Pivot. Here is an example from the documentation:

USE AdventureWorks2014;  
GO  
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5  
FROM   
(SELECT PurchaseOrderID, EmployeeID, VendorID  
FROM Purchasing.PurchaseOrderHeader) p  
PIVOT  
(  
COUNT (PurchaseOrderID)  
FOR EmployeeID IN  
( [250], [251], [256], [257], [260] )  
) AS pvt  
ORDER BY pvt.VendorID; 

Also, here is the link to how to use pivot: https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017

Since you already have all the data in your temp table, you could pivot that on the way out.

camba1
  • 1,795
  • 2
  • 12
  • 18
  • If you don't understand the question then you should not answer it, you should comment asking for clarification & flag/vote to close as unclear when you have the rep. – philipxy Sep 13 '18 at 18:17
  • @philipxy , thanks for the comment and fair enough, I should have asked him to clarify. However, given the information he provided, I did my best to try to help him. If I had been wrong oh well, I wasted my time and so did him. If I had been right, he would have been on his way to fix his issue. So, I threw the dice with an informed guess. – camba1 Sep 14 '18 at 19:58
  • Beware of downvotes. Questions that should be closed--including for being unclear--should not be answered. Answering unclear questions leads to messy posts as questions & answers morph & comments rack up. You can google 'site:[meta.se]' re answering unclear questions. [If the question is not clear, is it bad practice to still attempt an answer?](https://meta.stackexchange.com/q/222116/266284) PS Your answer is so general (partly because the question is unclear) that it is essentially the word "pivot". That is a comment. So it is not very 'useful'--see the answer arrow mouseover texts. [answer] – philipxy Sep 14 '18 at 20:56