This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle: how to “group by” over a range?
Let's say I have data that looks like this:
Item Count
======== ========
1 123
2 1
3 47
4 117
5 18
6 466
7 202
I want to create a query that gives me this:
Count Start Count End Occurrences
=========== =========== ===========
0 100 3
101 200 2
201 300 1
301 400 0
401 500 1
Basically, I want to take a bunch of counts and group them into ranges for statistical rollups. I don't think I'm using the right keywords to find the answer to this. I am going against Oracle, though if there is an ANSI SQL answer I'd love to have it.
select
a.mini,
a.maxi,
count(a.item)
from
(
select
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 0
when counter>100 and counter<200 then 101
when ....
end as mini
table.item,
case (table.counter)
when counter>=0 and counter<=100 then 100
when counter>100 and counter<200 then 201
when ....
end as maxi
from
table
) a
group by
a.mini,
a.maxi
One way is using CASE statements. If you want it to be scalable, try having the range in a separate table and use JOIN to count the occurence.
Related
So my data looks something like this:
Smpl_id Plate_id CT_Value
1 1 27
1 1 32
2 1 56
2 1 49
1 2 40
1 2 36
2 2 58
2 2 64
I would like to design a query that returns averages of CT_Value of each sample in a plate, so it would look like this:
Smpl_id Plate_id Avg_CT
1 1 29.5
2 1 52.5
1 2 38
2 2 61
I have tried
Avg_CT: DAvg("[CT_Value]","[qPCR_sample_data]","[Plate_id] = '" & [Plate_id] & "'" And "[Smpl_is] = '" & [Smpl_id] & "'")
But that just results in:
Smpl_id Plate_id CT_Value
1 1 45.25
1 1 45.25
2 1 45.25
2 1 45.25
1 2 45.25
1 2 45.25
2 2 45.25
2 2 45.25
I can't just list the plate or sample id numbers because this is actually a long list that is continually growing. I also need to use these average numbers in future calculations (that I have already figured out how to do).
Also, I have just started using MS Access (designing a brand new database), so I kinda understand SQL but have very little actual experience in it.
Thank you!
You will need to use a group by query.
Create a new query, and add the table (in my example I've called it tblSample).
Add the three fields, Smpl_id, Plate_id and CT_Value to the query grid.
On the Query Design memubar, click on the button labelled "Totals".
This introduces a new row in the query grid called "Total", with all three fields having it set to "Group By".
Simply change the "Group By" for CT_Value to "Avg", and run the query to get the results you are after:
Regards,
You should be able to use simple aggregation such as:
select t.smpl_id, t.plate_id, avg(t.ct_value) as avg_ct
from qpcr_sample_data t
group by t.smpl_id, t.plate_id
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need help with this because I Can't think of a way to do it.
I have this Table:
Score Table:
UserID: 1 1 2 1 2 3
Score: 100 -10 100 -10 -20 100
So what I need to do is create a view using a SUM function to get the current score of each user, something like this...
View:
UserID: 1 2 3
Score: 80 80 100
Any ideas how can I achieve that?
Thx for the help, but I'm having another issue.
Now I have another Column in my table (rID) for instance.
User ID : 1 2 1 1 2 2
rID: NULL Null 1 2 1 4
Score: 100 100 -20 -30 -20 -40
I want to group by UserID and rID taking diffrents scores for each one of the rID, I want this result:
UserID: 1 1 2 2
rID: 1 2 1 4
Score: 80 70 80 60
Any help would be appreciated!
Use GROUP BY:
SELECT UserID, SUM(Score)
FROM Table
GROUP BY UserID
This question already has an answer here:
Sqlite query - order results with numbers last
(1 answer)
Closed 7 years ago.
I have table data like this:
ID name
----------
1 30
2 aaa
3 zzz
4 20
5 40
6 10
My result should be sorted data
ID name
5 40
1 30
4 20
6 10
3 zzz
2 aaa
Means numeric and alphabetic should be separate and it should be sorted in DESC or ASC.
If anyone have an idea to perform this operation in sqlite3 help me
SELECT * FROM yourTableName ORDER BY ID ASC;
If you want something more detailed than that, it would help to know what you are currently running or what you want the output to be.
I have the following 3 tables:
1) Sweetness Table
FruitIndex CountryIndex Sweetness
1 1 10
1 2 20
1 3 400
2 1 50
2 2 123
2 3 1
3 1 49
3 2 40
3 3 2
2) Fruit Name Table
FruitIndex FruitName
1 Apple
2 Orange
3 Peaches
3) Country Name Table
CountryIndex CountryName
1 UnitedStates
2 Canada
3 Mexico
I'm trying to perform a CrossTab SQL query to end up with:
Fruit\Country UnitedStates Canada Mexico
Apple 10 20 400
Orange 50 123 1
Peaches 49 40 2
The challenging part is to label the rows/columns with the relevant names from the Name tables.
I can use MS Access to design 2 queries,
create the joins the fruit/country names table with the Sweetness table
perform crosstab query
However I'm having trouble doing this in a single query. I've attempted nesting the 1st query's SQL into the 2nd, but it doesn't seem to work.
Unfortunately, my solution needs to be be wholly SQL, as it is an embedded SQL query (cannot rely on query designer in MS Access, etc.).
Any help greatly appreciated.
Prembo.
How about:
TRANSFORM First(Sweetness.Sweetness) AS FirstOfSweetness
SELECT Fruit.FruitName
FROM (Sweetness
INNER JOIN Fruit
ON Sweetness.FruitIndex = Fruit.FruitIndex)
INNER JOIN Country
ON Sweetness.CountryIndex = Country.CountryIndex
GROUP BY Fruit.FruitName
PIVOT Country.CountryName;
I hate to rely on an outside post and present it as my answer, but this is a pretty steep topic and I can't do it justice. So I suggest you look at this article.
I have a table (table variable in-fact) that holds several thousand (50k approx) rows of the form:
group (int) isok (bit) x y
20 0 1 1
20 1 2 1
20 1 3 1
20 0 1 2
20 0 2 1
21 1 1 1
21 0 2 1
21 1 3 1
21 0 1 2
21 1 2 2
And to pull this back to the client is a fairly hefty task (especially since isok is a bit). What I would like to do is transform this into the form:
group mask
20 01100
21 10101
And maybe go even a step further by encoding this into a long etc.
NOTE: The way in which the data is stored currently cannot be changed.
Is something like this possible in SQL Server 2005, and if possible even 2000 (quite important)?
EDIT: I forgot to make it clear that the original table is already in an implicit ordering that needs to be maintained, there isnt one column that acts as a linear sequence, but rather the ordering is based on two other columns (integers) as above (x & y)
You can treat the bit as a string ('0', '1') and deploy one of the many string aggregate concatenation methods described here: http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/