|
|
|
|
|
|
|
Question of the Day |
Today's question (by Kathi Kellenberger): | |
Calculate the Days Between First and Last Orders | |
Your SQL Server 2017 database has a Sales table that contains CustomerID, OrderID, and OrderDate columns. You would like to return a list of CustomerIDs along with the difference in days between the very first order the customer placed and the most recent order. Which query will give you that answer? Query #1
SELECT CustomerID, DATEDIFF(DAY,LAG(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID), OrderDate) AS OrderSpanDays FROM Sales;Query #2 SELECT CustomerID, DATEDIFF(DAY,OrderDate, LEAD(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID)) AS OrderSpanDays FROM Sales;Query #3 SELECT DISTINCT CustomerID, DATEDIFF(DAY,FIRST_VALUE(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), LAST_VALUE(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)) AS OrderSpanDays FROM Sales;Query #4 SELECT DISTINCT CustomerID, DATEDIFF(DAY,FIRST_VALUE(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID), LAST_VALUE(OrderDate) OVER(PARTITION BY CustomerID ORDER BY OrderID)) AS OrderSpanDays FROM Sales; | |
Think you know the answer? Click here, and find out if you are right. |
Yesterday's Question of the Day (by Steve Jones - SSC Editor) |
SQLCMD OS Commands I am writing a SQLCMD script in SSMS with SQLCMD mode. I want to execute an operating system command to perform an action, like make a directory. How do I format this command in my script? Answer: Precede the command with !! Explanation: Two exclamation points (!!) are used to designate OS commands. These commands are passed to cmd.exe. Ref: Edit SQLCMD Scripts with Query Editor - https://docs.microsoft.com/en-us/sql/ssms/scripting/edit-sqlcmd-scripts-with-query-editor?view=sql-server-ver15 |
Database Pros Who Need Your Help |
Here's a few of the new posts today on the forums. To see more, visit the forums. |
SQL Server 2017 - Administration |
Space issue on a table - All, I am working on implementing table partitioning on a existing table. The table has clustered columnstore index. I have to drop the index and recreate it to use the partition scheme The table size before dropping the index is 200 GB and after dropping the index the table size is 4 TB Do […] |
SQL Server 2017 - Development |
Insert New Rows in sql based on difference between 2 dates - Hi, I want to create a new records based on the difference between Start and End dates when StartDate is not NULL . The New value created can be first of every month . I need this field for reporting purpose to get a count of employees my month . CREATE TABLE #MonthsYearBetwenDates […] |
View runs slower - I have the following query SELECT DateADD(day,9-DATEPART(dw,cdata .call_datetime ),cast(cdata .call_datetime as date) ) as effective_date , A.EmployeeID AS employee_id , COUNT(cdata .Extension) AS metric_value --, ISNULL(MS.metric_id,'') AS metric_id FROM ( SELECT EmployeeID FROM ad.it_activedirectorydata WHERE [Enabled] = 'True' ) A INNER JOIN [vw_exampl] cdata ON A.EmployeeID =cdata .employeeID INNNER JOIN tbl2 ON A.employyeid=tbl2.employeeid GROUP BY […] |
SQL Server 2016 - Development and T-SQL |
Update parent row from child row that has partial data. - On the same table I have table rows that are broken from the import and went to the next row. I need to bring it back to the parent row. For example ID 1 Col3 supposed to have the value 'GHI' but the 'I' went to the 2nd row or ID 2 Col1 cell. Same […] |
Parallel Load thousands csv Files - Hi, what is the fastest way to load thousands of csv files with the same structure into an OnPrem SQL DB (staging) - day by day? A parallelization would be ideal. These files have the customer number in the FileName (SSIS, Bulk Insert, Stored Proc., Data Factory, partitioning ...): Thanks Regards Nicole |
Administration - SQL Server 2014 |
AWS instance storage - We are planning to use one of AWS instance for sql server and wanted to know if the instance storage drives can be encrypted for storing PCI and PII data. Coudn't find info on AWS documents. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html |
SQL 2012 - General |
Replication causing log bloating - Hello, We've had a replication process in place for several years. It seems like every couple of years, there would seem to be some sort of hiccup where our transaction log would start to bloat like crazy. When I queried the sys.databases table to look at the log_reuse_wait_desc, "REPLICATION" would be the value. So we […] |
How to import Excel with XML - Hi, I have an Excel sheet that I have to import into SQL Server. It just has 3 columns, with one that looks like XML to me. Now I will admit I am not an expert with XML. The columns are something like this: Note: The column with Acct Info below has what I believe […] |
SQL Server 2012 - T-SQL |
Recursive CTE - Batches - Hi Folks I'm trying to group rows into batch and adding batch to query to fetch data from system. I know there is lot of questions why it is required. I'm trying fetch data from Oracle SQL developer. The issue is last row of batch is repeating in next batch. I added query for better […] |
XQuery - Use column value to display correct node - Hi All First time with XQuery and despite trying to read as many guides as I can, I'm hitting a bit of a mental block. We have a form design application that stores templates and details of the questions that relate to each template in relational tables. The attributes for the template and questions are […] |
Fastest way to load a CSV file in SQL Server Table - What is the fastest way to load a file in SQL Server Table ? I have large CSV Files ( 3-4 GB ) which I load through BULK INSERT. I find this slow compared to BCP. I am unable to load it using BCP as the data types are not supported ( INT,VARCHAR, etc) Please […] |
SQL Server 2019 - Administration |
OLE DB 0x80004005 connection error trying to run SQL HA Maintenance Plans - Hi, I'm getting a connection error when I try to run even the most basic plan, fails with scheduled or manual runs. The user is both a local-admin on all 3 nodes, and a SQL SA member. All engine processes and agent processes run-as that same AD account too. Everything works great (failovers etc), just […] |
COHESITY for Backup/Restore? - Any one have any experience with using Cohesity as the backup/restore platform instead of doing native SQL backups? With TSM, we always did a SQL backup, and then swept that up to tape. In our new backup system based on Cohesity, my backup admins want to backup up the database and log files directly from […] |
SSDT |
VS 2019 - Schema Compare - Ignore Fill Factor Not Working - Got a strange one with VS2019 and a DB Schema Compare. When comparing two tables on ServerA to ServerB, it is throwing up a difference for the fill factor being 80/90 which is right. However in my options I have "Ignore Fill Factor" enabled so I shouldn't be seeing these differences. As we testing different […] |
Integration Services |
SSIS compoenents - For SSIS to pickup any dll componenet where do we need to deploy? some dlls should not go to GAC. in that case what is default localtion for SSIS to pick up? |
| |
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved. webmaster@sqlservercentral.com |