Structured Query Language (SQL). For questions about the language, not Microsoft SQL Server (tag sql-server).

learn more… | top users | synonyms (1)

0
votes
1answer
19 views

PostgreSQL, how to query the database to retrieve the percentile elements of a dataset?

I need a PostgreSQL query that do the following job: given a percentage (e.g. 10%), the query should return each 1st element of each percentile, among all the elements selected. So suppose in this ...
2
votes
2answers
121 views

How to get data from three different tables

This question is continuing of this issue: How to get data from two different tables? Now I need to connect the table Sessions with 2 tables: Input Downloads When I use this query with real ...
-1
votes
0answers
29 views

Question Regarding Full outer join and excluding null values from tables [closed]

How to remove null value after joining 3 tables using full outer join?
6
votes
2answers
121 views

Selecting Distinct Rows but Counting All Rows

I have the following table: ---------------------------------------------- | ID | interestingData | timestamp | ---------------------------------------------- | 1 | 400 | ...
3
votes
1answer
34 views

Howto combine UNION with EXISTS?

I have a query that looks like this: SELECT PubKey, Title FROM Publication UNION SELECT NoteKey, Title, FROM Note Which works fine. My trouble start when I try to add an EXIST to it: SELECT ...
4
votes
2answers
61 views

How to trim first digit off an integer value?

i have data like Column1, i want to convert the Column1 data as Column2. Can any one suggest to me how i can achieve this in SQL? Column 1 Column2 1000001 1 1000002 2 1000003 ...
1
vote
1answer
55 views

Data structure design for supporting arbitrary number of columns in table or database [closed]

I am currently working on a sort-of a meta-modeler to build a free web service so that people can input data and run several models on that data. The task I am currently struggling is: user needs to ...
8
votes
2answers
84 views

SQL Server 2014 Sudden Slow Down / No inserts/ Not related to hardware or index

I'm running a 350GB database on my PC with ~40 million rows. SQL Server 2014, Win7, AMD 8350 @ 4.8GHZ, 16 GB of RAM, and a 500 GB SSD (database is hosted on it's own 500 GB SSD, with a ...
0
votes
0answers
46 views

Index Maintenance taking long time - Primary Key (Clustered Index) Varchar(35) [closed]

I have a table in my database that contains around 70 Million rows. Unfortunately the database designer had made the primary key (Clustered Index) column of type : Varchar(35). Obviously this is ...
2
votes
1answer
18 views

SQL Always on Availability LSN Query

Please can somebody explain how the following works: Scenario: Two nodes in always on availability group configuration (all databases). If I have a backup job on node 1 to backup Full and Log ...
3
votes
1answer
141 views

How to get data from two different tables? [closed]

I have these two MySQL tables: tab1: +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | ...
0
votes
1answer
42 views

I don't want to have the error when I drop a table although the table doesn't exist [closed]

I am using Oracle 12c and I am not interesting to have an error while droping my table 'CONTINENT' in case it doesn't exist. I did this set echo on set serveroutput on alter session set ...
0
votes
0answers
31 views

How can I display values as columns dynamically using postgresql

I have a data table containing finance data about some filings like: filing_id | custom_label | value ------------------------------------ 987345 | cash | 567 987345 | ...
0
votes
2answers
65 views

Can Oracle 11g Express Edition Work on Parallels

I am using a Mac and need to use Oracle 11g. I have Parallels installed with Windows 8.1 on it. Do I need to download anything else or just download Oracle 11g Express Edition and run it?
1
vote
1answer
36 views

Postgres CTE query for a single row not optimized

From my database I am trying to get a weighted sum of a user's stats, and I will only be querying the table one or two users at a time, so I wrote it as a view. Since it's a view, I pretend to ...
0
votes
1answer
37 views

difference between assingment and : [closed]

what is the difference between these two ,second is arithmetic expression assigne to sal , first one is ? how they differ exactly. if salary has intial value of 10 both result same answer. when to ...
0
votes
1answer
30 views

Restore from SQL Server 2014 Enterprise to BI

Is it possible to restore a SQL Server 2014 Enterprise database with table partitioning (Production) to a SQL Server 2014 BI Version (Test/Development) ? Of course I don't expect to use the table ...
1
vote
1answer
27 views

How to get particular object from jsonb array in PostgreSQL?

I have a field called 'user' that holds a json array that roughly looks like this: "user": [{ "_id" : "1", "count" : "4" }, { "_id" : "3", "count": "4"}] Now I want a query like: select count ...
-2
votes
1answer
35 views

I have created a zip file using 7z for backup but its taking time to open [closed]

I have created a zip file using 7z for backup but its taking time to open. Any possibility of making fast. Please suggest me.
3
votes
1answer
23 views

Postgres reduce by key if key is not null

I need to select N rows with seq > X which reduce_key is null and for each row group with the same reduce_key select the one with the biggest seq. Result should be strictly sequential (but with ...
1
vote
1answer
36 views

Return Distinct Records by Category Based Multiple Column Values

ID MODEL MODELSTATUS CONTROL 11 100 GOOD XN 24 100 TRENDING BF 33 101 GOOD XN 46 102 BAD BF 50 103 BAD ...
7
votes
1answer
160 views

Data Warehouse: How can i query daily snapshots?

I have some snapshots of a database that are not timeseries. For example: Snapshot day 1: +----+---------------+------------+------------+ | ID | Title | Category | Date | ...
1
vote
0answers
80 views

Unexpected SYSDATE behavior

I have a SQL trigger which adds the old value ,new value and time of update for column based on update, insert, update or delete. To update the time we used SYSDATEand inserted record from trigger. ...
1
vote
1answer
44 views

Why does this PostGIS query take so long to run?

I am new to PostGIS and SQL in general. I am running a query on polygons which, I hope, only selects distinct geometries and then repair (ST_MakeValid()) those distinct geometries. Thereby removing ...
1
vote
1answer
48 views

Surrogate key resolution, or what am I missing in the Surrogate v Natural argument?

I've read a lot from both camps in the surrogate vs. natural key debate. What I am struggling to understand, and what seems to be glossed over, especially in explaining how surrogate keys are more ...
0
votes
1answer
40 views

function return data type?

create or replace function Total_sal return varchar2 is Total number; begin select sum(salary)into total from employees; return total; end; / BEGIN DBMS_OUTPUT.PUT_LINE(TOTAL_SAL); END; ...
2
votes
1answer
27 views

Return top 100 words from strings

Not sure this is even possible but I was wondering if there is any way of using PostgreSQL to return the top 100 most occurring words from a database column containing strings of varying lengths? The ...
0
votes
0answers
7 views

Use array operators inside postgresql function

I'm trying to create a postgresql. But I keep having an error ERROR: operator does not exist: integer[] <@ integer Hint: No operator matches the given name and argument type(s). You might ...
0
votes
1answer
21 views

PostgreSQL 9.4: Index Only Used For Lower INET Ranges

For some reason queries on the high range of are extremely slow and queries on the low range are extremely fast. My index is an adapted version of the answer in Optimizing queries on a range of ...
0
votes
1answer
62 views

Why are correlated subqueries sometimes faster than joins in Postgres?

This relates to the dataset described in Postgres is performing sequential scan instead of index scan I've started working on adapting the import logic to work with a more normalised schema – no ...
3
votes
1answer
69 views

Subquery in MS Query doesn't work

I'm a beginner in SQL. I hope my question is not too stupid. I'm trying to write a query in MS Query and want to see the results in Excel. The problem is that MS Query doesn't accept my code. I get ...
-1
votes
1answer
18 views

in postgres how to select items that have a dupe across two columns

My data looks like this: queryable_type queryable_id User 1 User 2 User 2 I'd like to return dupes so that my result would look like this: queryable_type ...
5
votes
1answer
63 views

How to improve this union to ensure consistent results?

I've been trying to find an reliable and economic way of counting rows over a 7 day period and so far, this is the best I've been able to come up with: Schema CREATE TABLE IF NOT EXISTS "data" ( ...
0
votes
1answer
50 views

Is there a way to used grouped data multiple times in a query

Suppose a table contains employee id, department name and salary. I want to select the department that gives total salary greater than average salary given by all departments. The solution that I ...
1
vote
1answer
37 views

Return Bit Value based on previous records by Category

How would I return TRUE or FALSE if four of the last six records failed for a given MAKEID? So there has to be 4 failures (or FALSE records) in the last six MAKEID FAILURES CREATEDATE 101 ...
0
votes
0answers
14 views

Query Postgres table by Block Range Index (BRIN) identifier directly

I have N client machines. I want to load each of machine with distinct partition of BRIN index. That requires to: create BRIN with predefined number of partitions - equal to number of client ...
1
vote
1answer
30 views

How to combine 2 SQL statements into one?

I have these 2 SQL statements. Each year, I need to add the new one for the new DB. How can I write it so it will just work for the new years instead of me manually editing this each year? Is there a ...
0
votes
1answer
28 views

Scrapy data analyzed with R, mongoDB or other pipeline? [closed]

I'm scraping data using the Scrapy library in python and i'm planning to make few analysis in the future using R. In the future it could be a very big database with millions of items, what are the ...
4
votes
2answers
71 views

Get 2 Most Recent Records By Product

I am stumped. I need to return a TRUE or FALSE by ProductId if the 2 most recent records created are 'pass' and the LEVEL values are different. PRODUCTID CREATEDATE STATUS LEVEL ABC ...
0
votes
3answers
22 views

Set Value Based on Number of Records with Certain Values using SQL Server

I am stuck on how to return a bit value based on records with certain values. Query needs to return TRUE or FALSE if there are three different Levels of the same Control: Example data: PRODUCT ...
-1
votes
2answers
43 views

how to Create A SP with table variable and execute 3 SP statements in the same statement

how to Create A SP with table variable and execute 3 SP statements in the same statement . exec [dbo].[sp_KPI_Churn_MTD_Current_Year] exec [dbo].[sp_KPI_Churn_MTD_Previous_Year] exec ...
2
votes
0answers
16 views

TLS 1.2 with SQL Server 2008 [duplicate]

I have a VM where I use SQL Server 2008 as a black-box data store for a product I work with. I'm being told by my IT police that my VM is not (or will not be) secure in a short time unless I can ...
4
votes
3answers
424 views

Avoid double query on the same table

I have two queries which are doing basically the same thing, but have different grouping, the first query (query 1) is used to populate a chart, and the second to populate a table. Query 1: SELECT ...
7
votes
2answers
228 views

Shrinking a SQL Server Transaction Log

I’m currently having to deal with a SQL Server transaction log that has got out of control. Disclaimer: I’m not a dba and this isn’t my area of expertise so please bear with me. Currently I have a ...
2
votes
1answer
62 views

PostgreSQL 9.4 query seems very slow

I'm experiencing a very long response time for a query returning a relatively small number of rows. It takes over three minutes to return ~1.3 million rows. I would assume it was an indexing problem ...
2
votes
4answers
125 views

ERROR: check constraint is violated by some row

I've got the following error: ERROR: check constraint "cc_at_least_one_mapping_needed" is violated by some row Query: ALTER TABLE integrations.tax_aggregates DROP COLUMN IF EXISTS myob_id, ADD ...
0
votes
2answers
60 views

Two SQL servers for one website [closed]

I have a website that is deployed on an environment as follows: a load balance. two web server. two database servers. both of the two database server have the same structure and queries. I found ...
0
votes
3answers
48 views

PostgreSQL - Simple column binding

I have many tables of the same length (number of rows) that were previously ordered by some columns as they were being written on disk. My SELECT is of the form: SELECT t1.id, t1.value, t2.value, ...
0
votes
1answer
41 views

Add ranking to query output

I'm new at programming with PHP/mySQL and I have got a little problem here. This is how my code looks like: SELECT COUNT(*) AS Number , Name FROM table GROUP BY Name ORDER BY Number DESC ...
6
votes
2answers
87 views

m-to-n total participation in SQL

How should I model the following design in SQL? As it's shown, every A must have at least one B, and vice versa. I know I should have three relations: A(a), B(b) and R(a,b), but what about total ...