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

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

Related

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;

HANA concat rows

I use SAP-HANA database. I have a simple 2 column table whose columns are number, name, noodles, fish . The rows are these:
number name noodles fish
1 tom x
1 tom x
1 jack
2 jack x
I would like to group the rows by the id, and concatenate the names into a field, and thus obtain this:
number name noodles fish
1 tom x x
2 jack x
Can you please tell me how we can perform this operation in sap-hana? Thanks in advance.
Well, you did not really concatenate the names, but instead kept the same ones (if you would have concatenated the names as well, you would get something like jackjack in your result). I guess your x's indicate some sort of ABAP-style flags.
In any case, you would do this with grouping. This is a completely non-HANA thing (you can use the same basic SQL for any DB). You can group against several columns. All other columns that you want to select must be used in an aggregated expression (e.g. a SUM, MAX, COUNT, etc.).
To get the output from your question, I wrote the following code:
SELECT "ID", "NAME", MAX("FISH"), MAX("NOODLES")
FROM #TEST GROUP BY "ID", "NAME";
And got the same output as you. I used the MAX function based on the following assumption: you would want to get X if there is any X in the "concatenated" (aggregated) rows in that column. You get nothing / space if all the "concatenated" rows have space in them.

Average Distinct Values in a single column in Power Pivot

I have a column in PowerPivot that basically goes:
1
1
2
3
4
3
5
4
If I =AVERAGE([Column]), it's going to average all 8 values in the sample column. I just need the average of the distinct values (i.e., in the example above I want the average of (1,2,3,4,5).
Any thoughts on how to go about doing this? I tried a combination of =(DISTINCT(AVERAGE)) but it gives a formula error.
Thanks!!
Kevin
There must be a cleaner way of doing this but here is one method which uses a measure to get the sum of the values divided by the number of times it appears (to basically give the original value) then uses an iterative function to do it for each unique value.
Apologies for the uninspired measure names:
[m1] = SUM(table1[theValue]) / COUNTROWS(Table1)
[m2] = AVERAGEX(VALUES(Tables1[theValue]), [m1])
Assuming your table is caled table1 and the column is called theValue

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.

I don't understand the need for self-joins. Can someone please explain them to me?

SELECT region, name, population
FROM bbc x
WHERE population <= ALL (SELECT population FROM bbc y WHERE y.region=x.region AND population>0)
I dont understand the logic of x and y using for the same table.
x and y are taking to be two different instances of table bbc, To list a table two times in the same query, you must provide a table alias for at least one of instance of the table name. This table alias helps the query processor determine whether columns should present data from the right or left version of the table.
This query returns all regions with smalest population in each rgion. To make this query without self-join you'll need to do 2 queries for each region:
1.
set #min=Select min(population) from bbc where population>0 and region=#region
2.
select region, name, population from bbc where population=#min and region=#region