i'm using this query to select count of comments on other table but it give me error
SELECT
dbo.tblEmails.id, dbo.tblEmails.eTitle, dbo.tblEmails.ePreDesc, dbo.tblEmails.eTags, dbo.tblEmails.eFaDate, dbo.tblEmails.eViewCount,
dbo.tblEmails.ePrice, dbo.tblEmails.eImg, COUNT(tblComments.id) AS cCount
FROM
dbo.tblEmails as tblEmails
INNER JOIN
dbo.tblComments AS tblComments ON dbo.tblEmails.id = dbo.tblComments.PostID
GROUP BY
tblEmails.id, tblEmails.eTitle, tblEmails.ePreDesc, tblEmails.eTags, tblEmails.eFaDate, tblEmails.eViewCount, tblEmails.ePrice, tblEmails.eImg
UPDATE:
error is this :
the text,ntext, and image data types cannot be compared or
stored,except when using IS NULL or LIKE operator.
but i have not image data type in my table
Well, you haven't specified what error text is... But in this particular case it is easy to deduce.
Your problem is incorrect usage of aliases in join and select.
It should be not
INNER JOIN dbo.tblComments AS tblComments ON dbo.tblEmails.id = dbo.tblComments.PostID
but
INNER JOIN dbo.tblComments AS tblComments ON tblEmails.id = tblComments.PostID
And the same story is about select - not dbo.tblEmails.id but tblEmails.id since you've specified alias.
But note - using exact table_name as alias to dbo.table_name looks like a bad idea and may lead to confusion (and in fact, it has lead in your case).
Instead consider using short acronyms for aliases, like this:
SELECT
E.id, E.eTitle, E.ePreDesc, E.eTags,
E.eFaDate, E.eViewCount,E.ePrice, E.eImg,
COUNT(C.id) AS cCount
FROM dbo.tblEmails as E
INNER JOIN dbo.tblComments AS C ON E.id = C.PostID
GROUP BY
E.id, E.eTitle, E.ePreDesc, E.eTags,
E.eFaDate, E.eViewCount,E.ePrice, E.eImg
Do a sub-select to get the count:
SELECT dbo.tblEmails.id, dbo.tblEmails.eTitle, dbo.tblEmails.ePreDesc, dbo.tblEmails.eTags, dbo.tblEmails.eFaDate, dbo.tblEmails.eViewCount,
dbo.tblEmails.ePrice, dbo.tblEmails.eImg,
(select COUNT(*) from dbo.tblComments
where dbo.tblEmails.id = dbo.tblComments.PostID) AS cCount
FROM dbo.tblEmails as tblEmails
Related
I am still fairly new to SQL and the stored procedure I recently created keeps telling me that a subquery is returning more than one result but I can't figure out which one is the problem. If anyone has a moment and can tell me what I am missing, I would greatly appreciate it!
Thanks!
SELECT DISTINCT a.customer_no [id],
x.esal1_desc [constituent],
a.perf [activity],
a.sp_act_dt [activity_date],
c.description[activity_type],
d.display_name_tiny [solicitor],
s.description [status],
ISNULL(a.num_attendees,0)[attending],
a.notes [notes],
e.address [email]
FROM [dbo].t_special_activity a
left outer join [dbo].tr_special_activity_status s ON s.id = a.status
left outer join [dbo].tr_special_activity c ON c.id = a.sp_act
left outer JOIN [dbo].FT_CONSTITUENT_DISPLAY_NAME() d ON a.worker_customer_no = d.customer_no
left outer JOIN [dbo].T_EADDRESS e on a.customer_no=e.customer_no and primary_ind='Y'
left outer JOIN [dbo].TX_CUST_SAL x on a.customer_no=x.customer_no and default_ind='Y'
WHERE a.status IN (ISNULL(#status, (SELECT DISTINCT id FROM TR_SPECIAL_ACTIVITY_STATUS)))
AND a.sp_act_dt BETWEEN (ISNULL(#activity_start,(SELECT MIN(sp_act_dt) FROM T_SPECIAL_ACTIVITY)))
AND (ISNULL(#activity_end,(SELECT MAX(sp_act_dt) FROM T_SPECIAL_ACTIVITY)))
AND ((ISNULL(#list,0) = 0) OR EXISTS (SELECT customer_no FROM T_LIST_CONTENTS lc WITH (NOLOCK)
WHERE a.customer_no = lc.customer_no and lc.list_no = #list))
Alas, you cannot use this expression:
WHERE a.status IN (ISNULL(#status, (SELECT DISTINCT id FROM TR_SPECIAL_ACTIVITY_STATUS)))
The subquery is in a place where a single value is expected. In any case, I think you want:
WHERE #status IS NULL OR
a.status IN (SELECT id FROM TR_SPECIAL_ACTIVITY_STATUS)
Note that select distinct is irrelevant in an IN clause. At best it does nothing; at worst it impedes the optimizer.
I realize this is a little confusing. You are thinking that IN takes a list -- and the list could even be a subquery. But, the elements of the list are scalars not lists. So, when a subquery is an element of the list, then it is assumed to be a single value.
Its been a while since I've done sub queries and for the life of me I cant see whats wrong with my query.
The error message I get when executing is:
ORA-00904: "SUB"."PRO_REFNO": invalid identifier
This is my query. I'm obviously doing something wrong but I just cant see it.
SELECT
prop.PRO_ADR_1_LINE,
ele.POE_START_DATE,
ele.POE_ELEMENT_DESCR,
ele.POE_VALUE,
ele.POE_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE,
ele.POE_FURTHER_ATTRIBUTE_DESCR,
prop.PRO_SCHEME,
prop.PRO_SCHEME_DESCR,
GEO.GEO_BUS_UNIT,
GEO.GEO_REGION,
GEO.GEO_REGION_DESCR,
prop.PRO_NEIGHBOURHOOD_DESCR
--sub.pro_refno
FROM property prop
--inner join
left join GEO on prop.PRO_GEO_PATCH=GEO.GEO_PATCH
left join PROPERTY_OTHER_ELEMENT ele on ele.POE_PRO_REFNO =prop.PRO_REFNO
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO)
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
where
ele.POE_START_DATE BETWEEN '01-APR-2016' AND sysdate
AND
ele.POE_ELEMENT LIKE 'EST%'
AND
ele.POE_ELEMENT_DESCR <> 'Estate Walkabout - Would you live in this neighbourhood ?'
AND
ele.POE_VALUE IN ( '1','2','3','4','5','6','7','8','9','10' )
Both the outer query and sub query run fine separately. Like I said its been a while so I'm guessing its something stupid I've done/not done.
Thanks
Adam
You didn't give a name to the aggregate column:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) -- No name!!!
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
Change that to:
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) As PRO_REFNO
FROM
PROPERTY
WHERE
property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
Your subquery doesn't select PRO_REFNO, so the outer query cannot match it in the JOIN predicate. Try this for the subquery:
SELECT
property.PRO_SCHEME,
property.PRO_REFNO,
count(distinct property.PRO_REFNO)
FROM PROPERTY
WHERE property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME,
property.PRO_REFNO
Also, your COUNT(DISTINCT ...) isn't given an alias, which you'll need if it's ever going to be used in the outer select.
I have the query below working, the thing is I need to only list each unique "VolumeSerialNumber0" once. There's no shortage of questions and approaches to this problem on SO but they suggest using subqueries and group by clause, but when I try to do that I get an error "columnname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I feel like it has to be close I'm just not getting the magical syntax perfectly correct.
SELECT
dbo.v_R_System.Netbios_Name0,
dbo.v_GS_LOGICAL_DISK.TimeStamp,
dbo.v_GS_LOGICAL_DISK.Description0,
dbo.v_GS_LOGICAL_DISK.DeviceID0,
dbo.v_GS_LOGICAL_DISK.DriveType0,
dbo.v_GS_LOGICAL_DISK.Name0,
dbo.v_GS_LOGICAL_DISK.SystemName0,
dbo.v_GS_LOGICAL_DISK.VolumeName0,
dbo.v_GS_LOGICAL_DISK.VolumeSerialNumber0,
dbo.v_GS_PARTITION.Size0,
dbo.v_GS_LOGICAL_DISK.FileSystem0
FROM
dbo.v_R_System
INNER JOIN dbo.v_GS_LOGICAL_DISK
ON dbo.v_R_System.ResourceID = dbo.v_GS_LOGICAL_DISK.ResourceID
INNER JOIN dbo.v_GS_PARTITION
ON dbo.v_GS_LOGICAL_DISK.ResourceID = dbo.v_GS_PARTITION.ResourceID
SELECT
MAX(S.Netbios_Name0),
MAX(L.TimeStamp),
MAX(L.Description0),
MAX(L.DeviceID0),
MAX(L.DriveType0),
MAX(L.Name0),
MAX(L.SystemName0),
MAX(L.VolumeName0),
L.VolumeSerialNumber0,
MAX(P.Size0),
MAX(L.FileSystem0)
FROM
dbo.v_R_System S
INNER JOIN dbo.v_GS_LOGICAL_DISK L
ON S.ResourceID = L.ResourceID
INNER JOIN dbo.v_GS_PARTITION P
ON L.ResourceID = P.ResourceID
GROUP BY
L.VolumeSerialNumber0
I am joining a query with it self and my code is working ok on ase isql. However when I want to use it in access 2007 I get the following error "The derived table expression is missing a correlation name. Check derived table syntax in the reference manual"
The original code does something like this:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname
In the original code I join more tables in the inner query as well as some where conditions. But the above code should illustrate what I do.
It looks like the way I join the table with it self is the problem in access. Does anyone know what How is the correct sysntaxis? Or if access/JET/ACE support this inner join with it self approach?
Here is the original code:
select max(tort140.BEL_GRLAG_AP) as MaxPensjonGr, TT.IDE_KUNDE_PRSNR,
TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr, TT.Arbeidsgiver,
TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
inner join
(select distinct tort128.NUM_AVTALE_ID as AvtaleID,
tort009.IDE_ARBGIV_NR as Orgnr,
tort134.NVN_ARBGIV as Arbeidsgiver,
tort127.DAT_KUNDE_FOEDT_NUM as DAT_KUNDE_FOEDT_NUM,
tort127.IDE_KUNDE_PRSNR as IDE_KUNDE_PRSNR,
tort001.STA_SIVILSTATUS as Sivilstatus,
tort128.typ_status as Polisestatus,
tort128.rte_polisegrad as Polisegrad,
tort140.BEL_LOENN_AAR as Årslønn,
tort138.IDE_SEKV_TORT138"
from tort140 left join (tort138 join (tort128 join (tort134 join (tort009 join (tort001 join tort127
on tort127.DAT_KUNDE_FOEDT_NUM=tort001.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort001.IDE_KUNDE_PRSNR)
on tort127.DAT_KUNDE_FOEDT_NUM=tort009.DAT_KUNDE_FOEDT_NUM and tort127.IDE_KUNDE_PRSNR=tort009.IDE_KUNDE_PRSNR)
on tort009.IDE_ARBGIV_NR=tort134.IDE_ARBGIV_NR)
on tort128.IDE_SEKV_TORT127 = tort127.IDE_SEKV_TORT127)
on tort128.IDE_SEKV_TORT128 = tort138.IDE_SEKV_TORT128)
on tort140.IDE_SEKV_TORT138 = tort138.IDE_SEKV_TORT138)
where tort128.NUM_AVTALE_ID = '102356' and tort128.DAT_GYLDIG_FOM <= 20120101
and (tort128.DAT_GYLDIG_TOM >= 19520000 or tort128.DAT_GYLDIG_TOM is null
and tort128.DAT_HISTORISK is null and tort128.TYP_STATUS= 'akt' and tort127.DAT_KUNDE_FOEDT_NUM >= 19650000
and tort127.DAT_KUNDE_FOEDT_NUM <= 19550000 and tort127.DAT_TERMINERT is null and tort127.DAT_REGISTRERT<= 19550000
and tort009.DAT_SLUTT is null and tort134.DAT_HISTORISK is null
and tort138.DAT_AKSJON=(select max(p.DAT_AKSJON) from tort138 p where 1=1 and p.IDE_SEKV_TORT128=tort128.IDE_SEKV_TORT128)
) as TT
on TT.IDE_SEKV_TORT138 = tort140.IDE_SEKV_TORT138
Group by TT.IDE_KUNDE_PRSNR, TT.DAT_KUNDE_FOEDT_NUM, TT.AvtaleID, TT.Orgnr,
TT.Arbeidsgiver, TT.Sivilstatus, TT.Polisestatus, TT.Årslønn from tort140
The inner query works on access 2007 without any problem. I got the error message when I write the inner join.
Do you think this query can be suited into access?? Am I missing some brakets or something?
You have to add a join type in MS Access, Join on its own will not work.
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
Edit per comments, the line above is taken from the posted SQL and fits back in like so:
select TT.name, TT.lastname, max(amount) as maxsalecurrentweek
from sales
inner join
(select s1.id_employee, e.name, e.lastname, e.address, e.age, e.id_employee
from employee e
INNER join sales s1 on e.id_emplyoee = s1.id_employee
where
some conditions here) as TT
on sales.id_employee = TT.id_employee
group by
TT.name, TT.lastname
Still trying to get used to writing queries and I've ran into a problem.
Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)
The inner query gives an ID number in one column, the amount of times they appear in the table, and then a bit attribute if they are in region A. The outer query works but the error I get is incorrect syntax near the keyword IN. Of the inner query, I would like a number of how many of them are in region A
You must specify table name in query before where
Select count(region)
from table
where (regionTable.A=1) in
And you must choose one of them.
where regionTable.A = 1
or
where regionTable.A in (..)
Your query has several syntax errors. Based on your comments, I think there is no need for a subquery and you want this:
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A
which can be further simplified to:
select jxn.id, count(jxn.id) as counts
, 1 as A --- you can even omit this line
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
You are getting the error because of this line:
where (regionTable.A=1)
You cannot specify a condition in a where in clause, it should only be column name
Something like this may be what you want:
SELECT COUNT(*)
FROM
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from
jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
) sq
WHERE sq.a = 1