<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create View SQL in Data Management Questions</title>
    <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386457#M22167</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Christian,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks so much for your reply.&amp;nbsp; I still cannot get PIVOT to work, but it must be because I am not getting the SQL statement right and/or that my date field is a date.&amp;nbsp; I ended up getting the results I wanted by first creating one view for each cycle:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SELECT RouteId AS Route1, last_edited_date AS Cycle1&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;FROM View_Test&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;WHERE Cycle=1 AND Status=5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;GROUP BY RouteId, last_edited_date&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then I joined them together in a single view:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SELECT C1.Route1, C1.Cycle1, C2.Cycle2, C3.Cycle3&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;FROM (C1 LEFT JOIN C2 ON C1.Route1 = C2.Route2)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;LEFT JOIN C3 ON C1.Route1 = C3.Route3&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It works, but I am sure there is a more elegant way to do this in a single view for someone more proficient in SQL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 23 Oct 2015 17:04:16 GMT</pubDate>
    <dc:creator>MichelleD</dc:creator>
    <dc:date>2015-10-23T17:04:16Z</dc:date>
    <item>
      <title>Create View SQL</title>
      <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386455#M22165</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am using New &amp;gt; View to try to create a database view in SQLExpress.&amp;nbsp; I want to pivot the table like it works in this Access query:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: times new roman,times;"&gt;TRANSFORM First(Leaf.last_edi_1) AS FirstOflast_edi_1&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: times new roman,times;"&gt;SELECT Leaf.RouteId&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: times new roman,times;"&gt;FROM Leaf&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: times new roman,times;"&gt;GROUP BY Leaf.RouteId&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: times new roman,times;"&gt;PIVOT Leaf.Cycle;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tried making something similar work in with SQL to create a database view with no luck.&amp;nbsp; I can get the RouteId as rows, but can't figure out how to make the 10 cycles show up as columns with the last_edited_date filled in - it always errors on Pivot.&amp;nbsp; Can I not use Pivot in Esri and/or SQLExpress?&amp;nbsp; If not, is there a work around to transpose the table with Create View? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Oct 2015 22:24:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386455#M22165</guid>
      <dc:creator>MichelleD</dc:creator>
      <dc:date>2015-10-22T22:24:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create View SQL</title>
      <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386456#M22166</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Michelle,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is a small sample I put together with your outline:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;--Create Leaf table
create table leaf (ROUTEID INT, CYCLE VARCHAR(10), LAST_EDIT DATE);

--Insert records
INSERT INTO LEAF VALUES (1, 'CYCLE1', '10/23/2015');
INSERT INTO LEAF VALUES (1, 'CYCLE2', '1/23/2015');
INSERT INTO LEAF VALUES (1, 'CYCLE3', '3/23/2015');
INSERT INTO LEAF VALUES (2, 'CYCLE1', '5/23/2015');
INSERT INTO LEAF VALUES (2, 'CYCLE2', '12/23/2015');
INSERT INTO LEAF VALUES (2, 'CYCLE3', '7/23/2015');

--Create the select statement with a pivot
SELECT ROUTEID, CYCLE1, CYCLE2, CYCLE3 FROM 
(SELECT ROUTEID, CYCLE, LAST_EDIT FROM LEAF) LEAF_PIV 
PIVOT (MAX(LAST_EDIT) FOR CYCLE IN (CYCLE1, CYCLE2, CYCLE3)) PIV&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Results:&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Pivot.jpg" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/132916_Pivot.jpg" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In addition, this query does work for me in ArcMap/ArcCatalog if I cast the dates to varchar. What field types do you have for your last_edit field?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:45:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386456#M22166</guid>
      <dc:creator>ChristianWells</dc:creator>
      <dc:date>2021-12-11T17:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create View SQL</title>
      <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386457#M22167</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Christian,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks so much for your reply.&amp;nbsp; I still cannot get PIVOT to work, but it must be because I am not getting the SQL statement right and/or that my date field is a date.&amp;nbsp; I ended up getting the results I wanted by first creating one view for each cycle:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SELECT RouteId AS Route1, last_edited_date AS Cycle1&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;FROM View_Test&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;WHERE Cycle=1 AND Status=5&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;GROUP BY RouteId, last_edited_date&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then I joined them together in a single view:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SELECT C1.Route1, C1.Cycle1, C2.Cycle2, C3.Cycle3&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;FROM (C1 LEFT JOIN C2 ON C1.Route1 = C2.Route2)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;LEFT JOIN C3 ON C1.Route1 = C3.Route3&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It works, but I am sure there is a more elegant way to do this in a single view for someone more proficient in SQL.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Oct 2015 17:04:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386457#M22167</guid>
      <dc:creator>MichelleD</dc:creator>
      <dc:date>2015-10-23T17:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: Create View SQL</title>
      <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386458#M22168</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Michelle, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does the sample I provided work for you in SQL Server Management Studio?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you want to share your data as a csv or dbf file, I can take a look at it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Christian&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Oct 2015 17:08:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386458#M22168</guid>
      <dc:creator>ChristianWells</dc:creator>
      <dc:date>2015-10-23T17:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create View SQL</title>
      <link>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386459#M22169</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Christian,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am joining the View to my Routes layer with the cycle dates as columns so I can show a list of all the dates each cycle was completed (Status = 5) for a route on a single pop-up in a web map.  Everything works with the three test cycles I set up as described (one view for each cycle where Status=5 joined into a single view), and I am hoping performance isn’t impacted when I add in the remaining seven cycles.  I have attached a dbf if you think there is a better way to set up the View and want to take a look at it.  Otherwise I will use my 10 + 1 view solution, in which case I will mark your first response as the correct answer since it worked for you (I have only been working in ArcCatalog and may not have gotten that PIVOT query just right, especially adding in the status condition).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Michelle&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Oct 2015 18:27:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/create-view-sql/m-p/386459#M22169</guid>
      <dc:creator>MichelleD</dc:creator>
      <dc:date>2015-10-23T18:27:07Z</dc:date>
    </item>
  </channel>
</rss>

