In other words how do you do something simple like this:
select 1
Or more specifically in the particular problem I'm dealing with, something like this:
SELECT (case when exists (<subquery>) then 1 else 0 end) AS result
So in short is there a way in NHibernate to do a select without having it generate the "FROM table" clause?
You are approaching this problem wrong.
Execute the subquery you are after using a count projection then do the if else logic in code.
I'd turn that <subquery> into a select top 1 id from table style query. Then check for a non-null result. Any NH query will always start from table.
Related
I am trying to get the the sum value of a column but this value changes according to a condition.
If I have a particular value in the column 'splits', the I need to have something like this:
SELECT sum(weight)
from table
WHERE 'STR' in splits
Otherwise I only need:
SELECT sum(weight)
from table
I haven't been able to do this correctly and I really need a bit of help!
This is the only reasonable interpretation I can make of the question:
SELECT (CASE WHEN splits LIKE '%STR%' THEN 1 ELSE 0 END) as has_str,
SUM(weight)
FROM table
GROUP BY (CASE WHEN splits LIKE '%STR%' THEN 1 ELSE 0 END);
If i understand your case corretly, you only need to change the where clause to in() like that:
SELECT sum(weight)
from table
WHERE splits in('STR')
I Dont know which DBMS you are using, but that should work with most of them.
Very new to subqueries and find myself in need of help.
I would like to query from a single database. Within that query, I would like to calculate a variable from two variables with that database (SUBQ and TOTAL). My issue is this: my SUBQ variable needs to be subject to an additional set of WHERE constraints on top of those that will be employed for the whole query. Simplified example code below:
create table [blah]
as select date_part('YEAR',DATE) as Orig_Year,
sum([SUBQ variable])/sum(TOTAL) as UD_Rate
from [database]
where [full query requirements]
group by date_part('YEAR',DATE)
I have tried to create a subquery within that calculation by specifying a subquery in the FROM statement. So, for example,
select date_part('YEAR',DATE1) as Orig_year,
sum(a.SUBQ)/sum(b.TOTAL) as UD_Rate
from database b,
(select SUBQ
from database
where DATE2 is not null and
months_between(DATE3,DATE2) <= 100 and
VALUE1 in ('A','B')) a
where VALUE2 between 50.01 and 100
group by date_part('YEAR',DATE1)
Am I on the right track with my thinking here? I have yet to get anywhere close to a functional query and have had little luck finding a similar question online, so I'm at the point where I've tossed up my hands and come to you. Though I know little about them, would it be more appropriate to create a VIEW with the SUBQ value, and then merge it with the broader query?
Thoughts of pies and cakes for whoever is willing to assist me with this request. Thank you.
I think you just want condition aggregation in a window function. Something like this:
select sum(case when [subquery requirements] then t.subq else 0 end) / sum(t.Total)
from t;
I'm pretty sure this is what you are looking for. In terms of your create table:
select date_part('YEAR',DATE) as Orig_Year,
sum(case when ?? then Total else 0 end)/sum(TOTAL) as UD_Rate
from [database]
where [full query requirements]
group by date_part('YEAR', DATE);
I am guessing that the column to be compared is Total, subject to the conditions in the when.
Use Common-Table-Expression then:
-- Define the CTE expression name and column list.
WITH subquery (Orig_year, UD_Rate)
AS
-- Define the CTE query.
(
select date_part('YEAR',DATE1) as Orig_year,
sum(a.SUBQ)/sum(b.TOTAL) as UD_Rate
from database b,
(select SUBQ
from database
where DATE2 is not null and
months_between(DATE3,DATE2) <= 100 and
VALUE1 in ('A','B')) a
where VALUE2 between 50.01 and 100
group by date_part('YEAR',DATE1)
)
And then use subquery as you would use a table inside your main query
I would like to run a query that uses a column value (in which a tablename is stored) to determine the tablename in a from clause of a subselect.
Something like that:
SELECT column_with_tablename,
(SELECT COUNT(*) FROM VALUEOF(column_with_tablename)) as
numberofitems
FROM table1
I know that this is very fragile but i need it work.
(I inherited a database that stores tablenames in a column)
Is there a way ?
for a pure sql solution, refer to this answer
select column_with_tablename,
to_number(extractvalue(xmltype
(dbms_xmlgen.getxml('select count(*) c from '||column_with_tablename)
),'/ROWSET/ROW/C')) as count
from table1
I would like to query a DB2 table and get all the results of a query in addition to all of the rows returned by the select statement in a separate column.
E.g., if the table contains columns 'id' and 'user_id', assuming 100 rows, the result of the query would appear in this format: (id) | (user_id) | 100.
I do not wish to use a 'group by' clause in the query. (Just in case you are confused about what i am asking) Also, I could not find an example here: http://mysite.verizon.net/Graeme_Birchall/cookbook/DB2V97CK.PDF.
Also, if there is a more efficient way of getting both these results (values + count), I would welcome any ideas. My environment uses zend framework 1.x, which does not have an ODBC adapter for DB2. (See issue http://framework.zend.com/issues/browse/ZF-905.)
If I understand what you are asking for, then the answer should be
select t.*, g.tally
from mytable t,
(select count(*) as tally
from mytable
) as g;
If this is not what you want, then please give an actual example of desired output, supposing there are 3 to 5 records, so that we can see exactly what you want.
You would use window/analytic functions for this:
select t.*, count(*) over() as NumRows
from table t;
This will work for whatever kind of query you have.
I need to do
select * from xxx where name in (a,b,c...);
but I want the result set to be in the order of (a,b,c...). is this possible?
I found this question which is looks like your original question: Ordering by the order of values in a SQL IN() clause
ah - I see. you could do something horrendous with a case statement, and then order by that.. you'd effectivley be adding another column to your query to be an "order" that you could then "order by"
its ugly, but if you control the query, and the number in the 'in' clause is low, it could work (beleive an 'in' clause is limited to 255 chars)
e.g "IF name = a then 1 else if name = b then 2"
Failing that, probably best to sort in the client using a similar technique (assuming it was the client that injected the information into the 'in' clause in the first place)
-Ace
The method to do this will be DB-specific.
In Oracle, you could do something like:
SELECT * FROM xxx
where name in (a,b,c...)
ORDER BY DECODE(name,a,1,b,2,c,3);
IN statements are pretty limited, but you could get a similar effect by joining on a subquery.
here's an example:
SELECT x.*
FROM xxx as x
INNER JOIN ((select a as name, 1 as ord)
UNION
(select b as name, 2 as ord)
UNION
(select c as name, 3 as ord)) as t
ON t.name = x.name
ORDER BY t.ord
its pretty ugly, but it should work on just about any sql database. The ord field explicitly allows you to set the ordering of the result. some databases such as SqlServer support a ROWINDEX feature so you may be able to use that to clean it up a bit.