Udostępnij przez


Samouczek: podróż w czasie za pomocą języka T-SQL w usłudze Warehouse

In this tutorial, learn how to use T-SQL statements to time travel in a warehouse table. Time travel means to query data as it existed at a specific point in time, which is made automatically possible by Fabric Warehouse data retention.

Note

This tutorial forms part of an end-to-end scenario. Aby ukończyć ten samouczek, należy najpierw wykonać następujące samouczki:

  1. Tworzenie obszaru roboczego
  2. Tworzenie magazynu
  3. Ingest data into a Warehouse

Praca z zapytaniami dotyczącymi podróży w czasie

W tym zadaniu dowiesz się, jak utworzyć widok 10 pierwszych klientów według sprzedaży. Użyjesz widoku w następnym zadaniu, aby uruchomić zapytania dotyczące podróży w czasie.

  1. Ensure that the workspace you created in the first tutorial is open.

  2. On the Home ribbon, select New SQL query.

    Zrzut ekranu przedstawiający opcję Nowe zapytanie SQL na wstążce.

  3. In the query editor, paste the following code. Kod tworzy widok o nazwie Top10Customers. The view uses a query to retrieve the top 10 customers based on sales.

    --Create the Top10Customers view.
    CREATE VIEW [dbo].[Top10Customers]
    AS
    SELECT TOP(10)
        FS.[CustomerKey],
        DC.[Customer],
        SUM(FS.[TotalIncludingTax]) AS [TotalSalesAmount]
    FROM
        [dbo].[dimension_customer] AS DC
        INNER JOIN [dbo].[fact_sale] AS FS
            ON DC.[CustomerKey] = FS.[CustomerKey]
    GROUP BY
        FS.[CustomerKey],
        DC.[Customer]
    ORDER BY
        [TotalSalesAmount] DESC;
    
  4. Uruchom zapytanie.

  5. Po zakończeniu wykonywania zmień nazwę zapytania na Create Top 10 Customer View.

  6. In the Explorer pane, from inside the Views folder for the dbo schema, verify that the Top10Customers view exists.

    Zrzut ekranu przedstawiający okienko Eksplorator z wyróżnionym nowo utworzonym widokiem.

  7. Utwórz nowe zapytanie do obsługi zapytań dotyczących podróży w czasie.

  8. In the query editor, paste the following code. The code updates the TotalIncludingTax value for a single fact row to deliberately inflate its total sales. Pobiera również bieżący znacznik czasu.

     --Update the TotalIncludingTax for a single fact row to deliberately inflate its total sales.
     UPDATE [dbo].[fact_sale]
     SET [TotalIncludingTax] = 200000000
     WHERE [SaleKey] = 22632918; --For customer 'Tailspin Toys (Muir, MI)'
     GO
    
     --Retrieve the current (UTC) timestamp.
     SELECT CURRENT_TIMESTAMP;
    

    Note

    Obecnie można używać tylko strefy czasowej uniwersalnej koordynowanej (UTC) na potrzeby podróży czasowej.

  9. Uruchom zapytanie.

  10. Po zakończeniu wykonywania zmień nazwę zapytania na Time Travel.

  11. In the Results pane, notice the timestamp value (your value will be the current UTC date and time).

    Screenshot of the Results pane, highlighting the timestamp value to copy.

  12. To retrieve the top 10 customers as of now, in a new query editor, paste the following statement. The code retrieves the top 10 customers by using the FOR TIMESTAMP AS OF query hint.

     --Retrieve the top 10 customers as of now.
     SELECT *
     FROM [dbo].[Top10Customers]
     OPTION (FOR TIMESTAMP AS OF 'YOUR_TIMESTAMP');
    
  13. Zmień nazwę zapytania na Time Travel Now.

  14. Wróć do zapytania Time Travel, a następnie skopiuj wyniki zapytania za pomocą polecenia Copy.

    Screenshot of the Copy command, highlighting Copy Query results.

  15. Return to the Time Travel Now query, and then replace YOUR_TIMESTAMP with the timestamp you copied to the clipboard.

  16. Uruchom zapytanie i zwróć uwagę, że druga górna wartość CustomerKey wynosi 49 dla Tailspin Toys (Muir, MI).

  17. Zmodyfikuj wartość znacznika czasu na wcześniejszą , odejmując jedną minutę od znacznika czasu.

  18. Ponownie uruchom zapytanie i zwróć uwagę, że druga górna wartość CustomerKey wynosi 381 dla Wingtip Toys (Sarversville, PA).

Tip

Aby uzyskać więcej przykładów podróży w czasie, zobacz Jak: Przeprowadzać zapytania z użyciem podróży w czasie na poziomie instrukcji.

Next step