sql

SQL Server: is it possible to get data from another SQL server without setting linked server?

蹲街弑〆低调 提交于 2021-02-16 20:47:31
问题 I need to do the following query (for example): SELECT c1.CustomerName FROM Customer as c1 INNER JOIN [ExternalServer].[Database].[dbo].[Customer] as c2 ON c2.RefId = c1.RefId For some security reason my client doesn't allow me to create a linked server. The user under whom I execute this query has access to both tables. Is it possible to make it work without using linked server? Thanks. 回答1: You could use OPENROWSET, which'll require the connection info, username & password... While I

Hourly sum of values

眉间皱痕 提交于 2021-02-16 20:22:46
问题 I have a table with the following structure and sample data: STORE_ID | INS_TIME | TOTAL_AMOUNT 2 07:46:01 20 3 19:20:05 100 4 12:40:21 87 5 09:05:08 5 6 11:30:00 12 6 14:22:07 100 I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID. I tried the following query but i don't know if it's correct. SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302 WHERE MINUTE(INS_TIME) <=59 GROUP BY HOUR,STORE_ID ORDER BY INS_TIME; 回答1: SELECT STORE_ID, HOUR(INS_TIME) as

Hourly sum of values

霸气de小男生 提交于 2021-02-16 20:22:08
问题 I have a table with the following structure and sample data: STORE_ID | INS_TIME | TOTAL_AMOUNT 2 07:46:01 20 3 19:20:05 100 4 12:40:21 87 5 09:05:08 5 6 11:30:00 12 6 14:22:07 100 I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID. I tried the following query but i don't know if it's correct. SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302 WHERE MINUTE(INS_TIME) <=59 GROUP BY HOUR,STORE_ID ORDER BY INS_TIME; 回答1: SELECT STORE_ID, HOUR(INS_TIME) as

Arithmetic overflow error converting expression to data type int for basic statistics

社会主义新天地 提交于 2021-02-16 20:18:16
问题 I am trying to do a basic query that calculates average, min, max, and count. SELECT MIN(column) as min, MAX(column) as max, AVG(column) as avg, count(*) as count FROM database.dbo.table; Where column is of type int , and the database is azure sql standard. and in return, i get an error: "Arithmetic overflow error converting expression to data type int" Why is int getting a casting-to-int problem in the first place? and is there something I can add that will make this work on all numerical

MySQL code to convert Excel datetime

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-16 20:15:47
问题 Excel's datetime values look like 42291.60493, which means MySQL sees them as strings and not as dates. Is there a MySQL code that can convert them to MySQL datetime? (i.e. like in MS SQL) 回答1: I can think of 2 solutions: Convert your dates within excel to a formatted date string that conforms to mysql's date and time format using text() function within excel. Convert the number using calculation to date within mysql: (the expression below may be simplified) select date_add(date_add(date(

How to do SQL's GROUP BY using PHP?

◇◆丶佛笑我妖孽 提交于 2021-02-16 20:13:27
问题 I'd like to SELECT rows from a database table and group them using PHP instead of SQL based on a parameter (in this case by item). SQL: Clothes table id item owner 1 shoes joe 2 pants joe 3 hat joe 4 pants joe 5 hat tom SELECT * from Clothes where owner='joe' 1 shoes joe 2 pants joe 3 hat joe 4 pants joe Here's how I'd like the results to look after using PHP instead of SQL's GROUP BY item PHP : 1 shoes joe 2 pants joe //count 2 3 hat joe I'm sure there is a PHP array function for this I'm

SQL Subtract Column Values Based on Second Column Value with Group By Statement

Deadly 提交于 2021-02-16 20:01:25
问题 I have an SQL statement that looks so: select FY_CD, PD_NO, INPUT, SUM(HOURS) from LABOR_TABLE group by PD_NO, INPUT; Returning this: FY_CD|PD_NO| INPUT | HOURS 2008 1 Actuals 61000 2008 1 Baseline 59000 2008 2 Actuals 54000 2008 2 Baseline 59000 2008 3 Actuals 60000 2008 3 Baseline 70000 I'm trying to figure out how to subtract the Actual values from the Baseline values for each period, returning the following: FY_CD|PD_NO| INPUT | HOURS 2008 1 Variance 2000 2008 2 Variance -5000 2008 3

SQL - Operand data type datetime2 is invalid for subtract operator

微笑、不失礼 提交于 2021-02-16 20:00:07
问题 I want to subtract the value of the first row from the value of the second row of column _timestamp (shown below). _number is the ordering column in my data. and put the result in a new column called diff . I have tried it with the following query use dbtest select t2._number, t2._timestamp, coalesce(t2._timestamp - (select t1._timestamp from dbo.tcp t1 where t1._number = t2._number + 1), t2._timestamp) as diff from dbo.tbl t2 but I am getting the following error. Msg 8117, Level 16, State 1,

Why is fputcsv producing duplicate columns?

送分小仙女□ 提交于 2021-02-16 19:06:26
问题 I am adding a download button to a WordPress site that will query our database, and offer up the result as a CSV. Everything works, except the CSV produced has a duplicate column for each column it returns. We have checked the SQL query and it does not have duplicates. Here's how we are generating the CSV: $rows = //Call to SQL query function $fp = fopen('php://output', 'w'); fputcsv($fp, array_keys($rows)); foreach ($rows as $row) { fputcsv($fp, $row); } $filename = "EventResults.csv";

Why is fputcsv producing duplicate columns?

不羁岁月 提交于 2021-02-16 19:00:46
问题 I am adding a download button to a WordPress site that will query our database, and offer up the result as a CSV. Everything works, except the CSV produced has a duplicate column for each column it returns. We have checked the SQL query and it does not have duplicates. Here's how we are generating the CSV: $rows = //Call to SQL query function $fp = fopen('php://output', 'w'); fputcsv($fp, array_keys($rows)); foreach ($rows as $row) { fputcsv($fp, $row); } $filename = "EventResults.csv";