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.

sql
Filter by
Sorted by
Tagged with
7 votes
4 answers
27k views

How to use OUTPUT clause with SELECT/INTO statement

I have the following query SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname] INTO #Jedi which CREATE the temporary table #Jedi and INSERT the data inside it. I would like to use OUTPUT ...
Nicolaesse's user avatar
  • 2,680
2 votes
2 answers
8k views

GROUP BY month when selecting a date Teradata SQL assistant

SELECT EVENT_DT - ((EVENT_DT -DATE'1900-01-07') MOD 7) AS dates, CLSFD_USER_ID AS user_id, COUNT(DISTINCT CLSFD_USER_ID) AS number_of_user_ids, COUNT(DISTINCT CLSFD_CAS_AD_ID) AS ...
user avatar
1 vote
1 answer
403 views

Need advice with a Query to list Top 5 rated games

Hi i am helping my friend create a website that shows ratings for games. My table and columns are: Games game_id title genre Ratings user_id game_id rating timestamp Users user_id gender age ...
Jessica's user avatar
  • 15
0 votes
2 answers
3k views

Microsoft SQL Server Error: "Incorrect syntax near keyword Select"

Unsure what syntax the error is referring to at this statement :- Use MyDatabase CREATE TABLE TestTable AS (SELECT * FROM dbo.MyTable); Any help is appreciated!
mitchmitch24's user avatar
1 vote
1 answer
31 views

I want duplicates to be included with my in () statement

I want to include duplicates into my query. I havent succeeded in changing my "in" statements to "joins". My expected result is a row count of 115. My result is a row count of 108. If i do a "Group ...
Softy's user avatar
  • 65
0 votes
3 answers
437 views

Fast query in PostgreSQL

I have a very large database (~1TB), so running even a very simple query can take a very long time. Eg. for: EXPLAIN select count(*) from users; the cost is 44661683.87 disk page fetch. Hence ...
KarateKid's user avatar
  • 3,276
-2 votes
1 answer
41 views

Retrieve unique rows based on id

I have two tables: Report ReportId CreatedDate 1 2018-01-12 2 2018-02-12 3 2018-03-12 ReportSpecialty SpecialtyId ReportId IsPrimarySpecialty 1 1 1 ...
Bagzli's user avatar
  • 6,377
1 vote
4 answers
82 views

Count numbers that don't match between 2 tables

My original query is to count by month how many items have never been ordered. I have created a sql fiddle here http://sqlfiddle.com/#!18/e89a7/2 And put together the query below SELECT COUNT(...
Ryan Gadsdon's user avatar
  • 2,340
-1 votes
2 answers
179 views

SQL Group by to include alternate column values

I have the following table: ID,col1,col2,total 1,artist1,artist2,3 2,artist2,artist1,4 How do I group columns col1 and col2 and sum the totals so in cases like this I would get: artist1,artist2, 7 ...
MelosMojo's user avatar
0 votes
0 answers
191 views

SQL query inner join with 3 tables? [duplicate]

i have three tables; Table one - Reservations +-----------------+-------------+---------------------+---------------------+ | created_id | contract_no | rent_start_date | rent_end_date ...
SQL noob's user avatar
0 votes
2 answers
395 views

I have to write a procedure to select the details of the vehicle where the vehicle number is given as input

I have the schema as vehicle(vehicle_no,engine_no,offence_count,license_status,owner_no); I created a new table similar to that of table vehicle using the query create table vehi as select * from ...
Sreenu Murugesan's user avatar
1 vote
2 answers
73 views

Exclude items according to a record

The following query excludes all the products however, I am trying to exclude the products only if the R.OPERATING_UNITS = 'WP'. I was not sure if I need to include the case statement in the where ...
jax's user avatar
  • 830
0 votes
2 answers
53 views

Get max date for each line in a table in SQL

I edited my answer so the solution for my question is: select DISTINCT e.Cod_proiect , p.Descriere , p.Stare , p.ClientService , e.Data from ExpPoz as e join Proiecte as p ...
user1147188's user avatar
0 votes
1 answer
359 views

Node.js API with express & mysql - WHERE IN and bind parameter with mutliple comma seperated values

I have a url like this ... where multiple (comma seperated values can be added to the url) localhost:4001/api/v1/users/search?title=mr,dr That's my query: router.get('/search?', function(req, ...
Philipp M's user avatar
  • 3,418
-1 votes
2 answers
44 views

sum all value in stock(qty) and then join and sum all value of customer(qty) table and show into another column as available_stock

sum function results incorrect(when joining two tables) ## select dress,sum(qty) as sold from customer group by dress ----------------------- Dress | Sold | Casual Dress | 5 | Fancy ...
Hafiz Siddique's user avatar
0 votes
1 answer
733 views

Only one record returned after union all

Using an SQL interface into an AS/400 (iSeries) DB2 database but running into and issue where pallet information could be stored in two tables, one for non-allocated stock (warpall) and one for ...
PureBluff's user avatar
0 votes
1 answer
157 views

retrieve large amount of data from multiple tables within seconds

The following query retrieves data from multiple tables. There is a problem, as the data increases the amount of time to retrieve the data increases too ( the number of rows has reached to 20 ...
MIB's user avatar
  • 13
0 votes
2 answers
396 views

Calculate new users subscription amount MySQL

I have a dataset where I need to find out New subscribers revenue. These are subscribers that are paying either weekly or monthly depending on the subscription they are on. The unique identifier is ...
Pravin's user avatar
  • 471
0 votes
0 answers
59 views

Unique id Key from a select top 1 statement

With own id scheme as follow select TOP 1 @docID=id+1 FROM dbo.user_documents WITH (NOLOCK) ORDER BY ID DESC ... INSERT INTO dbo.user_Documents ([id],...) Values(@docID,...) as @Erland Sommarskog ...
H.C's user avatar
  • 586
0 votes
1 answer
159 views

Fetch data from two tables and add the values with same date in sqlite

I have two tables named as cash and bank both the tables have same attributes `( `id` VARCHAR, `person_id` VARCHAR, `date` DATE, `remarks` VARCHAR, `type` VARCHAR, `amount` INTEGER, `balance` ...
Zainul Abideen's user avatar
3 votes
1 answer
5k views

DESCRIBE with LIMIT in spark sql

I am using the DESCRIBE keyword to get column information about a temp view. It's a useful method however I have a table for which I'd only like to describe a subset of the columns. I'm trying to use ...
Eugene Brown's user avatar
  • 4,192
0 votes
1 answer
784 views

select between record with temp table [closed]

i want to show tariff with feb month record with date serial if tariff not available in month then show with previous tariff please see snapshot for more clarification ALTER PROCEDURE [dbo].[...
Lalit Sharma's user avatar
3 votes
1 answer
58 views

Fill a table with elements from another table

I've a problem with my java project. I have two text files to insert in postgresql with the jdbc driver. the two files are of this type: 1) snp (rsid, chr, boolean value) 2) locus (rsid, mrna, gene, ...
Valentina Fratini's user avatar
1 vote
1 answer
43 views

Query with Totals Query results as criteria returns the expected number of results squared

Background I am building an Access 2010 database that has a table [ControllerAdjustments] that keeps track of all adjustments made to controllers with an [AdjustmentID] autonumber field, a [...
Valiant's user avatar
  • 27
0 votes
3 answers
73 views

INSERT SELECT with differed table/col stucture

I am trying to create a INSERT SELECT statement which inserts and converts data from Imported_table to Destination_table. Imported_table +------------------+-----------------------+ | Id (varchar(...
Thomas Beumer's user avatar
0 votes
1 answer
146 views

MySQL get amount of books for each user

I'm trying to return the amount of books a member has from all libraries. In different libraries the member has a different member level. SELECT b.* FROM library a LEFT ...
mdnba50's user avatar
  • 369
1 vote
1 answer
49 views

Data spills out of table in Apache Drill

I've been using Apache Drill lately for checking my raw data. It's been going great, however I noticed that it doesn't pretty print or wrap when showing the data in a table. For example: +----+---...
skylerl's user avatar
  • 4,110
0 votes
1 answer
44 views

Get all childen from cte view

I want to get all the grandchildren, The following logic works for me, but as this is a view I cannot pass argument xxx SQL code: WITH grandchildren as ( SELECT e1.ID ID, e1.LName + ', '+e1....
Vijaya Simha's user avatar
1 vote
2 answers
32 views

Embedded images order

Lets consider simple table for products of some e-commerce site product (id SERIAL PRIMARY KEY, title TEXT, description TEXT, price REAL) and separate table for embedded images product_images (id ...
Bodito's user avatar
  • 107
0 votes
2 answers
56 views

How to combine different rows to one row in sql server

I saw some issues with the same tittle of mine but they are not really same with my problem(at least the ones that I could see) I have the below table: id1 id2 column1 ...
JollyRoger's user avatar
0 votes
1 answer
30 views

SQL Finding duplicates that includes a certain value

Trying to find duplicates (two or more) that one of the duplicates includes "Ambulance". Tried using SUM(CASE...), but the query result returns only the values with "Ambulance". select r....
Kenshin H's user avatar
2 votes
1 answer
2k views

Display all Dates in a given month and year in firebird select procedure

I am making a program for Time and Attendance. I have a table with this fields ATTEND_PK INTEGER NOT NULL, EMP VARCHAR, ATTEND_DATETIME TIMESTAMP, USER_PK SMALLINT, ATTEND_UPDATETIME ...
Don Juan's user avatar
  • 155
1 vote
1 answer
73 views

How to construct a SQL sub query in SQL Server 2008?

I have requirement to extract total number of rows from a table - ci_periodicBillings only for clients where they have rows from a particular date range from another table - ci_invoiceHeaders. I am ...
matt80's user avatar
  • 35
0 votes
1 answer
41 views

Turning a cascading one to one relationship into a one to many

I'm working on a table that contains Items and their details. The table contains the ItemID and also "Alt_ItemID" which stores another ItemID that can be used in place of the original Item. eg. ...
NotADog's user avatar
  • 159
1 vote
1 answer
1k views

GridView + System.Data.SqlClient.SqlException: "Invalid object name 'List'

I have a Problem... I want to show my "Automatically Created" Table in my Form2... I get the error: System.Data.SqlClient.SqlException: "Invalid object name 'List'....... but only for the "...
Deparli's user avatar
  • 33
1 vote
1 answer
129 views

Returning multiple values using function causing multiple query runs

We have kiosks for customers to check their purchase volume for two different categories of items. They will input their mobile number, which will send an OTP to their mobile numbers and they will ...
user3625561's user avatar
1 vote
2 answers
566 views

How to find number of distinct phones per customer and put the customers(counts) in different buckets as per the counts?

Below is the table where I have customer_id and different phones they have. customer_id phone_number 101 123456789 102 234567891 103 345678912 102 ...
Ishaj's user avatar
  • 15
-1 votes
3 answers
59 views

Find if N or more than N records exists between 2 dates. If so update those records without doing extra table scan

ID Name Time Status 1 A 5 0 2 B 6 0 3 C 7 0 4 D 10 0 Lets say N = 2, I need to find whether 2 records exists between time 5 and 10. In this case records with id ...
Dhakchianandan's user avatar
0 votes
1 answer
2k views

how to sort the data in data set i use vb.net and sql database

how to sort the data in data set i use vb.net and sql database my code Dim SQLSTATMENT As String = "SELECT * FROM CODES WHERE LEVEL = " & i & "" Dim ds As New DataSet Dim ...
ZUHAIR's user avatar
  • 23
2 votes
3 answers
850 views

MySQL subquery with two tables count and group by

I have database of doctors and hospital joined by hospitaldoctor table. I have to List town, amount of hospitals in each town, but only hospitals which have more than 5 doctors. SELECT hospital.town,...
Bartłomiej Matyjaszczyk's user avatar
0 votes
1 answer
787 views

Grouping multiple count queries on same table by time

I have a query in which I need 3 columns, along with an additional ‘Time’ Column, what I need to do is run each of the count queries and match them to the time associated, however whats happening is ...
user's user avatar
  • 395
-1 votes
3 answers
128 views

How to remove the last digit after the some symbols in SQL? [duplicate]

For input A.B.1.23 I want output A.B.1
Muthukrishnan Kandasamy's user avatar
-1 votes
2 answers
454 views

SQL How to display text next to values in column

Ask for your help. I work with MySQL and cannot figure out how to display text near value like this ID | Value ---------- 1 | 100 unit(s) ---------- 2 | 200 unit(s) ---------- 3 | 250 unit(s) ...
Joel's user avatar
  • 9
0 votes
0 answers
100 views

create a tree from database

I have a SQL table with the following structure for example: name last-name ------------------- maria moh adam rami moh adam sabi ali adam mary joh ali How I can ...
soul yenny's user avatar
1 vote
4 answers
107 views

What is the best way to implement a sql for retrieving multiple user names from same query?

I have a products table where I include 3 columns, created_user_id, updated_user_id and in_charge_user_id, all of which are related to my user table, where I store the id and name of the users. I ...
Bernard Wiesner's user avatar
-1 votes
2 answers
2k views

JPA merge is inserting instead of updating

I have a database with a field NAME and I need to upload a picture as blob type to a field PICTURE on every entity whose NAME is equal to the file name. However, upon running the code creates a bunch ...
LuccasK's user avatar
  • 11
1 vote
1 answer
214 views

How to edit my HTML codes for the output to highlight all cells of a row based on a condition?

I have a Stored Procedure in SQL Server 2012 which provides the HTML formatting of the output a T-SQL query. It works fine and is shown below: DECLARE @Body NVARCHAR(MAX), @TableHead VARCHAR(...
user3115933's user avatar
  • 4,363
-1 votes
3 answers
122 views

How to use "union all" in SQL?

I have two queries. The first: SELECT CreatedOn, COUNT(*) AS locationNonProcessed FROM Table1 WHERE IsActive = 0 GROUP BY CAST(CreatedOn AS DATE), CreatedOn The second: SELECT CreatedOn, COUNT(*) ...
Shailendra Pal's user avatar
1 vote
1 answer
112 views

Dropdown button to output value from database when clicked, on the same page

Sorry, I won't upload the whole code to avoid plagiarism, because this is my uni project, but I just want to ask a simple question. So basically I have a database with stored sensor data. Then I have ...
Izhek's user avatar
  • 21
0 votes
1 answer
58 views

SQL - Concat multiple rows into 1 on a join [duplicate]

So I've got 2 tables which looks like the below: MetaData Id | Value --------------------- 1 | 5 1 | 6 2 | 6 LookUp Value | Text --------------------- 5 | Car 6 ...
Bob Shillhill's user avatar