Scalar subquery contains more than one row - sql

Im working with H2 database and wanted to move some data. For that I created the following Query:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID;
When I now perform it in the H2 console I get the following error:
Scalar subquery contains more than one row; SQL statement:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID [90053-192] 90053/90053 (Hilfe)
What is this error telling me?
EDIT
What I want to achiev with my query:
Actually every CUSTOMER has a CUSTOMER_SERVICE. And I simply want to move the COLUMN EMAIL from CUSTOMER_SERVICE to the CUSTOMER Table. for that I already added a email column to the user. I hoped to be able to do it with my query but obviously not.

Your query is not syntactically valid (all subqueries must have parentheses around them).
What you are missing is a correlation clause. I believe you want:
UPDATE CUSTOMER c
SET EMAIL = (SELECT cs.EMAIL
FROM CUSTOMER_SERVICE s
WHERE s.ID = c.CUSTOMER_SERVICE_ID
);
I don't know what this is supposed to be: [90053-192] 90053/90053 (Hilfe).

Your select query is returning more than one row. If you don't want it to, then you need to do something like an aggregate or LIMIT 1 or something similar.

Your sub-query for at least one of your customers has multiple email addresses.
You could ... (Select top 1 serverice.email ...
Or ... (Select max(serverice.email) ...
Update Customer Set EMail=B.Email
From Customer A
Join (Select ID,max(EMail) as EMail From CUSTOMER_SERVICE Group By ID) B
on (A.CUSTOMER_SERVICE_ID = B.ID)

I've spend a lot time with this error type (there shoudn't be duplicates in DB).
At last I've found problem via SQL with COUNT like this:
UPDATE CUSTOMER
SET EMAIL = SELECT COUNT(service.EMAIL)
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID =
customer.CUSTOMER_SERVICE_ID;
And then select problem rows with EMAIL!='1'

Related

SQL - Set field as count from another table

I am trying to set a field in my target table as the count of the email addresses in another table. An email address can appear many times in my other table and I wanted to count the total up and set that count as my target field value.
SELECT
a.*
,a.PickedUp_Count AS COUNT(b.Emailaddress)
FROM
Master_List a
INNER JOIN
Picked_Up b
ON
a.Emailaddress = b.Emailaddress
It's best not to use * selectors because usually you want to know specifically what you're getting back from a table. You don't have to expand it in the select, but you DO have to identify how all of the individual columns will also be aggregated in the group by column.
SELECT
a.<field1>
a.<field2>
....
,COUNT(*) as PickedUp_Count
FROM
Master_List a
INNER JOIN Picked_Up b
ON a.Emailaddress = b.Emailaddress
GROUP BY
<field1>
,<field2>
....
The group by must include all fields listed in the select which are not already being aggregated in the select - i.e. any field that isn't the count.
Additionally, it's worth noting that PickedUp_Count is whatever random name you choose.

SQL Server : get IN value for which it was found that row

I'm having difficulties for solving this problem. I'm using Microsoft SQL Server 2014 (ver. 12.0.4100.1).
I have two tables contacts and business. I have an array of contact usernames. I'm fetching a business list from this username array with a subquery in the WHERE, like this:
SELECT business.id, business.name, business.notes
FROM business
WHERE business.id IN (SELECT contacts.business_id
FROM contacts
WHERE contacts.username IN ('username1', 'username2', 'username3'))
This query is working correctly, but I can't figure out how to select also the value for which every row was found; for example: if I fetch the business from the contact with username 'username1', I want to fetch it and see the result:
business.id = 1
business.name = 'Business 1'
business.notes = 'Notes from business 1.'
contacts.username = 'username1'
Getting that result, I could see easily from what contact came every business.
Thank you in advance!
EDIT: There may be more than one contact for every business, so doing an INNER JOIN would return duplicated business results for every contact. I want to know exactly what contact was the one which was selected on the subquery.
I think you are looking for a join:
SELECT b.id, b.name, b.notes, c.username
FROM business b JOIN
contacts c
ON b.id = c.business_id
WHERE c.username IN ('username1', 'username2', 'username3');

Querying records that meet muliple criteria

Hi I’m trying to write a query and I’m struggling to figure out how to go about it.
I have a suppliers table and a supplier parts table I want to write a query that lists suppliers that have specified related Parts in the supplier parts table. If a supplier doesn’t have all specified related parts then they should not be listed.
At the moment I have written a very basic query that lists the supplier if they have a related supplier part that meets the criteria.
SELECT id ,name
FROM
efacdb.dbo.suppliers INNER JOIN [efacdb].[dbo].[spmatrix] ON
id = spmsupp
WHERE spmpart
IN ('ALUM_5083', 'ALUM_6082')
I only want to show the supplier if they have both parts related. Does anyone know how I could do this?
Use a subquery with counting distinct occurences:
select * from suppliers s
where 2 = (select count(distinct spmpart) from spmatrix
where id = spmsupp and spmpart in ('ALUM_5083', 'ALUM_6082'))
As a note, you can modify your query to get what you want, just by using an aggregation:
SELECT id, name
FROM efacdb.dbo.suppliers INNER JOIN
[efacdb].[dbo].[spmatrix]
ON id = spmsupp
WHERE spmpart IN ('ALUM_5083', 'ALUM_6082')
GROUP BY id, name
HAVING MIN(spmpart) <> MAX(spmpart);
If you know there are no duplicates, then having count(*) = 2 also solves the problem.

Excel SQL update based on values in 2 tables

Let me start by saying that I am not using the right tools for the job but they are the only tools I have access to.
I am using Excel as a database that contains multiple tables with associated data. Normally in a database, this data could be associated with a foreign key I believe however that is not the case with Excel.
I have 2 tables:
TABLE items
batch_id customer_id
1 1
2 1
3 2
and
TABLE customers
id customer
1 cust1
2 cust2
I have a userform which only allows the user to select a customer by name.
What I would like to be able to do is update the customer_id in the items table based on a specific batch_id and a customer name.
This is what I have so far that isn't working.
UPDATE [items$]
SET [items$].customer_id=[customers$].id
INNER JOIN [customers$]
ON [items$].customer_id=[customers$].id
WHERE [items$].batch_id='value1'
AND [customers$].customer='value2'
[UPDATE]
The following seems to be a little closer to answer but is giving me a 'Operation must use an updateable query.' error.
UPDATE items
SET items.customer_id=(
SELECT FIRST(customers.id)
FROM customers
WHERE customers.customer=value2)
WHERE items.batch_id=value1;
I keep getting a 'Operation must use an updateable query error' with this but otherwise I see no reason why it shouldn't work
I'm not sure how to put in excel VBA. But the update syntax should be like (if u want to update all the rows)
Update items
set items.customer_id = (select customers.id
from customers inner join items
on customers.id = items.customer_id
where customers.id='Value2'
and items.batch_id='Value1');
Hope this might help u
Try with the following once may be you will get your expected result.
But this is the case of SQL.
UPDATE items i
JOIN customers c ON i.customer_id = c.id
SET i.customer_id = c.customer

Sql Statement trying to understand join statements

My code is pasted here for the Sql tables and information: http://pastebin.com/GsKp4z30
what I am trying to do is:
SuperClean would like to offer a special to their customers that use coupons. Retrieve the names and
addresses for the customers using coupons on any order from September 1, 2014 through December 24,
2014.
But I keep getting 40+ lines of information and I can't seem to get a join statement to work correctly.
I was trying this but I keep getting invalid identifier.
SELECT
CUSTOMER.CUSTOMER_NAME, CUSTOMER_CUSTOMER_NAME, CUSTOMER.CUSTOMER_ADDRESS
FROM CUSTOMER
INNER JOIN
CUSTOMER ON CUSTOMER.CUSTOMER_ID = CUSTOMER_INVOICE.CUSTOMER_ID;
You have a good first attempt.
Your first problem comes from INNER JOIN CUSTOMER - you are already selecting from CUSTOMER, so why JOIN it with CUSTOMER? You should be joining with CUSTOMER_INVOICE instead:
SELECT
CUSTOMER.CUSTOMER_NAME, CUSTOMER.CUSTOMER_ADDRESS
FROM CUSTOMER
INNER JOIN
CUSTOMER_INVOICE ON CUSTOMER.CUSTOMER_ID = CUSTOMER_INVOICE.CUSTOMER_ID;
I've also fixed the other problem with your SELECT, before it was this:
SELECT
CUSTOMER.CUSTOMER_NAME, CUSTOMER_CUSTOMER_NAME, CUSTOMER.CUSTOMER_ADDRESS
Yet there is no column or alias defined as CUSTOMER_CUSTOMER_NAME. So it will throw you an invalid identifier error.
This will return all of the customers with respective customer invoices - now we need to filter it down to invoices with the coupons set to YES using WHERE:
SELECT
CUSTOMER.CUSTOMER_NAME, CUSTOMER.CUSTOMER_ADDRESS
FROM CUSTOMER
INNER JOIN
CUSTOMER_INVOICE ON CUSTOMER.CUSTOMER_ID = CUSTOMER_INVOICE.CUSTOMER_ID
WHERE
CUSTOMER_INVOICE.COUPON_YESNO = 'YES';
and now filter the result-set down further to work with the particular date range from your question. I'll let you figure this part out unless you're having difficulty understanding the WHERE.