I am trying to return values from a query adding a column for the row position without adding an identity column. I don't want the absolute position in the table, but the position in the query result
Suppose I have a table like this
My_TBL
-----------------------
FLD_A FLD_B FLD_C
a A t
b B t
c C p
d D p
.. ..
and the select query is
select FLD_A,FLD_B from My_Tbl where FLD_C='p'
FLD_A FLD_B
-----------------------
c C
d D
What do I have in Db2 to add in my query to get each row counted in that output?
POS FLD_A FLD_B
-----------------------
1 c C
2 d D
Use row_number(). It will only count the rows that are actually returned.
select row_number() over (order by FLD_A,FLD_B) as POS,
FLD_A,
FLD_B
from My_Tbl
where FLD_C='p'
Related
I have a table with id and different values. I want to have my output something which would looks like this
id value
----------
1 t
1 f
2 t
3 f
4 f
4 f
Expected output
id value
---------
3 f
4 f
If we look at the output, my condition here to check if my id has all f as value then return f, if it has all t then don't, and if any one of the id has t also don't include row in output.
How to achieve this ?
create subquery and exclude the values accordingly. i think hiveql supports where clause subqueries.
select id, value
from your_data_source
where id not in
(select id from your_data_source where value='t' group by id)
group by id, value
I am very confused how to define the problem statement but Let's say below is table History i want to find those rows which have a pair.
Pair I will defined like column a and b will have same value and c should have False and d should be different for both row.
If I am using Java i would have set row 3, C column as true when i hit a pair or would have saved both row 1 and row 3 into different list. So that row 2 can be excluded. But i don't know how to do the same functionality in SQL.
Table - History
col a, b, c(Boolean ), d
1 bb F d
1 bb F d
1 bb F c
Query ? ----
Result - rows 1 and 3.
Assuming the table is called test:
SELECT
*
FROM
test
WHERE id IN (
SELECT
MIN(id)
FROM
test
WHERE
!c
AND a = b
AND d != a
GROUP BY a, d
)
We get the smallest id of every where matching your conditions. Furthermore we group the results by a, d which means we get only unique pairs of "a and d". Then we use this ids to select the rows we want.
Working example.
Update: without existing id
# add PK afterwards
ALTER TABLE test ADD COLUMN id INT PRIMARY KEY AUTO_INCREMENT FIRST;
Working example.
All the rows match the conditioin you specified. A "pair" happens when:
column a and b will have same value, and
c should have False, and
d should be different for both rows.
1 and 3 will match that as well as 2 and 3. Also, 3 and 1 will match as well as 3 and 2. There are four solutions.
You don't say which database, so I'll assume PostgreSQL. The query that can search using your criteria is:
select *
from t x
where exists (
select null from t y
where y.a = x.a
and y.b = x.b
and not y.c
and y.d <> x.d
);
Result:
a b c d
-- --- ------ -
1 bb false d
1 bb false d
1 bb false c
That is... the whole table.
See running example at DB Fiddle.
I am trying to make a sum of the count of different values.
Here's an example :
a
a
a
b
b
b
c
c
c
d
d
d
d
e
e
e
e
The output would be :
5
Because there's 5 different values in that column.
Perform a Distinct Count which should give you the count of distinct values in a column and no need to do sum here
select count(distinct colname)
from yourtable
After searching and giving some good tought and testing here's the correct query :
SELECT SUM(uniqueValues) FROM (SELECT COUNT(DISTINCT values) as uniqueValues FROM tablename GROUP BY values)
I have a table like this, A and B as columns:
A B
1 0
2 1
3 2
4 3
A record can be selected by defining a value for A. If the selected row has a value for B, the row whose A's value is equal to the record's B must also be selected, and if that selected record has a B it must again be selected and so on.
Example:
If the user queries for A = 3, the returned rows must be:
A B
3 2
2 1
1 0
This is the output because of this condition: A3 has a value for B; there is a record whose A is equal to the first row's B which is 2 and the second record's B is 1 which still has a matching record.
Is there a way this can be done without looping through the records?
You can use recursive CTE to achieve this:
http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
;WITH RCTE AS
(
SELECT * FROM Table1 WHERE A = 3
UNION ALL
SELECT t.* FROM RCTE r
INNER JOIN Table1 t ON r.B = t.A
)
SELECT * FROM RCTE
SQLFiddle DEMO
I want to write a query to achieve the following. I have a table xyz in which there are multiple row with same column value(1) in say column a.
I want to find in column b doesn't have a particular value for the set of rows with value 1 in column a.
Table xyz
---------
a b
1 te
1 we
1 re
2 te
2 re
3 ge
4 re
So basically I want to find if the column b does not have the value 'te' for a set of values from column a
when i do
Select a from xyz where b <> 'te'
group by a
I will get 1,2,3 and 4 both for the result.
But I want the result should only contain 1 and 2. Please help.
Select a from xyz where (b<>'te') and ((a=1) or (a=2))
or as variant
select a from xyz where (b<>'te') and (a in (1, 2))
select a from xyz
where b! = 'tz' and
a in (select a from xyz where b = 'tz')
Is this what you are looking for?
Try this for you:
Select a from xyz where b = 'te'
group by a
I just realized I didn't and still don't understand what you are asking. Could you try and restate it? The only non-trivial interpretation I can come up with that would return 1 and 2 based on this data would be:
What are the values of a such that there is a row with both a and
'te' and a row with both a and a value other than 'te'
in which case a query would be:
SELECT DISTINCT q1.a FROM (SELECT a FROM xyz WHERE b='te') q1
JOIN (SELECT a FROM xyz WHERE b!='te') q2 ON
q1.a=q2.a
The interpretation which corresponds with returning 3 and 4 in your example or with returning 1 and 2 in your geo example would be:
What are the values of a for which a te row does not exist?
in which case a query would be:
SELECT DISTINCT a FROM xyz WHERE a NOT IN (SELECT a FROM xyx WHERE b='te')
as shown here (sqlfiddle is acting up, so I used ideone)