sql-server

Dividing 2 numbers returns 0

不羁岁月 提交于 2021-02-17 05:57:28
问题 I'm trying to divide 2 counts in order to return a percentage. The following query is returning 0 : select ( (select COUNT(*) from saxref..AuthCycle where endOfUse is null and addDate >= '1/1/2014') / (select COUNT(*) from saxref..AuthCycle where addDate >= '1/1/2014') ) as Percentage Should I be applying a cast? 回答1: I would do it differently, using two sum s: select sum ( case when endOfUse is null and addDate >= '1/1/2014' then 1 else 0 end ) * 100.0 -- if you want the usual 0..100 range

Finding the Median value from a table, Group By Date SQLServer

喜欢而已 提交于 2021-02-17 05:22:10
问题 I have a complicated problem I am trying to solve. Please bear with me and feel free to ask any questions. I am quite new to SQL and having difficulty with this... I need to count the median of a group of values. Now the values are not given in a table. The values are derived from a table based on hourly occurrences grouped by date. Here's the sample table from where data is pooled. CREATE TABLE Table22( Request_Number BIGINT NOT NULL ,Request_Received_Date DATETIME NOT NULL ); INSERT INTO

convert date yyyy-mm-dd to mmm-yy SQL Server 2016

随声附和 提交于 2021-02-17 05:20:25
问题 I want to convert date yyyy-mm-dd (stored as a date format) to mmm-yy format. There are no exact matches in the prior questions on the site. I have tried substrings and convert function, was considering creating a scalar function but its taking me a while and hoping someone has an easy solution. 回答1: You can construct the format using string operations: select left(datename(month, datecol), 3) + '-' + right(datename(year, datecol), 2) Or using format() : select format(datecol, 'MMM-yy') 回答2:

“Package not accessible” error trying to import SQL Server JDBC package in project

霸气de小男生 提交于 2021-02-17 05:13:10
问题 I'm trying to use the SQL Server JDBC package in my Java project. I'm getting a The package com.microsoft.sqlserver.jdbc is not accessible error in my import statement. I've already added the MSSQL JDBC driver to the appropriate classpath (see attached image). I have another project set up exactly the same way that works just fine. The driver used is the same and the import statements match. Here are my import statements: package Main; import java.io.BufferedReader; import java.io

“Package not accessible” error trying to import SQL Server JDBC package in project

早过忘川 提交于 2021-02-17 05:12:13
问题 I'm trying to use the SQL Server JDBC package in my Java project. I'm getting a The package com.microsoft.sqlserver.jdbc is not accessible error in my import statement. I've already added the MSSQL JDBC driver to the appropriate classpath (see attached image). I have another project set up exactly the same way that works just fine. The driver used is the same and the import statements match. Here are my import statements: package Main; import java.io.BufferedReader; import java.io

Stored Procedure with multi value parameter behaving strangely

穿精又带淫゛_ 提交于 2021-02-17 05:10:12
问题 I created a stored procedure in sql server to feed SSRS to allow it to accept Multiple values. I have created it and when I used it in my report or execute it in sql server I have the following error message. Is there anything i am missing? Thanks Msg 207, Level 16, State 1, Line 35 Invalid column name 'London'. This is my sample data. feel free to create table with it DECLARE @MyTables AS TABLE (ID INT, City VARCHAR(100)) INSERT INTO @MyTables VALUES (1,'London'), (2,'Chester'), (3,'Luton'),

Calculate difference using nearest (oldest date) using same column

左心房为你撑大大i 提交于 2021-02-17 04:48:09
问题 I have the (simplified) database table A below which holds both the Mother Batch Purity Value and the Batch Puritys (all batches are made from the previous "Mother Batch". I am trying to figure out how to pull out via SQL the Batch Purity and the Difference between the Batch Purity and the previous Mother Batch. The sample names always remain the same, but can be numerous batch numbers Table A Sample Name | Purity | Date (DD/MMM/YY) ---------------+-----------+----------------- Mother Batch |

Sum column up to the current row in SQL?

不想你离开。 提交于 2021-02-17 03:40:08
问题 I'm trying to sum a column up to the current row (in SQL Server). How do I do this? select t1.CounterTime, t1.StartTime, t1.EndTime, isNull(t1.value, 0) as value1, -- How do I make Total1 the sum of t1.value over all previous rows? sum( isNull(t1.value, 0) ) over (partition by t1.CounterTime order by t1.CounterTime) as Total1 from SomeTable t1 order by t1.CounterTime But I got the partition by wrong... ╔═══╦═════════════════════════╦═════════════════════════╦═════════════════════════╦════════

Is there in SQL a way to enforce unicity of undirected edge?

不问归期 提交于 2021-02-17 03:21:35
问题 create table Location ( id integer primary key(1, 1), latitude decimal(8,6), longitude decimal(9,6), address varchar(100), name varchar(60) unique ); create table Journey ( id integer primary key(1,1), id_from integer foreign key references Location(id), id_to integer foreign key references Location(id), name varchar(100) unique, unique(id_from, id_to) ); With this schema, you could create 2 different journeys for a pair of locations, one for the way in and one for the way back. What I want

Pyspark connection to the Microsoft SQL server?

大憨熊 提交于 2021-02-17 02:47:40
问题 I have a huge dataset in SQL server, I want to Connect the SQL server with python, then use pyspark to run the query. I've seen the JDBC driver but I don't find the way to do it, I did it with PYODBC but not with a spark. Any help would be appreciated. 回答1: Please use the following to connect to Microsoft SQL: def connect_to_sql( spark, jdbc_hostname, jdbc_port, database, data_table, username, password ): jdbc_url = "jdbc:sqlserver://{0}:{1}/{2}".format(jdbc_hostname, jdbc_port, database)