Can I actually use sphinxQL with SphinxSE plugin?
Like this:
SELECT * FROM t1 WHERE query='SELECT id FROM products GROUP BY region, price';
Nope. Two completely different ways of accessing sphinx.
SphinxSE under the hood uses the SphinxAPI.
SELECT * FROM t1 WHERE query=';select=id;index=products;groupby=attr:region,price';
Although I dont know if the multiple attribute group by will work.
Related
I have an access to a database, and sure I can get all tables/columns accessible for me just using:
select * from ALL_TAB_COLUMNS
I can also access some tables using "#", as I understand a database link mechanism, like this:
select * from aaa.bbb_ddd#ffgh where jj = 55688
where aaa.bbb_ddd#ffgh corresponds to some table with a column jj
BUT I don't see this aaa.bbb_ddd#ffgh table in ALL_TAB_COLUMNS.
How can I request all tables (and columns inside them) accessible for me via these database links (or so)?
You can't, easily, get all columns accessible via all database links; you can get all columns accessible via one database link by querying ALL_TAB_COLUMNS on the remote database
select * from all_tab_columns#<remote_server>
where <remote_server> in your example would be ffgh.
If you want to get this same information for all database links in your current schema, you'd either have to manually enumerate them and UNION the results together:
select * from all_tab_columns#dblink1
union all
select * from all_tab_columns#dblink2
Or, do something dynamically.
As Justin says, it's clearer if you add which database the data is coming from; you can do this either by just writing it in the query:
select 'dblink1' as dblink, a.* from all_tab_columns#dblink1 a
union all
select 'dblink2', a.* from all_tab_columns#dblink2 a
Or by using an Oracle built-in to work, for example the GLOBAL_NAME table (there's lots more ways):
select db1g.global_name, db1a.*
from all_tab_columns#dblink1 db1a
cross join global_name#dblink1 db1g
union all
select db2g.global_name, db2a.*
from all_tab_columns#dblink2 db2a
cross join global_name#dblink2 db2g
I have created a database as part of university assignment and I have hit a snag with the question in the title.
More likely I am being asked to find out how many films each company has made. Which suggests to me a group by query. But I have no idea where to begin. It is only a two mark question but the syntax is not clicking in my head.
My schema is:
CREATE TABLE Movie
(movieID CHAR(3) ,
title CHAR(36),
year NUMBER,
company CHAR(50),
totalNoms NUMBER,
awardsWon NUMBER,
DVDPrice NUMBER(5,2),
discountPrice NUMBER(5,2))
There are other tables but at first glance I don't think they are relevant to this question.
I am using sqlplus10
The answer you need comes from three basic SQL concepts, I'll step through them with you. If you need more assistance to create an answer from these hints, let me know and I can try to keep guiding you.
Group By
As you mentioned, SQL offers a GROUP BY function that can help you.
A SQL Query utilizing GROUP BY would look like the following.
SELECT list, fields, aggregate(value)
FROM tablename
--WHERE goes here, if you need to restrict your result set
GROUP BY list, fields
a GROUP BY query can only return fields listed in the group by statement, or aggregate functions acting on each group.
Aggregate Functions
Your homework question also needs an Aggregate function called Count. This is used to count the results returned. A simple query like the following returns the count of all records returned.
SELECT Count(*)
FROM tablename
The two can be combined, allowing you to get the Count of each group in the following way.
SELECT list, fields, count(*)
FROM tablename
GROUP BY list, fields
Column Aliases
Another answer also tried to introduce you to SQL column aliases, but they did not use SQLPLUS syntax.
SELECT Count(*) as count
...
SQLPLUS column alias syntax is shown below.
SELECT Count(*) "count"
...
I'm not going to provide you the SQL, but instead a way to think about it.
What you want to do is select where the company matches and count the total rows returned. That count is the number of films made by the specified company.
Hope that points you in the right direction.
Select company, count(*) AS count
from Movie
group by company
select * group by company won't work in Oracle.
I am using a PostgreSQL database. I have one select query:
select userid, name, age from tbluser;
Now there is another table, tblcalculatedtax, which is generated on the fly and their column names are not predefined, the only mapping between that table and this table is userid. I want to get records after joining two tables. How can I get that?
Simpler:
SELECT *
FROM tbluser
JOIN tblcalculatedtax USING (userid)
Details in the fine manual about SELECT.
Your need SQL Joins. Here's a W3Schools tutorial: http://www.w3schools.com/sql/sql_join.asp
To quickly answer your question, though:
SELECT * FROM tbluser
INNER JOIN tblcalculatedtax
ON tbluser.userid=tblcalculatedtext.userid
The * selects all the columns, so you don't need to know their names. Of course, I'm not sure what use a column is to you if you don't know it's name: do you know what data it contains?
I have a table with 3 columns.
ORDER CATEGORY
NAV_PER_SHARE
Number_OUTSTANDING_SHARES
Now:
SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it runs fine
But:
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it says : Not a group by expression .
What am I missing?
You caNnot group because NAV_PER_SHARE is given per result. Did you mean
SUM(NAV_PER_SHARE*OUTSTANDING_SHARES)
?
From what it looks like, the solution would be to use a subquery.
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM_OF_OS
FROM (SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) SUM_OF_OS
GROUP BY ORDER_CATEGORY);
Although, I'm not sure how your one query works without a FROM keyword.
My 'VIEW' is ordered. But when I use a select to show its result, the output is shuffled.
This behavior also happens with the following statement
SELECT * FROM (SELECT * FROM TABLE ORDER BY COLUMNA) AS DERIVEDTABLE
How to prevent this shuffling?
Unfortunately, views can't be ordered in SQL Server. You have to do it on the select statement reading the view.
Lame, I know.
you must only have an ORDER BY on the outermost select that uses the view:
SELECT * FROM (SELECT * FROM TABLE) AS DERIVEDTABLE ORDER BY YourColumn
unless you use TOP
SELECT * FROM (SELECT top 1000000 * FROM TABLE ORDER BY COLUMNA) AS DERIVEDTABLE
however the final sort order could still change depending on the outer query, unless you add another order by:
SELECT * FROM (SELECT top 1000000 * FROM TABLE ORDER BY COLUMNA) AS DERIVEDTABLE
ORDER BY COLUMNA
You cannot. The only way to specify an order is to ask for it at the outermost level of the query - that's the only place where ORDER BY is intended to order the final result set. Any other use of ORDER BY is only intended to assist with other operations (such as to define what the "TOP 10" are when using TOP).
SQL Server 2000 used to be able to be tricked into applying an ORDER BY in a view, and unfortunately, the view designers in Enterprise Manager and SSMS continue to pretend that this works. It doesn't. By not allowing the trick to work any longer, this presumably offers more opportunities for query optimization.