syntax error missing expression - sql

Hi i'm trying to show the name of all Customers who have only one account with a balance of more than $1000 in any branch of the bank and one or more loan with an amount of more than $4000 in any branch in Edina
select account.cname,
from Account
where account.bal > 1000
UNION
(select loan.cname
from loan
where loan.amt>4000 AND
l.bname )in (
select bname
from branch
where lower(bcity)= 'edina'
);
does it look right?

You have extra comma in first select.
your in clause doe not have a column to compare with
Maybe something like:
select account.cname
from Account
where account.bal > 1000
UNION
select loan.cname
from loan
where loan.amt>4000 AND l.bname
and loan.bname in ( select bname from branch where lower(bcity)= 'edina' )

Related

Select top 5 customers with balance(:name, :balance) value and put other ones with name Other and sum of balance

Need help with active record query, I have customers table with name and balance fields.
How can I make query that will return 6 values, first 5 is top customers by balance and 6th one is sum of all others?
need to select name and balance, for others it will custom name ''Other
Divide et impera: make two view then put results together
select *
from ( select * from view_top_5
union
select * from view_sum )
where view_top_5 is view (or a subquery) that gives you the top 5 customers, view_sum is a query giving you the sum. To get "all others" you can build your query from this skeleton:
-- skeleton for view "view_sum"
select <what you need>
from mytable
where customer_id not in (select client_id
from view_top_5)

Get a column from each Table

I'm working with SQL Server 2008
I have 2 tables and I want to get 2 columns from the 1st table and 1 column from the 2nd table
when I make JOIN it gives me another things...
here is my code:
Select SUM(Sales.CDnum) as CDnum, SUM(Sales.Total) as TotalMoney,
SUM(Expenses.Costs) as Expenses, SUM(Sales.Total - Expenses.Costs) as Winnings
from Sales
JOIN Expenses
ON Sales.ID = Expenses.ID
but when I execute the code it gives me only one raw from the 1st table and 1 raw from the second ... because I have 1 raw in the 2nd table and 1st I have many raws...
someone can help me....
as I see ID columns are not related to each other in both tables, so you need to union them, for example like in this query:
select sum(q.sales), sum(q.expense), sum(q.sales)-sum(q.expense) as winnings
from (
select sum(Sales.Total) as sales, 0 as expense from Sales
union all
select 0 as sales, sum(Expenses.Costs) as expense from Expenses
) as q

SQL Grouping / Contract Value

I know this is going to be a simple one but after a midnight run at other coding my SQL brain is fried so I'm just reaching out for some quick help. I've got a test table with Agent, AgentID, Parent Account, AccountID, and TCV. What I need to do is pull all the agent/account IDs where the AccountIDs belong to an aggregate parent account under that agents name >= 10K.
So in this example, John has 2 accounts under the parent account ABC123 and since their total value is >=10K, these 2 would get pulled. But notice below where Jane has 2 accounts likewise under ABC123 but b/c their total value in her name is < 10K, they would not be pulled. So the results would be something like this:
Essetially I need to pull all AccountIDs where the total value of the parent account they roll-up to for that person is >= 10K. BTW, I'm using SQL Server Management Studio R2.
You just do a simple group by/having to get a list of agentids/parentaccounts that meet the 10k criteria. Then you can use that in a sub-select to join back to the same table and get the list of account ids
select agentid, accountid
from table t
inner join (
select agentid, parentaccount
from table
group by agentid, parentaccount
having sum(tcv) >= 10000
) t1
on t.agentid = t1.agentid
and t.parentaccount = t1.parentaccount
;WITH MyCTE AS
(
SELECT AgentID,
ParentAccount,
SUM(TCV) AS Total
FROM TableName
GROUP BY AgentID,
ParentAccount
)
SELECT T.AgentId, T.AccountId
FROM Table T
JOIN MyCTE M
ON M.AgentId = T.AgentId
AND M.ParentAccount= T.ParentAccount
WHERE M.Total>10000

Display full records of duplicate entries across multiple columns

I have a table (customers) that includes fields telephone_1, telephone_2, telephone_3 & telephone_4.
What I need to see is full records of where any telephone numbers are used in other customers - as I believe something has gone wrong with the data somewhere and customer numbers are duplicated into each other!
I have tried the below code, but this doesn't give me what I want as only compares 1/1, 2/2, 3/3 & 4/4. I have an ID field which can be used to differentiate between records.
SELECT *
FROM Customers AS a
WHERE 1 < (SELECT Count(*)
FROM Customers AS b
WHERE a.Telephone_1 = b.Telephone_1
OR a.Telephone_2 = b.Telephone_2
OR a.Telephone_3 = b.Telephone_3
OR a.Telephone_4 = b.Telephone_4
Any assistance is appreciated - thanks!
How about this?
SELECT *
FROM Customers AS a
WHERE 1 < (
SELECT Count(*)
FROM Customers AS b
WHERE a.Telephone_1 IN (b.Telephone_1, b.Telephone_2, b.Telephone_3, b.Telephone_4)
OR a.Telephone_2 IN (b.Telephone_1, b.Telephone_2, b.Telephone_3, b.Telephone_4)
OR a.Telephone_3 IN (b.Telephone_1, b.Telephone_2, b.Telephone_3, b.Telephone_4)
OR a.Telephone_4 IN (b.Telephone_1, b.Telephone_2, b.Telephone_3, b.Telephone_4)
)
========Take 2=======
Following the comments:
Create a new table with telephone numbers nd customer IDs
CREATE TABLE tempTelephoneNos (
INTEGER customer,
VARCHAR(32) telephoneNo
);
assuming those are the appropriate data types for the customer Id and phone no.
Populate the new table
INSERT INTO tempTelephoneNos (customer, telephoneNo)
SELECT customer_id, telephone_1
FROM customers
UNION ALL
SELECT customer_id, telephone_2
FROM customers
UNION ALL
SELECT customer_id, telephone_3
FROM customers
UNION ALL
SELECT customer_id, telephone_4
FROM customers
Then you can find out which telephone numbers appear for more than one customer with
SELECT customer, telephoneNo
FROM tempTelephoneNos
WHERE 1 < (SELECT COUNT(*) FROM tempTelephoneNos GROUP BY telephoneNo)

Find Duplicates in SQL and UPDATE them?

I'm trying to find all duplicates in a Table and change one of their values.
Now i use:
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
The problem that it returns only
Amount
23.6500
41.8800
42.3500
And not
Amount
23.6500
23.6500
41.8800
41.8800
42.3500
42.3500
So I can't UPDATE all the rows.
How can I get it the way I showed?
Thanks,
Dan
Just wrap it inside an IN query:
SELECT Amount
FROM Bids
WHERE Amount IN (
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
)
UPDATE: added UPDATE statement
UPDATE Bids
SET Burned = 1
WHERE Amount IN (
SELECT Amount
FROM Bids
GROUP BY Amount, AuctionID
HAVING ( COUNT(Amount) > 1 ) AND (AuctionID=1)
)
Assume that you have Id in Bids table:
SELECT Amount
FROM Bids b1
WHERE AcutionId = 1
AND EXISTS (Select 1 from Bids b2
WHERE b2.AuctionID = b1.AuctionId
AND b1.Amount = b2.Amount
AND b1.Id <> b2.Id)
I'm curious to know why your original select doesn't satisfy your requirement. If for every member within a set of duplicates you're only selecting one of them, then you have one to update. It should be informative to add AuctionId to the select provided by Frank Schmitt to see what distinguishes these rows.