Getting count over() value after distinct executed - sql

I have a query such as
select
distinct
t.*,
t.name + ' ' + t.lastname as customername
count(t.id) over() as count
from
table t
inner join othertable o
on t.id = o.tableid
where t.blah = 'aaa'
The problem is that the count over() calculates the results before the distinct is executed and therefore returns an incorrect value.
I could remove the distinct and use group by but this will give the count for each group and I want the sum of those values.
I could do a subquery but the problem is this query is getting built up inside an application so I'd have to do some string manipulation to add the where clause to the subquery and main sql body.
Is there a way to get the count show the results after the distinct is executed?
Thanks

This should solve the problem
select count(v.col_a) over() as count,
v.*
from (select distinct t.col_a, t.col_b, ... --all columns you need
t.name + ' ' + t.lastname as customername
from table t
inner join othertable o on t.id = o.tableid
where t.blah = 'aaa') v

Use group by, but use the correct expression:
select t.*,
t.name + ' ' + t.lastname as customername
sum(count(t.id)) over() as total_count
from table t inner join
othertable o
on t.id = o.tableid
where t.blah = 'aaa'
group by . . .

Related

Subquery within scalar subquery fails with error ORA-00936 Missing Expression

This is the query that does not work:
SELECT distinct ord.DateOrdered
, (SELECT docno
FROM th_mm_c_orderline_history
WHERE th_mm_c_orderline_history_id
in (SELECT max(th_mm_c_orderline_history_id)
FROM th_mm_c_orderline_history
GROUP BY c_orderline_id )
order by docno,c_orderline_id) as docno
FROM c_order ord
INNER JOIN c_orderline on c_orderline.c_order_id = ord.c_order_id
INNER JOIN th_mm_c_orderline_history
on th_mm_c_orderline_history.c_order_id=ord.c_order_id
It is throwing me ORA-00936 Missing expression error
This query works fine:
SELECT docno
FROM th_mm_c_orderline_history
WHERE th_mm_c_orderline_history_id
in (SELECT max(th_mm_c_orderline_history_id)
FROM th_mm_c_orderline_history
GROUP BY c_orderline_id )
order by docno,c_orderline_id as docno
Just remove the order by clause from the inline select. You can not use orde by clause there. You can use it in the outer select if you need it...This is how you can do all three of them without the error:
SELECT distinct ord.DateOrdered
, (SELECT docno FROM th_mm_c_orderline_history
WHERE th_mm_c_orderline_history_id
in (SELECT max(th_mm_c_orderline_history_id)
FROM th_mm_c_orderline_history
GROUP BY c_orderline_id)
) as docno
FROM c_order ord
INNER JOIN c_orderline on c_orderline.c_order_id = ord.c_order_id
INNER JOIN th_mm_c_orderline_history on th_mm_c_orderline_history.c_order_id=ord.c_order_id
You can use "order by statement" end of the whole select statement. Because of not use the column C_ORDERLINE_ID in select statement, it can be error in order by statement. Try this version in below.
SELECT DISTINCT
C_ORDER.DATEORDER,
(SELECT DOCNO
FROM TH_MM_C_ORDERLINE_HISTORY
WHERE C_ORDER_ID = C_ORDER_ID
AND TH_MM_C_ORDERLINE_HISTORY_ID IN ( SELECT MAX (TH_MM_C_ORDERLINE_HISTORY_ID)
FROM TH_MM_C_ORDERLINE_HISTORY
GROUP BY C_ORDERLINE_ID)) AS DOCNO,
C_ORDER.DOCUMENTNO
FROM C_ORDER
INNER JOIN C_ORDERLINE ON C_ORDERLINE.C_ORDER_ID = C_ORDER_ID
ORDER BY DOCNO, C_ORDERLINE_ID;

Oracle sql using subquery to group multiple row result into one row

I was using a LISTAGG which someone here helped but after a few select field, I started to get an ORA error. I would rather use an Oracle subquery to group multiple row results into one row. In the past, I have used STUFF in sqlserver to do this, how would I do this on Oracle.
I have the following query
select d.id, d.name, d.date_sale, d.address, d.city, d.state, d.zipcode, d.description, d.explanation, d.received_date,
SELECT (dd.my_id, dd.customer_name, dd.category, dd.transaction_date, ';'
) AS GROUPED_COLUMNS
from table1 d
left join table2 dd on d.id = dd.my_id
where d.id =1 and d.isActive =1
You can do:
select d.*
(select list_agg(dd.my_id || dd.customer_name || dd.category || dd.transaction_date, ';') within group (order by transaction_date)
from table2 dd
where d.id = dd.my_id
) AS GROUPED_COLUMNS
from table1 d
where d.id = 1 and d.isActive = 1

How can i eliminate dups?

select C.Id as candidateId,C.Name, C.Phone, Status.ResultStatusText , Status.TimeStamp, Status.notes ,
(Select count(*) from CandidateCallHistory where CandiateId = candidateId) AS numbCalls,
(SELECT SUBSTRING((SELECT ',' + Name
FROM Jobs
WHERE Id in (select value from fn_Split(c.JobIds,','))
FOR XML PATH('')),2,200000)) AS jobsList
from Candidate2 C
outer APPLY (select top 1 CH.CandiateId, CH.ResultStatusText , CH.TimeStamp , CH.notes
from CandidateCallHistory CH
where CH.CandiateId = C.Id
order by TimeStamp desc) as Status
where Status.ResultStatusText <> 'completed' and Status.ResultStatusText <> 'canceled' and c.isactive = 1
I have multiple records in the CandidateCallHistory table and seems this is causing issue with the outer apply ( i may be wrong) as it should only get the most recent record in the table since it selects top 1.
Try to add distinct in first line after select:
select distinct [...]

why are the results of the two queries different

the first query:
SELECT u.id , prop1.id
FROM ( SELECT '9fbc6e9b59504c08ac643752c1e2d033' AS id ,
'|6813dbbfec6241a19b8d2316d2cb2a65,1|' AS customprop
UNION
SELECT 'f2271c45682f45fc84527c4afff0ab16' AS id ,
'|6813dbbfec6241a19b8d2316d2cb2a65,2|' AS customprop
) u
INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY a.Id ) id ,
A.Id propId ,
B.NAME
FROM ( SELECT '6813dbbfec6241a19b8d2316d2cb2a65' AS id ,
CONVERT(XML, '<v>1,职业资格1</v><v>2,职业资格2</v>') AS value
) A
OUTER APPLY ( SELECT Name = N.v.value('.',
'nvarchar(Max)')
FROM A.[VALUE].nodes('/v') N ( v )
) B
) prop1 ON CHARINDEX('|' + prop1.propid + ','
+ CONVERT(NVARCHAR(10), prop1.id)
+ '|', u.customprop) > 0
GROUP BY u.id ,
prop1.id
the second query:
SELECT u.id ,prop1.id, count(*)
FROM ( SELECT '9fbc6e9b59504c08ac643752c1e2d033' AS id ,
'|6813dbbfec6241a19b8d2316d2cb2a65,1|' AS customprop
UNION
SELECT 'f2271c45682f45fc84527c4afff0ab16' AS id ,
'|6813dbbfec6241a19b8d2316d2cb2a65,2|' AS customprop
) u
INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY a.Id ) id ,
A.Id propId ,
B.NAME
FROM ( SELECT '6813dbbfec6241a19b8d2316d2cb2a65' AS id ,
CONVERT(XML, '<v>1,职业资格1</v><v>2,职业资格2</v>') AS value
) A
OUTER APPLY ( SELECT Name = N.v.value('.',
'nvarchar(Max)')
FROM A.[VALUE].nodes('/v') N ( v )
) B
) prop1 ON CHARINDEX('|' + prop1.propid + ','
+ CONVERT(NVARCHAR(10), prop1.id)
+ '|', u.customprop) > 0
GROUP BY u.id ,
prop1.id
sql can be executed on sqlserver 2005 directly.
the first query can produce one item and the second query produce two items.
I think that the two queries should both produce two items.
I have thouht for three days and I really want to konw why.
I'm a Chinese and my English is poor.I hope you can understand my description
Tough question, but the problem is with this line:
INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY a.Id ) id ,
The ORDER BY is ambiguous and consequently, if it is executed multiple times (which it can be because of the INNER JOIN it is contained in), it may not always return the same ordering/assignment. This can cause a latter join condition to only match on one record instead of two, which is what happens in the query plan being used for the version without the count(*) column.
To fix this, you just need to add something to make the ordering assignment unique, like this:
INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY a.Id, B.Name ASC ) id ,
Try it like this, it should work.
Your problem is with the ORDER BY clause of the ROW_NUMBER - since the a.ID is not unique the outcome is unpredictable. Make that unique and your problem will go away - you can use something like
...SELECT ROW_NUMBER() OVER ( ORDER BY newid() ) id...

how to get the count in SQL Server?

I have tried a lot to figure how to get the count from two tables with respect to master table
I have three tables
Using these table values I need to get this output..
Tried but could get the desired result
http://en.wikipedia.org/wiki/Join_(SQL)
SQL - LEFT OUTER JOIN and WHERE clause
http://forums.devshed.com/oracle-development-96/combination-of-left-outer-join-and-where-clause-383248.html
You have to first GROUP BY in subqueries, then JOIN to the main table:
SELECT
a.AttributeId
, COALECSE(cntE, 0) AS cntE
, COALECSE(cntM, 0) AS cntM
FROM
AttributeMaster AS a
LEFT JOIN
( SELECT
AttributeId
, COUNT(*) AS cntE
FROM
EmployeeMaster
GROUP BY
AttributeId
) em
ON em.AttributeId = a.AttributeId
LEFT JOIN
( SELECT
AttributeId
, COUNT(*) AS cntM
FROM
MonthlyDerivedMaster
GROUP BY
AttributeId
) mdm
ON mdm.AttributeId = a.AttributeId
SELECT AttributeId,
(SELECT COUNT(Eid) FROM EmployeeMaster WHERE AttributeMaster.AttributeId = EmployeeMaster.AttributeId) as master_eid,
(SELECT COUNT(Eid) FROM MonthnlyDerivedMaster WHERE AttributeMaster.AttributeId = MonthnlyDerivedMaster.AttributeId) as monthly_eid
FROM AttributeMaster