I am getting 500 for following query
SELECT count(*), count(DISTINCT(member)) FROM datasetName
If i run each count in separate query it works fine
Which Couchbase Server version are you using?
If 6.0 then try the following workaround:
SELECT t1.c1, t2.c2
FROM
(SELECT count(*) c1 FROM test) t1,
(SELECT count(DISTINCT(member)) c2 FROM test) t2
If 6.5 then your query should work as is.
Related
i have q sql query where i am fetching data using subquery and left join. it is working properly on sql server but when i use this query in hibernate using HQL it says unexpected token (;
below is my query which is working fine on sql server -
SELECT IH.vendorName, IH.C, IHP.CP FROM (SELECT vendorName, count(*) as C
FROM InvoiceHeader GROUP BY vendorName) IH LEFT JOIN (SELECT vendorName,
count(*) AS CP FROM InvoiceHeader WHERE invoiceStatus='Processed' GROUP BY
vendorName) IHP ON IHP.vendorName=IH.vendorName ORDER BY IH.C DESC
here i am trying to convert my sql query to HQL
Query q = sessionFactory.getCurrentSession().createQuery("SELECT
IH.vendorName, IH.C, IHP.CP FROM (SELECT vendorName, count(*) as C FROM
InvoiceHeader GROUP BY vendorName) IH LEFT JOIN (SELECT vendorName, count(*)
AS CP FROM InvoiceHeader WHERE invoiceStatus='Processed' GROUP BY
vendorName) IHP ON IHP.vendorName=IH.vendorName ORDER BY IH.C DESC");
but i am getting this error at
org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:41: unexpected token: (
org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:61: unexpected token: count
Here's partially how to do it.
First, your query needs to be simplified, because the joins are not at all efficient. MSSQL does not support FILTER, but Modern SQL shows us a sufficent alternative.
SELECT vendorName, sum(case when invoiceStatus = 'Processed' then 1 end) as CP, count(*) as C FROM InvoiceHeader GROUP BY vendorName ORDER BY C DESC;
Secondly you could use createNativeQuery if it turns out to be impossible to translate it to HQL, but this is my attempt to do so:
SELECT I.vendorName, COUNT(I) as C, SUM(CASE WHEN I.invoiceStatus = 'Processed' THEN 1 END) AS CP FROM InvoiceHeader I GROUP BY I.vendorName ORDER BY C DESC
It is important you use the table alias, so that it will resolve properly within the query, even though it's the only table.
Is it that pgadmin 4 does not support the execution of multiple select queries?
I tried executing this query
select cust, prod
from sales;
select *
from sales
it only showed me one table
You are missing semicolon after first query which is incorrect SQL syntax.
select cust, prod
from sales;
select *
from sales;
FYI, do not expect two separate results after executing it in query tool, you will only get result from last query only.
Updates for WITH clause question.
with Min_cust as (
select cust, max(quant),min(quant) from sales group by cust
),
FinalMin as (
select sales.cust,sales.prod,minium from sales
natural join Min_cust as A where sales.quant=A.min
)
select * from FinalMin;
I'm migrating from an Oracle database to SQL Server 2012. Some SQL which works well in Oracle doesn't work with SQL Server.
The following is my SQL and the error.
SELECT
SUM(COUNT(DISTINCT dfc.rentalNumber))
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
GROUP BY
dfc.rentalNumber
and the error is
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
Remove the SUM from the query, as it has no use, since you are also using GROUP BY. The results will be the same as your original query.
SELECT
COUNT(DISTINCT dfc.rentalNumber)
FROM DueFromClient dfc
WHERE dfc.facilityId = facilityId
AND dfc.isRentalComponent = 1
GROUP BY dfc.rentalNumber
This doesn't make much sense, so I suggest also adding the rentalNumber to the SELECT in order to make sense of your data and to also make full use of the GROUP BY.
SELECT
COUNT(DISTINCT dfc.rentalNumber)
, dfc.rentalNumber
FROM DueFromClient dfc
WHERE dfc.facilityId = facilityId
AND dfc.isRentalComponent = 1
GROUP BY dfc.rentalNumber
You don't need to sum count and group by. Try this:
SELECT
COUNT(DISTINCT dfc.rentalNumber)
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
sqlfiddle for this query
All of proposed below are suboptimal and returns same result as query above, but they may meet needs of #Sachi Pj with using of same construction as in original query with SUM(COUNT(DISTINCT()) two more options:
SELECT SUM(dfc2.rentalNumber)
FROM
(
SELECT
COUNT(DISTINCT dfc.rentalNumber) rentalNumber
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
) AS dfc2
GROUP BY dfc2.rentalNumber
sqlfiddle for this query
And without GROUP BY since doubles was eliminated by distinct:
SELECT SUM(dfc2.rentalNumber)
FROM
(
SELECT
COUNT(DISTINCT dfc.rentalNumber) rentalNumber
FROM
DueFromClient dfc
WHERE
dfc.facilityId=:facilityId
AND dfc.isRentalComponent = 1
) AS dfc2
sqlfiddle for this query
You can compare with your original query sqlfiddle to being sure what the results are same.
When I run select from linked SQL Server 2012 like this:
select A.*
from A, (select TOP 1 * from B) as B
where A.test in (B.col1, B.col2)
I only get 1 row from A though A has many rows. B here is parameters table with only 1 row.
A and B here is synonyms which point to tables on remote server.
We know that TOP without ORDER BY may produce non-deterministic results, but if we have only 1 row in table it must be OK. However, the MS SQL Server 2012 in case from question do his job very bad. To avoid such problem you have to write ORDER BY in nested table (B)! After that, query give right results. So, right query must be:
select A.*
from A, (select TOP 1 * from B ORDER BY <any field>) as B
where A.test in (B.col1, B.col2)
Take into account that SQL Server do well in case with local server but working differently in case of linked server.
In your given query, the join structure is considered a
CROSS JOIN
, so by adding a filter in the
WHERE
clause you are effectively changing the join criteria to an
INNER JOIN
following this join structure. Also everyone else who will maintain your code will be better off if you use the ANSI-92 SQL Standard.
Consider a query like this:
SELECT A.*
FROM A
INNER JOIN
(
SELECT col1 AS test FROM B
UNION
SELECT col2 AS test FROM B
) C
ON A.test = C.test
I'm just getting started learning SQL Server Management Studio. I'm familiar with building nested queries in Access, e.g.:
Query1 (get a data set)
select t1.a,t2.b,t1.abc,t2.def from tbl_FNA t1
inner join
tbl_DMZ t2 on
t1.b=t2.b
Query2 (return only the rows from Query1 with the minimum value of b)
select q1.* from Query1 q1
inner join
(select a,min(b) as min_b from Query1 group by a) q2
on
q1.a=q2.a
and
q1.b=q2.min_b
This makes it easier to debug the code because if there's something wrong with Query1, I only have to change it in one place instead of 3 places.
I'm aware that if I have write access to the database, I can create views and/or stored procedures that can help with this. But in this case, I have read-only access.
Is there a way in SSMS that I can save a query and then refer to that query by name inside another query?
You can do this inside the 'session' using a CTE, but without write access you can't save any objects to the database.
WITH q2 AS
(select a,min(b) as min_b from Query1 group by a)
select q1.* from
Query1 q1
inner join
q2
on
q1.a=q2.a
and
q1.b=q2.min_b
SELECT * FROM q2
SELECT TOP 1000 * FROM q2