tsql

SQL help - exception report

烈酒焚心 提交于 2021-01-29 16:02:17
问题 I have a company table (CompanyID, CompanyName), Date Table (Datekey int, date, isTradingHoliday 0/1), Fact table (id, datekey, companyID, StockClosePrice) I need an exception report. Expectation is for all the companies, for all trading days, there will be a StockClosePrice. I need to find for which date and for which companies on the trading days I don't have data. So basically, I need help to write a query to find for year 2019 for which days and for which companies I don't have data in

SQL UPDATE and CASE statement does not work

情到浓时终转凉″ 提交于 2021-01-29 15:09:07
问题 I have the following table "Sales" in MS SQL Server, where [FieldX] is not updated properly: (..[My comment] is of course not an actual column in the table) The reason why the last 3 rows should be = 3, is because I am updating the fields with the following rules: with cte_previous_rows AS ( SELECT Staff_Id, LAG(FieldX) OVER (partition by Staff_Id ORDER by [date]) as Prev_Row FROM Sales ) UPDATE Sales SET FieldX = (CASE WHEN Staff_id_sales < 1500 AND ClosedSale = 0 THEN 0 WHEN Staff_id_sales

Calculate time at specific Timezone, from UTC Time

本小妞迷上赌 提交于 2021-01-29 14:45:16
问题 I want to calculate Datetime at given timezone based on Datetime in UTC. I thought that I can do it with the following: DECLARE @timeUTC DATETIME = '2019-01-01 10:00:00' SELECT @timeUTC AS timeUTC, @timeUTC AT TIME ZONE 'Central European Standard Time' as at_time_zone_offset, CONVERT(datetime, @timeUTC AT TIME ZONE 'Central European Standard Time',1) at_timezone_convert -- OUTPUT ---timeUTC |at_time_zone_offset |at_timezone_convert ---2019-01-01 10:00:00.000 |2019-01-01 10:00:00.000 +01:00

SQL Server Calculate during 07H00 and 23H00 during weekdays

淺唱寂寞╮ 提交于 2021-01-29 11:48:23
问题 I have a dataset I am retrieving from DevOps Warehouse but the report readers want to only report on the following: Time calculated should only be between 07:00 & 23:00 and Monday to Friday. I have tried a few blogs but all of it calculated a ridiculous amount of minutes Start Date: 2019-08-19 06:05:30.483 End Date : 2019-08-19 08:13:59.357 Total Minute : 20177 Simple DateDiff : 128 My Current T-SQL query looks like: ;WITH cte AS ( SELECT System_Id, StartDate, EndDate, DATEADD(d, Number,

How do I transpose a result set and group by week?

家住魔仙堡 提交于 2021-01-29 09:00:27
问题 I have a view based on query: SELECT CONVERT(VARCHAR(10), date, 103) AS date, eventid, name, time, pts FROM results WHERE DATEPART(yy, date) = 2019; This provides a data set such as this: Date EventID Name Time Points 24/04/2019 10538 Fred Flintstone 22:27 10 24/04/2019 10538 Barney Rubble 22:50 9 24/04/2019 10538 Micky Mouse 23:17 8 24/04/2019 10538 Yogi Bear 23:54 7 24/04/2019 10538 Donald Duck 24:07 6 01/05/2019 10541 Barney Rubble 21:58 10 01/05/2019 10541 Fred Flintstone 22:00 9 01/05

T-SQL - Loss of float precision when using procedures

此生再无相见时 提交于 2021-01-29 08:52:32
问题 I have been trying to create a T-SQL procedure where I sum all of the family's monthly income into a variable @prihodki and at the same time, I sum all of the family's monthly expense into a variable @odhodki . Printing a variable inside this procedure outputs the correct answer, however when I try to execute the procedure and store the returned value into a variable, the precision gets lost. CREATE PROCEDURE izracunajPromet_TaMesec AS BEGIN SET NOCOUNT ON; DECLARE @prihodki float, @odhodki

Query SQL to get all column and min max datetime by MSSQL

a 夏天 提交于 2021-01-29 07:21:11
问题 I'm using Microsoft SQL Server. And Here is my table Added sample data format text as table ╒══════════╤═════════════╤══════════╤═══════╤═════════════╤═════════════════════╤══════════════════╤══════════════════════╤══════════╤═════════════╤═════╤════════════╤════════════╤════════════╤══════════╕ │ RecordID │ ID Employee │ Resource │ Shift │ ProjectID │ Drawing No │ Production order │ PN │ Quantity │ ProductName │ BNo │ Start Date │ Start Time │ End Date │ End Time │ ╞══════════╪═════════════╪

TSQL “Illegal XML Character” When Converting Varbinary to XML

早过忘川 提交于 2021-01-29 07:20:18
问题 I'm trying to create a stored procedure in SQL Server 2016 that converts XML that was previously converted into Varbinary back into XML, but getting an "Illegal XML character" error when converting. I've found a workaround that seems to work, but I can't actually figure out why it works, which makes me uncomfortable. The stored procedure takes data that was converted to binary in SSIS and inserted into a varbinary(MAX) column in a table and performs a simple CAST(Column AS XML) It worked fine

How to update multiple rows in a temp table with multiple values from another table using only one ID common between them?

筅森魡賤 提交于 2021-01-29 07:00:22
问题 I am trying to reconcile the IDs in a temp table (top) from another DB table (bottom). Since I only have one ID that's common between the two, I am only getting the top result for all the rows (ReconGlobalRemunerationGrantID) in my temp table. I am aiming to get each of the unique ID and update my temp table as such. Right now, my update query is simple and I update using the ID common between the 2 tables. Is there a function or another command statement I can use to get the result intended?

Converting logic of DateTime.FromBinary method in TSQL query

拥有回忆 提交于 2021-01-29 06:05:07
问题 I have a table that contain column with VARBINARY(MAX) data type. That column represents different values for different types in my c# DB layer class. It can be: int , string and datetime . Now I need to convert that one column into three by it's type. So values with int type go to new column ObjectIntValue and so on for every new column. But I have a problems with transmitting data to datetime column, because the old column contains datetime value as a long received from C# DateTime.ToBinary