SQL, incrementing column value returned from SELECT query - sql

SELECT x, y, z FROM table_one
WHERE y='asd'
ORDER BY z ASC;
Hi, I'm querying my database using the query above, upon the return of the query I'd like to increment z by 1 (not update it but just increment it so it shows in the result). I don't want to do an Update statement, this is just temporary and should only be visible in the query result.
How would I go about doing this? It's for a school assignment. I've tried to use REPLACE without any success. What works is changing z to z+1 but then the column name changes to ?column? instead of z.
Any help would be appreciated!

You need add column alias:
SELECT x, y, z + 1 AS z -- here
FROM table_one
WHERE y='asd'
ORDER BY z ASC;

Related

How to bring rounded integer as output by multiplying two columns in SQL

I have two columns A and B in SQL. Column A has value 5.2 and Column B has value 2.1. I am multiplying column A and B in Column C. Answer is 10.92. I would like to get rounded number 11 as Integer in column C instead of 10.92 as float or decimal. How should I write query. Should there be a Cast function or similar?
Sample query is:
SELECT *, A multiply B As C
FROM Table X
You need query like this:
select *, cast(round((a*b), 0) as int) from X
Here you can see on DB FIDDLE
select round((columnA*columnB),0)
from Table

How to use a variable as a column name in DolphinDB?

To simplify my question: start with the following table in DolphinDB:
t=table(1 2 3 as x, 4 5 6 as y)
I would like to select a column from the table, but I prefer to assign the column to choose in a separate statement. I tried the following:
colName= x
select colName from t
and
colName="x"
select colName from t
neither works. I am sure there is a way to do this in DolphinDB. Could someone point out where to look at in the manual? Thanks!
You can take a look at the part about metaprogramming in DolphinDB's manual:
https://www.dolphindb.com/help/Metaprogramming.html
For your question, try this:
colName= "x"
sql(select=sqlCol(colName), from=t).eval()

SQL: Computing sum of all values *and* a sum of only values matching condition

Suppose I fetch a set of rows from several tables. I want to know the total sum of values in column x in these rows, as well as sum of only those values in x where the row satisfies some additional condition.
For example, let's say I fetched:
X Y
1 0
10 0
20 100
35 100
I want to have a sum of all x (66) and x in those rows where x > y (11). So, I'd need something like:
SELECT sum(x) all_x, sum(x /* but only where x > y */) some_x FROM ...
Is there a way to formulate that in SQL? (Note that the condition is not a separate column in some table, I cannot group over it, or at least don't know how to do that.)
EDIT: I use Oracle Database, so depending on Oracle extensions is OK.
You could use a case expression inside the sum:
SELECT SUM (x) all_x,
SUM (CASE WHEN x > y THEN x ELSE 0 END) some_x
FROM my_table
You're looking for the CASE operator :
SELECT sum(X) all_x,
sum(CASE WHEN X>Y THEN X ELSE 0 END) some_x
FROM Table1
In this case (no pun) you would get 11 for some_x
You can use whatever condition you want instead of X>Y after the WHEN, and select whatever value instead of X.
SQL fiddle to test this query.
Below Query will give What you want
select SUM(x) as x,(select SUM(x) from test5 where x>y )as 'X>Y'
from test5

SQL to return one row for each distinct value of a column (do not mind which row)

I have a table with a column named X. X contains number from 0 to 99. But there are duplicates (e.g. 0 is there multiple times! )
Now I need a query that gives any of the rows with 0,1,2,3...99 meaning I get 100 results at with one query, but I don't care which of the x==0 , x==1 ... I get, but just one of them!
Is there such thing in sql?
select distinct x
from your_table
To get a complete record you can group by the X column. But you have to tell the DB which of the duplicate values of the other columns you want.
select x, min(y) as y
from your_table
group by x
If you build a group by X then this value will be distinct. For the other columns you need a so called aggregate function like for example min(). That tells the DB to pick the minimum Y of every X group.

SQL Query returning records that have always the same value

I am learning SQL and doing some practice. I cannot give you the exact scenario because the website I'm practicing on don't want any solution to be found directly on the web so I'll explain the situation in other words. The question I am stuck is using a table XYZ with columns X, Y, and Z. Column X can have duplicates and column Z also. what I need to find is the X's that always have the same value in Z. So
X Y Z
1 ? a
1 ? a
2 ? b
2 ? c
3 ? c
3 ? a
would return me 1 because when X is 1 Z is always a.
My real problem is that I feel I am missing some SQL knowledge in order to achiev this. I would appreciate it if anyone can give me a hint, not a solution but maybe a link to the the SQL knowledge im missing or and brief explanation of the SQL statement that could make me do this.
Otherwise have a nice day.
David.
edit: SELECT X FROM XYZ GROUP BY X HAVING COUNT(DISTINCT Z) = 1 worked and I understand it well. Now what I cannot understand is how to add the Z column to the resultset.
select x, min(z)
from tab
group by x
having min(z) = max(z)
-- or
having count(distinct z) = 1
There are several SQL simple functions that could be used to accomplish this.
DISTINCT MSDN http://technet.microsoft.com/en-us/library/ms187831(v=SQL.105).aspx
GROUP BY http://technet.microsoft.com/en-us/library/ms177673.aspx