SQL Query returning records that have always the same value - sql

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

Related

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, incrementing column value returned from SELECT query

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;

Getting fields that take less than certain distinct values

IF I have data with two columns feature and feature_value, just like the example data set below
feature feature_value
X 1
X 1
X 2
Y 7
Y 8
Y 9
Z 100
and I want to get only feature,feature_value columns for features that have less than 3 distinct values (in this case only columns having X and Z), what is the efficient way to do it? Using Count(Distinct) and applying the where condition or is there any faster way?
Please note that this answer uses generic SQL, and since your question isn't clear as to why you'd only get the "X" and "Y" records when you want those that appear 3 or less, I've also taken the liberty of understanding your answer to mean you're looking only for the features that appear less than three times in the feature column, per your question saying you're looking for "columns [sic] for features that have less than 3 distinct values." If you meant 3 or more, you can easily adjust that in the below subquery by changing < to >=.
Subquery to GROUP BY feature and get the count, then select only those records.
SELECT * FROM my WHERE feature IN
(SELECT feature
FROM my
GROUP BY feature
HAVING COUNT(*) < 3)
;
http://sqlfiddle.com/#!2/29c05/1

SQL Query to Add Values from Column X for Every Entry That Has Y

I need to write a query that is going to calculate the sum of one column depending on the values of another. Basically I need to get the sum of a certain drug administered for each patient in one of my DB's tables. My table has an account number column (x), drug ID column (y) and an amount administered column (z). The thing is there can be multiple rows for each account number so what I need to do is pull the total amount of that drug administered for each patient account number. So in essence I need a query that will return the sum of z for for every x with a where clause at the end using column y. I hope I am explaining this clearly because thinking about it confuses me! Any help would be appreciated. Thanks guys!
This is a simple GROUP BY query, I'm not sure what's confusing you.
SELECT x, SUM(z) total_z
FROM table
WHERE y = 123
GROUP BY x
Use GROUP BY:
SELECT x, y, sum(z)
FROM t
GROUP by x, y

Help with select query

I have a table with 2 columns:]
ID Name
1 A
1 B
1 C
1 D
2 E
2 F
And I want write a query to have output like :
1 A B C D
2 E F
It's possible ?
You want a Pivot, which is easy in excel, but requires (I believe) quite a bit of work in SQL Server, as it is hard to determine how many columns you need. You could dynamically construct the sql based on a max() aggregate, I suppose.
Start looking here
a good article you can look at :
http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/