Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
2
votes2
answers6341
viewsHow to get last Quence on Oracle?
I tried 3 different ways, to make one select of the last sequence of my table, but none worked. SELECT SEQ_ID_PAIS.CURRVAL FROM DUAL; Error: ORA-08002: a sequência SEQ_ID_PAIS.CURRVAL ainda não foi…
-
2
votes2
answers569
viewsSplit a query
I need some help from you guys, i have a query on my system, which has 4 cases SELECT TOP 3 * FROM ( SELECT Title, SUM(Count) AS Count FROM ( SELECT CASE WHEN EstadoId = EstadoIdAutor AND EstadoId =…
-
2
votes2
answers807
viewsHow to model n-to-n relationship correctly? Women also have friendship with men and not only men have friendship with women
Hello, I want to do the following: Create in Mysql the n-to-n relationship where men have friendships with women and women also have friendships with men. Example: John has friendship with Fernanda,…
-
2
votes1
answer214
viewsLinked SQL Server and Firebird: Error 'Must declare the scalar variable'
I made a Linked of my SQLSERVER with the FIREBIRD. But when I pass a variable inside the query to return the amount of record I get the error: Message 137, Level 15, Status 1, Line 1 Must declare…
-
2
votes4
answers593
viewsSql - Query of two maximum values
Suppose we have this database codigo, data, valor,seq (1, '2014-01-01', 10,2), (1, '2014-01-02', 20,1), (2, '2014-01-03', 30,1), (2, '2014-01-05', 40,1), (2, '2014-01-05', 50,2), (3, '2014-01-06',…
-
2
votes1
answer417
viewsPerform calculation in SQL command
I performed a command of select on my table estoque where I bring the supply model, your current amount and what is your minimum amount in stock, for that I made the query as follows: SELECT…
-
2
votes1
answer2008
viewsSQL Server - Split Function
I have the following table: The goal is to make, in SQL Server, a function Split (by ',') the column EixoX where the TipoGrafico is different from 'Stockchart'. That is, if the TipoGrafico is…
-
2
votes1
answer1510
viewsMerge the output of two Select’s - Postgresql commands
My need is to merge the result of two select commands so that I get everything back at once, for example in this command: SELECT codigosuprimento, suprimento, count (codigosuprimento) quantidade,…
-
2
votes0
answers269
viewsHow to read an excel and insert the data into a table?
I wanted to send data from a file Excel for a table on Sql. I tried using the bulkcopy.ColumnMappings.Add but you’re making a mistake. System.Invalidoperationexception The Given Columnname does not…
-
2
votes1
answer143
viewsMake a SELECT multiple tables and save to a spreadsheet
I’m trying to make a SELECT with multiple tables, where I have a product with your information, and I have another table structure to be able to add an extra field and its information. What I tried…
-
2
votes1
answer183
viewsSelect with PDO and SQL barring HTML
I am trying to make a Select with PDO and MYSQL, but when I put php code in the middle of my html it does not allow the execution of the rest of the page. follows the code excerpt: <select…
-
2
votes1
answer1478
viewsClasura Where em campo tipo json no (Laravel + Eloquent)
The problem is the following, I have to bring from a select the permissions that a profile has, only that the permissions are in a json field, and because it is several permissions we have a…
-
2
votes1
answer91
viewsProcedure SQL (run every second)
Can anyone explain me how this mysql command will work? SELECT @row := @row + 1 AS n_linha , m.* FROM (SELECT b.user_id as prox_user_id, t.auctionID, t.productID, t.auc_due_price+t.auc_plus_price AS…
-
2
votes1
answer403
viewsTable is not generated in Oracle database
That normal wheel: create table M_VENDEDORES ( ID_VENDEDOR number (10,0) not null, NOME varchar2 (100) not null, CPF number (11,0) not null, ENDERECO varchar2 (100) not null, NUMERO varchar2 (10)…
-
2
votes1
answer173
viewsSqlite - Too Many levels of Trigger recursion
I’m getting the error below when I try to make an insertion in Sqlite. Error while executing SQL query on database 'BDTESTE': Too Many levels of Trigger recursion Table creation CREATE TABLE…
-
2
votes4
answers4389
viewsSelect from previous months
I need to do select data from previous months, month by month. My query is like this: SELECT * FROM VIEW_INCD WHERE MONTH(DH_CRIA_INCD) = DATEPART(mm,GETDATE()) - 1 AND YEAR(DH_CRIA_INCD) =…
-
2
votes1
answer55
viewsIs it possible to "Group by" by column content?
I was wondering if it was possible to use GROUP BY based on the date of a column, in this case the column ANALYSIS that creates groups whenever it finds 0, instead of the column. DIA MES YEAR TODAY…
-
2
votes2
answers711
viewsINNER JOIN + LEFT JOIN
I’m trying to make a query where the data lies in 4 tables: TAFCBHISTPED = NUPED, DTEMIS TAFGRPROD = CDGRPR, DSGRPR TAFPROD = CDPROD, CDGRPR TAFITHISTPED = NUPED, CDPROD, QTPED My query stayed that…
-
2
votes1
answer1322
viewsSelect grouping by average hour and sum per day
How can I return in a select the daily sum of the value column, Since before adding up I have to average an hour? My table receives the "value" once a second, I need to make the average per hour of…
-
2
votes1
answer401
viewsQuery to sum query total
I have a Query to select a user list with rates I have to make the sum of these debits. SQL /* * PAGAMENTO */ SELECT pe.name as nomepessoa, py.created_at as datapagamento, py.amount_paid as…
-
2
votes1
answer386
viewsForeach with 2 conditions
I’m trying to put two conditions on the foreach, but I’m not succeeding. The idea is as follows: the client accesses the site in the following URL: /room/balcony and appears only products that have…
-
2
votes3
answers529
viewsDifference between dates in months with decimal return
Good morning ! I am using the command below and the result returned is 6. In fact, looking at the dates, I haven’t exactly closed the 6 months, I think that through this function are not being…
-
2
votes2
answers710
viewsHelp with Join in two different selects
I have two select commands (a bit complex). First code: select A.no_cidadao NOME, F.no_equipe UNIDADE from tb_cds_cidadao_resposta E, tb_cds_cad_individual A, tb_cds_prof as G, tb_pessoa_fisica as…
-
2
votes1
answer107
viewsError inserting data from an excel file into sql
I have a problem when I insert data from excel file into the database because a field that I am entering says it is not of the type time and does not allow inserting , but in the excel file the text…
-
2
votes2
answers48
viewsHow to recover the last payment?
I have the following table in my bank: "financeiro financeiro_id INT AUTO_INCREMENT, financeiro_pagamento DATE, financeiro_pid INT, financeiro_valor INT, "; In this table I record the payments that…
-
2
votes0
answers33
viewsPDO class, how to prepare really avoids SQL Injection s
underlined textHello, I am studying PHP and, regarding POO together with PDO, I have the following question: I always see in the articles in this regard, that making use of the PDO statement:prepare…
-
2
votes1
answer3058
viewsSQL distinct with sum
Hello, I have a problem making a sum in a query. Example and query results: select DISTINCT t.ID, t.nomeuc as "Nome UC", t.tipoturno as "Tipo Turno", a.num_presencas as "Número de Presenças" from…
-
2
votes1
answer60
viewsAjax and PHP form open dynamic list with SQL query
Friends, good afternoon! I need to edit a form that was already ready, including some new options. I know almost nothing about PHP, and I’m having a hard time getting the form combobox to return an…
-
2
votes3
answers7703
viewsRemove last number with sql
I would like to take the last paragraph 1 from this sequence: 0495747500000049908275289000100040000000041 How do I do in sql server?
-
2
votes2
answers569
viewsHow to delete duplicated lines sequentially
I need help deleting the line that is duplicated sequentially, but not the duplicate amount of the same record in the table, example lines 6 and 7, are status repeated sequentially, so I would have…
sqlasked 7 years ago Ricardo Souza 459 -
2
votes2
answers814
viewsDelete triggers by query
Consultation To consult all the triggers of a database, use: SELECT * FROM sys.triggers Failed attempt I tried to exclude all or some of these triggers contained in the result: DELETE FROM…
-
2
votes1
answer209
viewsHow to create a query with a non-existent and error-free table
I have a query that runs in several different schemas in the database, and there is a specific table that only some schemas have, and if there is the value returns. SELECT t.coluna1 ,t.coluna2…
-
2
votes2
answers857
viewsGroup By By Data
I have a table called tsc, with the properties ID and DataHoraOcorrencia. I need to check the amount of ID for DataHoraOcorrecia. SQL SELECT DataHoraOcorrencia as DataHora, COUNT(tsc.ID) as…
-
2
votes2
answers335
viewsHow to return a special character to a query
Good morning! It may be simple my question, but I am starting in SQL Server still, so I would like to know how to return in select a special character. As you can see, there is a simple quotation…
-
2
votes1
answer4187
viewsWhen to use the OVER clause?
To Documentation says : Determines partitioning and sorting of the row set before application of the associated window function. An example I created just to demonstrate this: select ROW_NUMBER()…
sqlasked 7 years ago Daniel Gentil 1,484 -
2
votes2
answers840
viewsUpdate in a column using more than one return
I have to update a certain field of a table by subtracting from it the values returned from a select in another table. The problem I’m facing is that as select returns more than one value and I have…
-
2
votes1
answer743
viewsHow not to bring a particular column in SQL using IF and ELSE?
Gentlemen, I have a table where I want to bring the column when the value is null but when it is filled it should not be displayed, as I can implement this condition in SQL ? SELECT OBS.NUOBS,…
-
2
votes2
answers229
viewsHow to use the WHERE clause on top of a column made from row_number() over()
I’m trying to use Where on top of a column generated by row_number but it doesn’t work; it’s possible to do this?
-
2
votes2
answers171
viewsDelete and relocate index
How should I associate data to have a db SQL Server 2017 with a good performance. I am doing a data association and being new in this area I feel a little lost. I have the following structure as an…
-
2
votes1
answer456
viewsReturn quantity of SQL grouped items
I have the following SQL SELECT `setor`.`set_setor` as age_status_texto, `prospect_agendamento`.`age_status` FROM (`prospect_agendamento`) JOIN `setor` ON…
sqlasked 7 years ago Sr. André Baill 6,946 -
2
votes1
answer150
viewsAre there classification metrics for the elaboration of SQL queries?
Doubt Have some method to sort in difficulty levels, an SQL query in easy, medium or difficult? This question may be a little subjective, but speculating may be a good option. Context I was…
-
2
votes1
answer217
viewsUse Datepart as Query parameter
I’m developing a system and I’m bumping into an SQL query that consists of the user searching for Date and will have the option to choose if the query will be made adding Weekend to these dates,…
-
2
votes2
answers1443
viewsGROUP BY with 2 tables
I need to make a select with JOIN in two tables but I am not able to group to not repeat the lines. Follows the expression SQL: SELECT Andamento.NumeroProcesso, Andamento.Chave, Andamento.Codigo,…
-
2
votes0
answers28
viewsSQL command denied to user selector
I am trying to update my database, and set the Visible of all these vouchers to 0, I am using ide for the command and it accuses an error and I believe it is at syntax, someone can help me? UPDATE…
-
2
votes3
answers1491
viewsHow to use alter table by modifying an attribute to PK and FK?
I have a table Funcionário, where your PK that’s the one Cod_Func. Then I created this table Vendedor, created the attributes manually, did not put the Cod_Func table Vendedor as PK, because this is…
-
2
votes1
answer142
viewsRatio 1:N check date, difference 6 month
I am developing a SQL Server Database, which lists two tables: Where a process has N movements, I need to return the following data: all Process that have the column Irrecuperavel(int) = 1 and that…
-
2
votes2
answers44
viewsColumn 'discount' cannot be resolved. How to use a column that is the result of a calculation to calculate another column in the same query?
I have the following query: Select price, col2, col3*7.5 AS desconto, (price*desconto) AS finalprice From tab1. When I run I get the following message: Column 'desconto' cannot be resolved. How to…
sqlasked 6 years, 1 month ago zwitterion 2,876 -
2
votes1
answer218
viewsInsert Mysql syntax error
I can’t resolve the error below: Notice: Undefined index: Html_cor_task in C: Users User Desktop pi Fe Order2 Cadastrataefa.php on line 14 You have an error in your SQL syntax; check the manual that…
-
2
votes2
answers428
viewsHow to make a query that brings only the count of an uninterrupted sequence?
I want the counting of uninterrupted values of the current year backwards, until the first occurs NULL. Example: Several people make donations to an NGO annually and this is stored in a database,…
-
2
votes1
answer66
viewsSelect random ID between 2 and 4
It is possible to randomly select the ID by placing, for example, the ID numbers that can be drawn (2/4)? Note: Only one line of this selection should be returned. I put this code but it wasn’t:…