In often use in TSQL the following query :
SELECT COUNT(*), *
FROM CUSTOMER c
WHERE c.Name like 'foo%';
When I try to execute this query in Oracle SQL Developer it doesn't work and throws me an error:
"Missing expression"
What is the good syntax ?
Thanks in advance.
This will perform better:
SELECT COUNT(*) OVER (), c.*
FROM CUSTOMER c
WHERE c.Name like 'foo%';
One approach is to do something like the following. This will result in a count(*) result for each line. But beware, there is a Cartesianjoin; if you have many rows like 'foo%' this will perform badly.
select a.cntr, c.*
from CUSTOMER c
, (select count(*) cntr
from customer b
where b.name like 'foo%' ) a
where c.name like 'foo%'
here below MySQL code is the way to get both select * from and total rows returned in a single query.
$result = $db->get_results("SELECT a.total, c.* FROM tableName c,
(select count(*) total from tableName b where b.post=25) a
where c.post=25");
then you can get variable total to get count, and * can use for based on your table column.
But how to get the total without foreach() or directly into a vaiable?
I go it total without foreach Here:
echo "Total: ".count($result);
Related
I'm struggling with this query:
SELECT *
FROM Transactions
WHERE CustomerID IN (SELECT ID FROM Customers
WHERE Name LIKE '%Test%')
It takes 10 seconds to run, however if I create the query manually by taking the 4 values returned by the sub query it runs in milliseconds, for example:
SELECT *
FROM Transactions
WHERE (CustomerID = 1 OR CustomerID = 2 OR
CustomerID = 3 OR CustomerID = 4)
To clarify, running
SELECT ID FROM Customers WHERE Name LIKE '%Test%'
returns the values 1,2,3,4 immediately
Any ideas? What am I missing?
As you already said, when you have the customer id's it runs in milliseconds so the filtering of the customer name is the problem.
The first wildcard (WHERE name LIKE '%Test%') is the suspect here because sql server needs to read all the strings in the name column like a regular expression and find if there is any "Test" in there for every row in the table!
If the names you are filtering for would always start with a "Test" and you could do a WHERE name LIKE 'Test%' it would work much better because sql server only needs to read the start of each string.
Edit:
Here is a little bit different version of the original query if you want to try:
SELECT * FROM Transactions t
WHERE EXISTS (
SELECT 1 FROM
Customers c
WHERE c.ID = t.CustomerID
AND c.Name LIKE '%Test%'
)
What happens with a join?
SELECT t.*
FROM Transactions t JOIN
Customers c
ON t.CustomerID = c.ID
WHERE c.Name Like '%Test%';
Sometimes, JOINs optimize better than IN.
Can I use query inside the like operator.
I used the query below, but it returns error.
select *
from customers
where cust_name like '%'||select name from members||'%'
Something like the following should work :
SELECT
*
FROM
customers c
WHERE 1=1
AND EXISTS
(SELECT 1
FROM members m
WHERE 1=1
AND c.cust_name LIKE '%'||m.name||'%'
)
In PLSQL if you want to run a Query then you need to decalre a variable to hold the result of the query. So you can do it as :
DECLARE
var customers%ROWTYPE;
BEGIN
SELECT c.*
INTO var
FROM customers c
INNER JOIN members m ON c.cust_name LIKE '%' || m.name || '%';
END;
it is sufficient to enclose the subquery in parentheses
like as
cust_name like '%'||(select name from members)||'%'
but this only works when the table "members" has only one record
You should define the name part of members in subselect (works with only 1 row):
select *
from customers
where cust_name like (select '%'||name||'%' from members);
Same can be done with JOIN (works with multiple rows):
select C.*
from customers C
INNER JOIN MEMBERS M ON (C.CUST_NAME LIKE '%'||M.NAME||'%');
At http://www.dofactory.com/sql/sandbox I'm experimenting with submitting my own SQL queries against their sample database to become better at SQL. What I want to do is to select all countries from Customer that have exactly the fewest number of tuples. Here is my query attempt:
SELECT a.Country
FROM [Customer] a, (SELECT COUNT(*) AS Tot
FROM [Customer]
GROUP BY Country) b
GROUP BY a.Country
HAVING COUNT(*) = MIN(b.Tot)
However, the website returns an empty table instead of the correct result which is (Ireland, Norway, Poland). The correct result is easily realized by grouping the table by country and using COUNT(*), and then looking at the countries that have the smallest COUNT(*) value out of all COUNT(*) values. I would like some advice on how to generate the correct result without any assumptions about the table's data.
I would do this using SELECT TOP 1 WITH TIES:
SELECT TOP 1 WITH TIES c.Country
FROM Customer c
GROUP BY c.Country
ORDER BY COUNT(*) ASC;
Two notes:
When using table aliases, make them abbreviations for the tables. This makes the query much easier to follow.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
Learned somtihing new(WITH TIES) from Gordon Linoff, again...
Here my solution without it...
Select a.Country from [Customer] a
group by a.Country
having count(*) = (select min(b.Tot) from (SELECT COUNT(*) AS Tot FROM [Customer] GROUP BY Country) b)
If you are not using sql 2012 then,
declare #Fewer int=2
;With CTE as
(
select c.*
,ROW_NUMBER()over(partition by countryid order by customerid)rn
from dbo.Customers C
)
select * from cte
where rn<=#Fewer
I'm trying to select rows from a table with the exception of any rows containing 'NEW_' at the beginning. I think I have the logic correct but I am unsure of the syntax. Can anyone help me out?
SELECT *
FROM CUSTOMER e1
WHERE e1.cust_ref LIKE 'CUST_REF%'
AND e1.cust_ref NOT IN (SELECT e2.cust_ref
FROM CUSTOMER e2
WHERE e1.cust_ref = 'NEW_' + e2.cust_ref);
select * from CUSTOMER
WHERE cust_ref like 'CUST_REF%'
AND cust_ref not in
(select cust_ref from sd_filter_element where cust_ref not like 'NEW_%');
You should use like to do this.
If you just want cust refs that don't have "NEW_", why not just do this?
select c.*
from CUSTOMER c
WHERE c.cust_ref not like 'NEW_%' ;
Here is SQL query for filter option
select *
from sd_filter_element
WHERE
cust_ref not like 'NEW_%';
I have trouble with converting native SQL query to HQL.
Query is something like follows:
select count(*)
, sum(select count(*) from employee e where e.company_id=c.id))
from company c where c.id = someID
First returned value is count of companies, second - amount of employees for specified company.
I.e. I have to get this two values for company with id=someID.
The trouble is hibernate doesn't support subselects in SELECT section, only in WHERE - by specification.
Actually I can:
1) use native query to run this through EntityManager
2) do the same dividing this "complex" query to two simpler SQL queries
But may be there are exist more convenient methods to realize initial query in HQL? - this is a question.
Thank you.
this might work:
select count(c.*) as count_companies
, c as count_emp
from company c
inner join
(
select company_id, count(*) as c
from employee
where employee.company_id = someID
group by company_id
) e
on e.company_id=c.id
group by c.id