Error in code. Only one expression can be specified in the select list when the subquery is not introduced with EXISTS - sql

I'm trying to run this query but no matter what I do I keep getting this error and I cannot work it out. Any help would be appreciated.
select
CC.ContactID,
from
Client C
join Contacts CC on CC.ContactID = C.ContactID
where
ClientID In (
Select
ClientID,Sum(Total-allocated) as Bal
from
Main
Where
Total <> Allocated
and NomTypeID < 10
Group By
ClientID
HAVING Sum(Total-allocated) > 10
)

I'm not sure what DB kind you are using (so I also could not test my answer).
But in general when you use IN you need the subquery to return only one column.
So the query should be (removed the Sum(Total-allocated) as Bal):
select
CC.ContactID,
from
Client C
join Contacts CC on CC.ContactID = C.ContactID
where
ClientID In (
Select
ClientID
from
Main
Where
Total <> Allocated
and NomTypeID < 10
Group By
ClientID
HAVING Sum(Total-allocated) > 10
)

Please use below query,
select
CC.ContactID from
Client C
join Contacts CC on CC.ContactID = C.ContactID
join (Select ClientID,Sum(Total-allocated) as Bal from Main Where
Total <> Allocated and NomTypeID < 10
Group By ClientID HAVING Sum(Total-allocated) > 10)) m
on (C.ClientID = m.ClientID);

Related

Return Duplicate emails along with User Ids that are different

I'm running into an issue with a duplicate query and I hope you guys can help.
Essentially what I want to do is find and list of the duplicate emails associated with different userids
My query is:
select UserId, acitveid, email, userstatusid
from (select u.UserId, u.acitveid, cd.email, u.userstatusid,
count(*)over (partition by cd.email) as cnt
from ContactDetails cd
join UserContactDetails ucd on ucd.ContactDetailsId = cd.ContactDetailsId
join dbo.[User] u on u.UserId = ucd.UserId ) ua
where cnt >1
The issue I have with the above query is that it is returning the same userids for some of the results so it looks like:
Userid AcitveId email UserStatusid
123 1 abc#123.com 1
123 1 abc#123.com 1
135 1 efg#123.com 1
142 1 efg#123.com 1
The results Im looking for are simply:
Userid AcitveId email UserStatusid
135 1 efg#123.com 1
142 1 efg#123.com 1
WITH base AS (
SELECT DISTINCT u.UserId
,u.acitveid
,cd.email
,u.userstatusid
,
FROM ContactDetails cd
JOIN UserContactDetails ucd ON ucd.ContactDetailsId = cd.ContactDetailsId
JOIN dbo.[User] u ON u.UserId = ucd.UserId
)
,duplicate_emails AS (
SELECT email
,count(userId) AS cnt
FROM base
GROUP BY 1
HAVING count(userId) > 1
)
SELECT b.*
FROM base b
JOIN duplicate_emails de ON b.email = de.email
A self join across Email = email and id <> id would work fine here. That said, your request and lack of sample data means that we are largely guessing based off the query and sample output you have provided. The below should get you pretty close and, if you update your OP, I am sure we can get you exactly what you're after.
SELECT ActiveUser.UserID Active_UserID,
ActiveUser.ActiveID Active_ActiveID,
ContactDetails.email AS Email,
DuplicateUser.UserID AS Dup_UserID,
DuplicateUser.ActiveID As Dup_ActiveID
FROM ContactDetails INNER JOIN
ContactDetails AS Duplicate ON ContactDetails.email = Duplicate.email AND ContactDetails.UserID <> Duplicate.UserID INNER JOIN
UserContactDetails AS ActiveUserContactDetails ON ActiveUserContactDetails.ContactDetailsID = ContactDetails.ContactDetailsID INNER JOIN
dbo.[User] AS ActiveUser ON ActiveUser.UserID = ActiveUserContactDetails.UserID INNER JOIN
UserContactDetails AS DuplicateUserContactDetails ON DuplicateUserContactDetails.ContactDetailsID = Duplicate.ContactDetailsID INNER JOIN
dbo.[User] AS DuplicateUser ON DuplicateUser.UserID = UserContactDetails.UserID

SQL query to get 10 last topics with replies

I have a simple forum database and want to write SQL query to get
10 last topics (threads) with replies from 3 or more unique posters.
Result: Topic (thread) name | Last message text | User name | Date
UPDATED:
Here is what I tried:
SELECT thread.thread_id, thread.thread_name, message.thread_id,
COUNT(message.thread_id)
FROM thread, message
WHERE message.thread_id = thread.thread_id
GROUP BY message.thread_id
HAVING COUNT(message.thread_id) >= 3
LIMIT 10
But it returns 10 topics (threads) with 3 or more replies (but not 3 or more unique posters)
UPDATE 2:
SELECT message.thread_id AS ID, thread.thread_name AS topic
FROM message INNER JOIN thread
ON message.thread_id = thread.thread_id
GROUP BY message.thread_id
HAVING COUNT(*) >= 3
LIMIT 10
This also returns 10 topics with 3 or more replies
UPDATE 3, thanks #shawnt00
SELECT thread.thread_name AS 'Topic', message_text AS 'Message', person_nickname AS 'Nickname', message_date AS 'Date'
FROM thread,
(
SELECT thread.thread_id, MAX(message_date) AS last_date
FROM thread
INNER JOIN message ON message.thread_id = thread.thread_id
GROUP BY thread.thread_id
HAVING COUNT(DISTINCT message.person_id) >= 3
) AS temp
INNER JOIN message
ON message.thread_id = temp.thread_id AND message.message_date = temp.last_date
INNER JOIN person ON person.person_id = message.person_id
WHERE thread.thread_id = temp.thread_id
ORDER BY message.message_date DESC
LIMIT 10
with data as (
select p.thread_id, max(message_date) as last_date
from thread t inner join message m on m.thread_id = t.thread_id
group by p.thread_id
having count(distinct m.person_id) >= 3
)
select *
from data d
inner join message m
on m.thread_id = d.thread_id and m.message_date = m.last_date
inner join person p on p.person_id = m.person_id;
The only assumption is that there can't be ties on dates. If you have analytic functions available then there are other approaches using those. I'm sure you can figure out how to get top 10.
i think you need below query.
Assuming that you are using mysql so i used Limitfunction as you want last 3 unique row
select distinct th.thread_name as Topic_name, m.message_text, p.person_nickname as User_name, m.message_date as Date from message m
inner join thread th on m.thread_id=th.thread_id
inner join persion p on p.persion_id=m.persion_id
order by message_date desc
Limit 3
But if the db is SQLSERVER Then use below query
select distinct TOP(3) th.thread_name as Topic_name, m.message_text, p.person_nickname as User_name, m.message_date as Date from message m
inner join thread th on m.thread_id=th.thread_id
inner join persion p on p.persion_id=m.persion_id
order by message_date desc

Incorrect count value in subquery on table join column

The query I'm executing seems to be ignoring the where clause in the subquery
(select count(amazon) from orders where b.amazon = 2 and manifest = a.dbid)
column amazon is type INT
SQL SERVER 2014
If I run the query on its own and enter the value for manifest I get the correct result which I am expecting and is 1
select count(amazon) from orders where amazon = 2 and manifest = '211104'
Result Returns 1
When I run the query below I get a result of 5 which is the count of all orders where manifest = 211104 but the value of amazon is 1 in 4 results and 2 in 1 result.
Select distinct
top 30 DBID, today ,sum([amazon-orders])
From
(
SELECT [dbid], [today],
(select count(amazon) from orders
where b.amazon = 2 and manifest = a.dbid) as [amazon-orders]
FROM [manifest] a
join orders b on a.[dbid] = b.[manifest]
) t1
Group By
DBID, today
order by dbid desc
Can someone please help me.
Thanks
You have an extra join so you are counting multiple times... do this:
Select distinct
top 30 DBID, today ,sum([amazon-orders])
From
(
SELECT [dbid], [today],
(select count(amazon) from orders b
where b.amazon = 2 and manifest = a.dbid) as [amazon-orders]
FROM [manifest] a
) t1
Group By
DBID, today
order by dbid desc
or like this
SELECT [dbid], [today], count(o.amazon)
FROM [manifest] a
join orders o on a.dbid = o.manifest and o.amazon = 2
group by dbid, today
or this if you have columns you don't want to join (there is more going on than just this one join in your query and you need to use a left join):
SELECT [dbid], [today], sum(case when o.amazon is not null then 1 else 0 end)
FROM [manifest] a
left join orders o on a.dbid = o.manifest and o.amazon = 2
group by dbid, today
How's this?
SELECT top 30 [dbid], [today], sum(case when b.amazon = 2 then 1 else 0 end) as [amazon-orders]
FROM [manifest] a
join orders b on a.[dbid] = b.[manifest]
group by DBID, today
order by dbid desc
Pretty sure that because your query is in effect joining orders twice it's increasing the count.
Use this :
select a.DBID, a.today, count(b.amazon) from [manifest] a
join orders b on a.[dbid] = b.[manifest] and b.amazon = 2
Group By a.DBID, a.today

Display Y/N column if record found in detail table

I'm trying to create a query so that I can have a column show Y/N if a particular item was ordered for a group of orders. The item I'm looking for would be OLI.id = '538'.
So my results would be:
Order#, Customer#, FreightPaid
12345, 00112233, Y
12346, 00112233, N
I cannot figure out if I need to use a subquery or the where exists function ?
Here's my current query:
SELECT distinct
OrderID,
Accountuid as Customerno
FROM [SMILEWEB_live].[dbo].[OrderLog] OL
inner join Orderlog_item OLI on OLI.orderlogkey = OL.[key]
inner join Account A on A.uid = OL.Accountuid
where A.GroupId = 'X9955'
and OL.CreateDate >= GETDATE() - 60
I would suggest an exists clause instead of a join:
select ol.OrderID, ol.Accountuid as Customerno,
(case when exists (select 1
from Orderlog_item OLI join
Account A
on A.uid = OL.Accountuid
where OLI.orderlogkey = OL.[key] and A.GroupId = 'X9955'
)
then 1 else 0
end) as flag
from [SMILEWEB_live].[dbo].[OrderLog] OL
where OL.CreateDate >= GETDATE() - 60;
This prevents a couple of problems. First, duplicate rows which are caused when there are multiple matching rows (and select distinct add unnecessary overhead). Second, missing rows, which happen when you use inner join instead of an outer join.

select count () how to add column

This request gives me the count of the request occurrences where EMAIL occures.
select count(*)
from ADRESS K left outer join ADRESS L
on K.LFDNRSECONDADRESS=L.LFDNR
left outer join ADRESS V
on K.VERLFDNR=V.LFDNR
where ((UPPER(K.EMAIL)= 'my#email.com'
or exists (select ADRESSEMAILADR.LFDNR
from ADRESSEMAILADR
where ADRESSEMAILADR.ADRESSLFDNR=K.LFDNR
and UPPER(ADRESSEMAILADR.EMAIL)=
'my#email.com' )
)) and K.ART='K'
But I also would like go get all occurrences of the column "LFDNR".
Like
3
1234
2345
3456
...
So the first is the count and the followingup are the results of all columns where LFDNR = X.
Of cause I tried
LFDNR, select count(*)
K.LFDNR, select count(*)
And so on...
No luck so far.
If I understand correctly, you want group by:
select k.LFDNR, count(*)
from ADRESS K left outer join
ADRESS L
on K.LFDNRSECONDADRESS = L.LFDNR left outer join
ADRESS V
on K.VERLFDNR = V.LFDNR
where (UPPER(K.EMAIL)= 'my#email.com' or
exists (select ADRESSEMAILADR.LFDNR
from ADRESSEMAILADR
where ADRESSEMAILADR.ADRESSLFDNR = K.LFDNR and
UPPER(ADRESSEMAILADR.EMAIL) = 'my#email.com'
)
) and
K.ART = 'K'
group by k.LFDNR;