Skip to content

Instantly share code, notes, and snippets.

@brianberns
Last active May 18, 2021 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianberns/67d574411988da8d68fe4329d7d89508 to your computer and use it in GitHub Desktop.
Save brianberns/67d574411988da8d68fe4329d7d89508 to your computer and use it in GitHub Desktop.
open System
open System.Linq
open FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq.RuntimeHelpers
type Db = SqlDataConnection<"Server=.;Database=QueryTest;Trusted_Connection=true">
let d = Db.GetDataContext()
type Result =
{
pkey : int
lphasekey : Nullable<int>
orgk : int
time : int
}
let test vDate bDate =
let q =
query {
for row1 in d.Table1 do
where (row1.Date >= vDate)
where (row1.Date <= bDate)
join row2 in d.Table2 on
(row1.Table2Key = row2.Table2Key)
let key = AnonymousObject<int,int,Nullable<int>>(row1.Table3Key,row1.ProjectTableKey,row2.ProjectPhaseKey)
where row2.ProjectPhaseKey.HasValue
groupValBy row1 key into g
select
{
pkey = g.Key.Item2
lphasekey = g.Key.Item3
orgk = g.Key.Item1
time = g.Sum (fun x -> x.Data)
}
}
for value in q do
printfn "%A" value
[<EntryPoint>]
let main argv =
test
(DateTime.Parse "5/1/2021")
(DateTime.Parse "6/1/2021")
0
USE [master]
GO
/****** Object: Database [QueryTest] Script Date: 5/18/2021 3:37:20 PM ******/
CREATE DATABASE [QueryTest]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'QueryTest', FILENAME = N'D:\Data\SQL Server\Databases\QueryTest.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'QueryTest_log', FILENAME = N'D:\Data\SQL Server\Databases\QueryTest_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [QueryTest] SET COMPATIBILITY_LEVEL = 140
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [QueryTest].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [QueryTest] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [QueryTest] SET ANSI_NULLS OFF
GO
ALTER DATABASE [QueryTest] SET ANSI_PADDING OFF
GO
ALTER DATABASE [QueryTest] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [QueryTest] SET ARITHABORT OFF
GO
ALTER DATABASE [QueryTest] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [QueryTest] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [QueryTest] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [QueryTest] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [QueryTest] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [QueryTest] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [QueryTest] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [QueryTest] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [QueryTest] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [QueryTest] SET DISABLE_BROKER
GO
ALTER DATABASE [QueryTest] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [QueryTest] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [QueryTest] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [QueryTest] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [QueryTest] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [QueryTest] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [QueryTest] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [QueryTest] SET RECOVERY FULL
GO
ALTER DATABASE [QueryTest] SET MULTI_USER
GO
ALTER DATABASE [QueryTest] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [QueryTest] SET DB_CHAINING OFF
GO
ALTER DATABASE [QueryTest] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [QueryTest] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [QueryTest] SET DELAYED_DURABILITY = DISABLED
GO
EXEC sys.sp_db_vardecimal_storage_format N'QueryTest', N'ON'
GO
ALTER DATABASE [QueryTest] SET QUERY_STORE = OFF
GO
USE [QueryTest]
GO
/****** Object: Table [dbo].[Table1] Script Date: 5/18/2021 3:37:20 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table1](
[Table1Key] [int] NOT NULL,
[Date] [datetime] NOT NULL,
[Table2Key] [int] NOT NULL,
[Table3Key] [int] NOT NULL,
[ProjectTableKey] [int] NOT NULL,
[Data] [int] NOT NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[Table1Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Table2] Script Date: 5/18/2021 3:37:20 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table2](
[Table2Key] [int] NOT NULL,
[ProjectPhaseKey] [int] NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[Table2Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Table1] WITH CHECK ADD CONSTRAINT [FK_Table1_Table2] FOREIGN KEY([Table2Key])
REFERENCES [dbo].[Table2] ([Table2Key])
GO
ALTER TABLE [dbo].[Table1] CHECK CONSTRAINT [FK_Table1_Table2]
GO
USE [master]
GO
ALTER DATABASE [QueryTest] SET READ_WRITE
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment