Questions tagged [sql]

Structured Query Language (SQL) is a language for querying databases. Questions should include code examples, table structure, sample data, and a tag for the DBMS implementation (e.g. MySQL, PostgreSQL, Oracle, MS SQL Server, IBM DB2, etc.) being used. If your question relates solely to a specific DBMS (uses specific extensions/features), use that DBMS's tag instead. Answers to questions tagged with SQL should use ISO/IEC standard SQL.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
9 views

SQL pivot of large amount of records takes a very long time

In the current test, I expect to generate 1000 rows (ie: Line ID) with about 80 or so columns. I have various tables where each record that are linked together by a Line ID. I need to use dynamic ...
HelloPuppy's user avatar
0 votes
0 answers
11 views

How do I get these fields to be ordered by the same way in SSRS?

I have two fields "Role Code Old Value" and "Role Code New Value" both of these fields as you can see in the screenshot have the same values. The issue is that they are in a ...
BiGeek's user avatar
  • 21
-1 votes
1 answer
28 views

Get the row if there is Date column NULL otherwise get with Max date

I have a MS SQL table with Badge, name and Date columns. The Badge column does not have unique values. I want to fetch unique Badge values such that if the Date is NULL, then it should pick that Badge....
Jay.'s user avatar
  • 309
-3 votes
0 answers
11 views

how to create row level security sql-based on foundry slate app? [closed]

I'm working on a solution on Foundry Slate that revolves around a relatively sensitive data dataset and I need an RLS approach to guarantee the privacy of this data. How can I implement this in the ...
Diogo Pires's user avatar
0 votes
0 answers
16 views

Troubleshooting LlamaIndex SQL Query: Incorrect 'No Record Found' Response for Existing Data

I'm working on a Python project using LlamaIndex with SQLAlchemy to query a MySQL database. The project involves executing natural language SQL queries using the NLSQLTableQueryEngine from LlamaIndex. ...
Gatso90's user avatar
0 votes
1 answer
19 views

SQL / NetSuite: CASE (expr1) WHEN (> condition1) THEN outcome1 ... how to make it work?

(NetSuite) I'm doing a transaction search and if the amount of gallons for last trailing twelve months is within certain ranges of contract compliance, then apply scaled liquidated damages. We have at ...
BernieW's user avatar
0 votes
1 answer
32 views

SQL GROUP BY while selecting the latest record + extra conditions

I am trying to figure out what would be the most efficient way of achieving the following: I have two tables: word_translations(id, word_id, language_id) and translations(id, text, ...
Adam's user avatar
  • 1,084
0 votes
3 answers
34 views

Pulling records with same value in multiple columns

I have a sub-table like the following. It contains 14 fields of true/false values, and the Status field is 0 if all values for Status 1-14 is false. DCID Status Status1 Status2 Status3 1 0 false ...
d.sweigart's user avatar
0 votes
0 answers
21 views

How to generate 4-4-5 retail calendar in Dbt / Snowflake SQL

I am using DBT, I need to generate a 4-4-5 calendar for year 2021 to year 2025 to join with my weekly data drop. Is there a way to write sql (with macro if necessary), or import some package to ...
sweetlemon's user avatar
0 votes
2 answers
33 views

SQL, random number

I am using MariaDB, version 11.1.2, client 15.2 for OSX10.17 (x86_64). I can generate a random number using: SELECT FLOOR(RAND() * 50) + 1; When run it will generate a random number between 1 and 50. ...
SPlatten's user avatar
  • 5,386
0 votes
0 answers
55 views

How to do SQL recursive logic

I have this sample dataset like this ; acctid payseq totalamount acctnum expectprice payorder aac1 99 1000.00 1111aa 800.00 1 aac1 99 1000.00 1111bb 800.00 2 aac1 99 1000.00 1111cc 800.00 3 aac1 ...
Vikraman's user avatar
0 votes
0 answers
21 views

SQL WHERE clause doesn't work with arrays

I currently using a tool named retool to query data from my bigquery instance. The problem is quite a few of the field names use arrays and I'm having bit of an issue filter field names with arrays in ...
TayOnTech's user avatar
0 votes
1 answer
40 views

Comparing dates from 2 columns minus one position on one of the columns

I am trying to achieve the following in: DATE_2 column 1.The first result I always take it from DATE_1 column " 25-JUN-18", then I have to do a comparison. if DATE_1, rn 2, 25-JUN-18 > ...
Bob's user avatar
  • 23
-1 votes
1 answer
31 views

Calculate consecutive days a user log in per calendar date

I need to calculate the number of consecutive logins for each of my user_id for each calendar_date using Databricks SQL. If I can't I could go for pyspark as well. user_id calendar_date ...
karek77's user avatar
  • 37
2 votes
1 answer
17 views

how to find current month of last year in amazon athena query?

for example, If today is 2023-09-23 (yyyymm) then I need to extract data from 2022 september to 2023 september (2022-09-01 to 2023-09-30). FOR BELOW CODE, My intention was to print data from 2022 ...
dajung Lee's user avatar
0 votes
0 answers
32 views

Is there a SQL pattern to follow for historical tables that expire old records when new records come in?

id ... effect_date expire_date 1 ... 2023-10-23 9999-12-31 1 ... 2023-10-15 2023-10-22 1 ... 2023-10-10 2023-10-14 1 ... 2023-10-01 2023-10-09 I have a history table that has an id, bunch of data ...
Brian Jones's user avatar
-1 votes
2 answers
23 views

SPring JPA can not parse a native SQL Query with a single quote

I am trying to parse this sql query using spring data jpa to use in my application. It is a postgresql qurey using some of the geospatial functions using postgis. This query is basically trying to ...
Soumalya Bhattacharya's user avatar
-1 votes
0 answers
12 views

SQL General Error 1442 althoug I'm making a temporal table to make the update [duplicate]

I have the next table in PhpMyAdmin SQL Table scenes And I try to make a trigger to increse in one the scen_number of the record that have the same proj_id and scen_number of the entered record. Then ...
0 votes
1 answer
33 views

Need to add a default value to a returned SQL SELECT queried list

We have a Kentico 13 page type field with a Drop-down list form control that queries a db table to populate the list. Currently, the list pre-selects the top value, but this won't work because it's ...
DayByDay's user avatar
0 votes
1 answer
20 views

How to find duration in seconds from an interval day to second object in Databricks

In a Databricks SQL query, I am creating a column to get the difference in Timestamps of successive rows. I use something like the following, select (Timestamp - (LAG(Timestamp, 1) OVER (partition ...
MetallicPriest's user avatar
0 votes
2 answers
108 views

Create a sequence of number

I revised my clarification. Considering the search value as 300 and as per the below data already available in table (30000, 80000)--300 will fit(30000-80000=50000) where 300 sequence of numbers are ...
Kar B's user avatar
  • 1
0 votes
0 answers
32 views

Cannot read properties of undefined (reading : 'productName')

I'm encountering a problem when I pass a selectedProductName eg. 'Black Jacket (M)', to my next query. The selectedProductName should get productName from the product table and with that, I'll use ...
Kristian Diaz's user avatar
0 votes
0 answers
16 views

How to remove a CHECK constraint from a column in Interbase 2020

In Interbase (2020) I have a table CATEGORY with a field Category on which a CHECK CONSTRAINT was implemented with: ALTER TABLE CATEGORY ADD CHECK (Category > 999 AND Category < 10000); Now ...
Mark's user avatar
  • 130
-2 votes
2 answers
39 views

Mysql query IN or NOT IN faster?

I have two simple queries below: SELECT * from table_name where status IN (0, 1, 2, 3, 4) SELECT * from table_name where status NOT IN (5, 6) The possible value of status is 0 to 6, and column status ...
Van Tho's user avatar
  • 614
0 votes
2 answers
19 views

Copy value from jsonb column to another nested jsonb column

I have an entity that has, among other things, two jsonb columns : "options" and "features". I want to copy data from a field inside the options column to a two levels deep field ...
FE-P's user avatar
  • 79
0 votes
0 answers
11 views

SQL Convert mixed exchange rates to local currency

I have expenses recorded with Mixed Exchange Rates. "'dbo.fn_KurBul (@DATE, @EXCHANGERATE, 1) @DATE : Current Date @EXCHANGERATE: 0-TURKISHLIRA, 1-DOLLAR, 2-EURO'" With that function. I can ...
Lashekhan's user avatar
-5 votes
0 answers
34 views

SQL Sever query to add prefix along with identity feature [closed]

I am trying to create a table on SQL Server and add one column named CID with identity feature, so that the row could be filled automatically, but also I'm require to add prefix on that number. How do ...
Vijay Kandpal's user avatar
0 votes
1 answer
41 views

Inserting lines with common identifier in a thread-safe way

I created an application that dynamically creates SQL code. This application is supposed to group some production orders that are identified by their barcode and that are consolidated into one pick ...
MikeFUT's user avatar
0 votes
1 answer
17 views

Merge overlapping time frames (min and max) into one and blanket assign to relevant user if detection time criteria is matched

I have a database that records user access. User access is requested and users are given a two hour window to access their space. At the moment, users can request multiple requests, which sometimes ...
ME_2023's user avatar
0 votes
1 answer
29 views

Azure SQL: Error converting data type nvarchar to bigint

A failing piece of code extracted from the procedure: DECLARE @row UNIQUEIDENTIFIER DECLARE @count BIGINT DECLARE @deleteToDate BIGINT DECLARE @deleteToDateYesterday BIGINT DECLARE @DaysMiliseconds ...
Neurobion's user avatar
0 votes
2 answers
49 views

Identify the sequence of missing number in a SQL table

Could someone please help me on write a query to fetch the missing numbers in a sql table column. I have a table: Credit Note with the column "Credit No" Credit No 1 2 4 8 10 15 I ...
Kar B's user avatar
  • 1
0 votes
0 answers
20 views

How to access a value of a dynamic key in memsql

I have a MemSQL database with a table: MyTable primaryId configuration 1 { [keyID]: { status: "open" } } 2 { [keyID]: { status: "closed" } } The key keyID is a dynamic key e.g ...
Lonwabo Rarane's user avatar
1 vote
2 answers
31 views

Timestamp in Oracle

I have created the below table and inserted the 2 records. CREATE TABLE TIME_TEST(tran_data_timestamp timestamp) insert into time_test values('07-DEC-23 12.01.55.000000000 AM'); insert into time_test ...
Ram's user avatar
  • 749
0 votes
0 answers
43 views

SQL distinct on INNER JOIN not on the select

I have a Rails app with ActiveStorage. I have a model Attachment which is shared between all possible "attachable" (Establishment, Event etc). To avoid multi upload of the same file, might ...
brcebn's user avatar
  • 1,603
0 votes
0 answers
21 views

multiple id in jsonb column sql. supabase

I have to tables in my database. One is Workout the other is exercise the structure as the following: exercise_TABLE id | name | description ---+-------+------ 1 | aaa | aaa 2 | bbb | BBB 3 | ...
Shahad Alharbi's user avatar
-1 votes
0 answers
27 views

When using pyspark spark.sql(), do "inner join" and "exists" work differently?

When the same query is obtained through pyspark spark.sql() and pyathena, the result is different. The result obtained through spark.sql() is that there are fewer rows. The query I used is as follows. ...
yoon's user avatar
  • 1
0 votes
0 answers
30 views

How can an alias name be assigned to a database? [closed]

Are there any methods to create an alias for a database similarly to SQL Server aliasing. For SQL server aliasing below link can be referred - https://learn.microsoft.com/en-us/sql/database-engine/...
RD Seeker's user avatar
-1 votes
0 answers
40 views

Select from the database with session clause [closed]

I want to select data from my database when my information is according to data from another database which I have converted into session I.e $dt=year; $sql=" select * FROM student_score where ...
adechian's user avatar
-1 votes
2 answers
43 views

Should I use explode or Unpivot?

I have a table consist of No Contract, Past, Current, and Expired columns. |No.Cont| Past |Current|Expired| +-------+------+-------+-------+ |113 | X | | | |114 | | X | ...
Ichichaa's user avatar
0 votes
1 answer
38 views

get the third last record on sql sort by date in codeigniter

im trying to select an item's third lastest by date record from sales table i have googled the "regular" sql for this. i think it will be like this select sales.DATENAME(month, DATEADD (...
loveearthmix's user avatar
-3 votes
2 answers
76 views

Creating Two Different Data Types for Table

I desire to make a header of table in the columns containing integer data type for giving number each data sequentially and string data type for the header. The table illustrated below No ...
hilda.mathilda2's user avatar
0 votes
0 answers
63 views

Primary key index is not used in join [duplicate]

TL;DR query is making a full table scan on a simple join statement and I don't know why, see the last EXPLAIN output for details I have a use case where I have to find a value closest to 12:00 for ...
eeqk's user avatar
  • 3,594
0 votes
1 answer
46 views

How DO I write a SQL query to show related records in one row

I need an SQL Server query that will list the select rows from the first table and list the related records from the second table on the same row as the first table record. Here is the dataset from ...
Jaime Dolor jr.'s user avatar
0 votes
1 answer
34 views

This join query is only supposed to output 39 rows, but I am outputting 140. What may I be doing wrong that I can do differently? [closed]

I am having a problem where I am wanting to use a join to find what color names are not part of any sets. The expected output is 39 colors, but I output 140 colors. SELECT DISTINCT a.color_name AS [...
SleepyJamie's user avatar
0 votes
2 answers
26 views

Split date range and create record for each year in snowflake

Input |EMP_KEY |START_DT |END_DT | | 1 |12-1-2020 |05-06-2023| |2 |1-1-2022 |12-21-2023| OUTPUT |EMP_KEY |START_DT |END_DT | |1 |12-1-2020 |12-31-2020| |1 |...
user16653488's user avatar
0 votes
4 answers
58 views

Find group and sorting data in laravel

I have two tables, one is products and the other is varients. In product table I store product basic data such as name, image, brand, etc. In varients table I store variants of product such as 1kg, ...
M kumar's user avatar
0 votes
2 answers
47 views

How to group data into time buckets of a specified interval using Clickhouse

I'm trying to write a query, where the data for a certain time range is grouped in groups of x days from the start of the date range. An example would be: daterange : 05/11/2023 to 05/12/2023 So my ...
thecreator232's user avatar
-1 votes
0 answers
35 views

How to fetch data in sql gallery table core php? [closed]

How to fetch data in sql gallery table using recipe_id ,image upload sql databse tbl_recipes_galleryI tried it and it doesn't show any data image 1.tbl_recipes The data show image 2.tbl_recipes use ...
jay raman's user avatar
0 votes
0 answers
24 views

Summing values by time intervals in Presto SQL

I have a data series that has a start and stop timestamp and some large integer values in different columns. The goal is to sum the integer values by minute when that minute falls between start and ...
Richard's user avatar
1 vote
4 answers
69 views

Create an alternative ID in query Oracle SQL

I am trying to add a column in a SQL query to use as an alternative ID. The data has this format: UserID | Value --------------- 1 | 23 2 | 10 1 | 45 I'd like to create another column ...
Rickerz's user avatar
  • 59

1
2 3 4 5
13358