Suppose, I have a composite primary key that consists of 3 columns: [ShardKey], [SiteId] and [ServiceId]. Column [ServiceId] is an identity column and should start from 1 for a new combination of [ShardKey] and [SiteId], but SQL server fails – the column [ServiceId] never starts from 1, it just incremen…
Tag: sql
Selecting variables as individual rows in table [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 2 y…
Are the table indexes used in the select on the resultset of a subquery in the FROM clause?
I have a table in the SQL Server with more than 50 millions rows, so I have some compound indexes in my table. Indexes with 3, 4 columns. There is a situation where I really need to use a subquery in the FROM clause. example of the structure( the index is: column2, column3, column4 ): The example is only to
MySql Query to get certain number of values from limit and order by rand()
In below SQL query I need 25 rows containing product = ‘y’ and 25 rows having product = ‘n’. Product is a column having Y/N value only. In limit of random 50 rows I need 25 rows from each values. Kindly assist. I have tried Union but it didn’t work. Answer Simply use UNION for ea…
Problem when trying to create a table on SQL DEVELOPER [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 2 years ago. Improve this question I was trying to create a table on SQL Developer, and I had the error …
Create A shift summary, with a shift spanning over 2 days
I have a data set like the sample below. I need to query and summarize the number of transactions per shift. The problem is that the shift cycle does not align with the normal hours of a day. Eg. The shift for day 2020-01-01 will start at 06:00 AM on 2020-01-01 and end at 06:00 AM on 2020-01-02 Below are
SQL selecting most recent row inside join
I have 2 tables companies and invoices I want to select all companies with their most recent invoice price. I don’t seem to get it working. This is what I tried: But the query loads very long and I don’t know why. The structure looks like this: companies invoices The BC_ID is the same as the compa…
Running total of positive and negative numbers where the sum cannot go below zero
This is an SQL question. I have a column of numbers which can be positive or negative, and I’m trying to figure out a way to have a running sum of the column, but where the total cannot go below zero. I have tried a number of different window functions on this, but haven’t found a way to prevent t…
Overlap the intervals using mysql
+——+————+————+ | id | start_date | end_date | +——+————+————+ | 1 | 2019-01-01 | 2019-01-12 | | 1 | 2019-01-10 | 2019-01-27 | | …
SELECT with a variable
I have 3 tables : t_object : With all the classes t_operation : With all the operations, link with t_object.Object_ID = t_operation.Object_ID t_operationtag : With all the tag value for each operation, link with t_operation.OperationID = t_operationtag.ElementID I try to get an list for each of my operation (…