SQL Server: check for value in a range between 2 columns - sql-server-2008

I am trying to find values between a range from 2 different col.
Below is the structure of my table :
OBJECT FIELD LOW HIGH
----------------------------
S_IDOCMONI ACTVT 01 03
I wanted to search for values 01, 02 and 03 between cols LOW and HIGH. Currently i have written a CASE statement only to check in col LOW :
select *
into [abc]
from
(select
*,
case
when [OBJECT] = 'S_IDOCCTRL' and [FIELD] = 'ACTVT' and [LOW] in ('01')
then '1'
end [Subgroup]
from
[user-master]) as x
where
subgroup is not null
Could anyone help me where I can search for value 02 (as a range between cols. LOW and HIGH)

Related

count nulls values in a entry (columns)

I have the following table:
ID
SPNR
SP1
SP2
SP2
01
2
x
x
02
2
x
03
3
x
x
04
2
x
05
3
x
x
x
SPNR is the amount of SP needed.
The first record is ok. In the second you need 2 but there is only one. In the third, 3 are needed and there are only two.
I need a query that lists the records that do not meet the condition (ID = 2 ,3, 4)
I am relatively new to MYSQL and cannot find the "count if" option for the same record. Can someone shed some light on me.
You could use a boolean sum expression here:
SELECT *
FROM yourTable
WHERE (SP1 IS NULL) + (SP2 IS NULL) + (SP3 IS NULL) != SPNR;

Pick relevant value in a set of columns

It might be an easy question for DBA, but not for me.
I have a simplified Family table that looks like that: (part1 and part2 are ids)
famid
indiv1
indiv2
1
42
27
2
33
22
3
42
12
4
22
42
5
42
27
I can easily retrieve all families that match part1=X or part2=X
But the requested output format is to produce only the famid AND (part1 value or part2 value) BUT without the column that matches the value X.
For example, for a value of 42, the query should return:
[[1,27], [3,12], [4,22], [5,27]]
I would like to know if there is a 'simple' way to produce such a result with SQL query (or sequelize) only, but without a stored procedure.
Thx
Assuming you only have two columns to check and want to return the one that doesn't = X then you want to use a CASE statement to check if Indiv1 = 42. Then if it is return Indiv2 otherwise return Indiv1.
SELECT FamID
,CASE
WHEN Indiv1 = 42
THEN Indiv2
ELSE Indiv1
END AS Indiv
FROM Family
WHERE Indiv1 = 42
OR Indiv2 = 42
See my SQL Fiddle Demo

How to move count to the next row if it is holiday in SQL Server?

Input
Table 1:
CountryCode is_holiday ? TicketsRaiseDay NofTickets TotalTickets
91 No Mon 10 10
91 No Tue 20 20
91 Yes Sat 10 NA (since it is holiday )
91 No Mon 5 15 (Sat tickets +Monday Tickets )
Here totaltickets column is calculated or computed column or report column .
Note : is_holiday column is the responsible to hold holiday detail so need not to bother about this field .
It would be great if i can get either ssrs expression or sql code .
This should do the trick:
You can see a working example here.
SELECT
x.countrycode,
x.is_holiday,
case when x.previousHoliday = 1 THEN previousNumberOfTickets + x.nofTickets
when x.is_holiday = 1 then 0
else x.nofTickets
end
from (select
is_holiday,
nofTickets,
countrycode,
lag(is_Holiday,1) over (order by countrycode) as previousHoliday,
lag(nofTickets,1) over (order by countrycode) as previousNumberOfTickets
from tickets) x
Note: this doesn't handle consecutive holidays. Also, you would probably want to order by something other than countrycode. I wasn't sure what you're ordering by.
Edit: I just noticed you're using sql server 2008, and the lag function is not available until 2012.

SSRS - Returning the average where date is 2015

I have been trying to figure this out for a while now and just couldn't find the answer anywhere.
I have a report in SSRS with a column group assigned to "Year", this column expands depending on what parameter the user enters into StartYear. If the user enters "2013" the report will extract all data from 2013 to 2015, this means that there are then 3 columns with the same name ("Cost").
My report looks something like this when entering StartYear as "2013" the value beneath "Year" displays the "Cost" column :
Area | 2013 ("Year") | 2014 ("Year") | 2015 ("Year")
A | 20 | 50 | 25
B | 15 | 65 | 35
C | 40 | 70 | 20
Before the report get built, the reports looks something like this:
Area | [Year]
[Area] | [Cost]
I want to add a column to this report which displays the Average but only for the Year 2015.
This is what I have tried sofar but it brings back the Average for one row and all the year : 20, 50 and 25 instead of 25, 35 and 20:
=Sum(IIF(Fields!Year.Value = 2015, Avg(Fields!Cost.Value), 0))
Any help would be greatly appreciated.
You need an expression like:
=Avg(IIF(Fields!Year.Value = 2015, Fields!Cost.Value, Nothing))
If you are using an IIf expression to get a subset from a DataSet, you must specify Nothing as the False part of the expression, otherwise you just get a bunch of zeroes included in the aggregate, skewing the results.
This assumes the aggregate is running outside any particular Group Scope - you can always add a Scope to the expression to make sure you are checking the entire DataSet:
=Avg(IIF(Fields!Year.Value = 2015, Fields!Cost.Value, Nothing), "DataSet1")
Edit after comment
To expand on the Scope comment above, you can specify the aggregate to run at the Row Group level by adding the Row Group name as a parameter to the expression:
=Avg(IIF(Fields!Year.Value = 2015, Fields!Cost.Value, Nothing), "Area")
Assuming Area is the name of the Row Group.

SSRS Stacked Column Chart - Multiple column overlap

In SQL Server Reporting Services 2008 R2, I have the following dataset.
Date | Value 1 | Value 2
--------------------------
Week 1 | 52 | 57
Week 2 | 49 | 63
Week 3 | 88 | 71
I have a Stacked Column Chart with the X axis of Date, and the Y axis of Value. The columns in the chart currenly shows Value 2 on top of Value 1. This shows the column as the total of Value 1 and Value 2. So for Week 1, it is 109 etc.
Now my question is, how would I get the chart to show the total of each column to be the highest Value in the dataset? This would still show both values but would have the entire column of the lowest value with the remaining value on top. So for week 1, the total would be 57. The column for Value 1 would be 52 and the column for Value 2 would be 5.
This may be confusing so I have added a dummy image of what I intend the final graph to look like.
Maybe you can write a query like this:
SELECT LEAST(value1, value2) as value1, GREATEST(value1, value2) as value2
value1 < value2 as color
FROM ...