I would like to perform a select similar to the example below. Basically I have three columns name, x, value. I would like to return the name whose values are all positive across a given range of x.
for example, I want to return the names whose values are all positive for x between 1 and 3, this should return y3 and y4. could anyone please help me, thanks.
select name
from your_table
where x between 1 and 3
group by name
having min(value) >= 0
Related
I have a table in SSRS that has both row and column groups.
For each row group [Cat], I need to highlight the highest value in the column group, which is the sum of all counts for that category in a given month.
Can't for the life of me figure it out, so if anyone could help that would be great!
Thanks
Example of dataset
This is what I'm aiming for
Table in Design View
Current outcome
The problem you will face is that you will have to try to use nested aggregates with scopes defined. This might be possible (but I don't think it is...)
There is a fairly simple way to fix it though. I can;t give an exact answer as I don;t know what your dataset looks like but typically you would have to make some changes to your dataset, then its simple.
So assuming your dataset looks something like this
Cat myDate counts
A 20171001 90
A 20171001 6
B 20171001 18
C 20171001 1
A 20171101 100
A 20171101 20
....
....
Then aggregate everything so the report does not have to do any real aggregation with something like
SELECT
*
, max(counts) OVER(PARTITION BY Cat) as maxInCat
FROM (
SELECT
Cat, myDate
, SUM(counts) as counts
FROM myTable
GROUP BY Cat, myDate
) x
This will give you a dataset with an additional column maxInCat. This column will contain the maximum value in each category so we can compare against this in the report.
The expression can then be something like
=IIF(SUM(Fields!counts.Value)>0 and SUM(Fields!counts.Value) = Fields!maxInCat.Value, "Yellow", Nothing)
EDIT
I've updated the actual backcolor expression as it didn't account for blanks/zeros
Ignoring the fact the the columns are not sorted as I don't have time, here's the result
Here's an answer that I think does what you need:
declare #Table as table
(
[Cat] char(1),
[Sector] tinyint,
[Counts] int,
[Date] date
);
insert into #Table
(
[Cat],
[Sector],
[Counts],
[Date]
)
values
('A', 1, 4103, '2017-10-01'),
('A', 1, 3001, '2017-11-01'),
('A', 1, 1128, '2017-12-01'),
('A', 1, 5917, '2018-01-01'),
('A', 1, 9594, '2018-02-01'),
...
So you know where the data is coming from.
with [AggregatedData] as
(
select
t.Cat,
t.Sector,
t.Counts,
t.[Date],
sum(t.Counts) over (partition by t.Cat, t.[Date]) as [SumCounts]
from #Table as [t]
)
select
ad.Cat,
ad.Sector,
ad.Counts,
ad.[Date],
ad.SumCounts,
max(ad.SumCounts) over (partition by ad.[Date]) as [MaxSumCounts]
from [AggregatedData] as [ad]
Then in SSRS, you can use:
=iif(IsNothing(Fields!SumCounts.Value) = FALSE AndAlso Fields!SumCounts.Value = Fields!MaxSumCounts.Value, "Yellow", "Transparent")
Which gives:
new to SQL.
I have the following set of data
A X Y Z
1 Wind 1 1
2 Wind 2 1
3 Hail 1 1
4 Flood 1 1
4 Rain 1 1
4 Fire 1 1
I would like to select all distinct 'A' fields where for all rows that contain A have flood and rain.
So in this example, the query would return only the number 4 since for the set of all rows that contain A = 4 we have Flood and Rain.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
Please let me know if you need further clarification.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
You can use aggregation, and filter with a having clause:
select a
from mytable t
where x in ('Flood', 'Rain') -- either one or the other
having count(*) = 2 -- both match
If tuples (a, x) tuples are not unique, then you want having count(distinct x) = 2 instead.
You Shooud use count(distinct X) group by A and having
count(distinct...) avoid situation where you have two time the same value for X
select A
from my_table
WHERE x in ('Flood', 'Rain')
group A
having count(distinct X) = 2
i need to run query which do the following:
order the table by x , so the result will be.
then it do a multiple order by for a groups according to the following x range.
so userId_1 will be before userId_2 , because both are in 90-100 x range.
and userId_1 y value > userId_2 y value.
How to implement that ? thanks
You should be able to get the desired effect by dropping the last digit of x for ordering, i.e.
SELECT *
FROM MyTable
ORDER BY x DIV 10, y
I am stuck with the following issue. I have 1 table that looks like this:
field_number.. Value
````````````````````````````````
1 ......................... 1
2 ..........................1
3 ......................... 2
4 ..........................2
etc.
I want to group different fieldnumbers and have an average for the value column. So the output should be:
field_number................Value
name(1,2)...................... 1.............. ((1+1)/2)
name(3,4)...................... 2.............. ((2+2)/2)
I have checked previous questions but cannot find any question that covers this issue (I might search on the wrong keywords though). So if this has already been covered my appologies, but any help or a point to a previous answer would be appreciated.
** =============UPDATE============= **
I went through your suggestions but did not get it right. So I am trying to be more specific. I almost have the result I want apart from the fact I want to have a fixed value in one of my columns. I have the following query:
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (15, 17, 24) A
UNION
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (16, 108, 18)
etc.
This gives me a table with two columns
wp_rg_lead_detail.value................field_number
4.3 (average)..............................15 (first value of av calculation)
What I want is to change the field number (15 in this case) in a fixed value (text). What and how should I add this to the query?
SELECT avg(value) FROM table WHERE field_number in (1,2)
SELECT avg(value) FROM table WHERE field_number in (3,4)
If your table is really this simple, you can also get away with:
select distinct
Value,
count(Value) as '#'
from table_name
group by Value
If you acctually want to group by a range, than you can put the logic of the range in your grouping clause (see this fiddle)
select distinct
avg(Value) as average,
floor(Value),
count(Value) as '#'
from table_name
group by floor(Value)
In the fiddle I used grouping on whole integers, but you can make that as complex as you like (see, for instance, this example)
If you are actually also interested in your corresponding fields, use group_concat() like so
select
Value,
group_concat(
distinct field_number
order by Value
) as fields
from table_name tn1
group by Value
order by Value
output:
Value | fields
---------------------------------
1 | 1,2
2 | 3,4
See this fiddle implemented from this blog post
For a generalized answer.
SELECT CONCAT('name','(',GROUP_CONCAT(field_number),')') AS field_number,
AVG(Value) as Value
FROM table_name
group by table_name.`Value`
Hope this helps.
Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)