I am trying to figure how I could do this where I have a table as follows:
ID FKeyID Complete
1 6 1
2 6 0
3 6 0
4 7 0
5 8 0
6 8 0
I want to create a function to return 1 or true if any FKeyID for example 6 has a value of 1 in complete column and 0 if it does not.
This is a function that takes fKey value and should return 1 or 0 based on that.
So in above basically if my FKeyID is 6 return 1 because complete column is 1 in one of the rows, and 0 for FKeyID 8 because none of values in column complete is 1.
CREATE function [dbo].f_x
(
#FKeyID int
)
RETURNS bit
as
begin
return case when exists
(select 1 from test where Complete = 1 and FKeyID = #FKeyID)
then 1 else 0 end
end
Related
I have a postgres table that looks like this:
A B
5 4
10 10
13 15
100 250
20 Null
Using SQL, I would like to check whether the value in column A is larger than the value in column B and if so, then add a 1 to the column True. If the value in column A is smaller or equal to the value in column B or if column B contains a [NULL] value, I would like to add a 1 to the column False, like so:
A B True False
5 4 1 0
10 10 0 1
13 15 0 1
100 25 1 0
20 [NULL] 0 1
What is the best way to achieve this?
You can use case logic:
select t.*,
(case when A > B then 1 else 0 end) as true_col,
(case when A > B then 0 else 1 end) as false_col
from t;
Suppose I have the following records:
code sequence_no group_no
4 1 1
2 2 1
3 3 1
4 4 1
1 1 2
3 1 3
4 2 3
the output should be: within the same group(by group_no) the code column is updated with the first(by sequence_no) code that is not 4.
so the output should look like this:
code sequence_no group_no
2 1 1
2 2 1
2 3 1
2 4 1
1 1 2
3 1 3
3 2 3
Here's my code, and my logic is that if the input.code == 4, then assign the next code to the temp variable. This doesn't work if the first record code == 4 for some reason. And I don't think this logic covers the last record of the group if it == 4.
type temporary_type=
record
decimal("\x01") l_code;
end;
temp :: scan(temp, in) =
begin
temp.l_code::
if (temp.l_code == 4) in.code
else temp.l_code;
end;
temp :: initialize(in) =
begin
temp.code :: in.code;
end;
out :: finalize(temp, in) =
begin
out.* :: in.*;
out.code :: temp.l_code;
end;
I'd like to update approximately the first X number of rows in a table but I want to always update all rows with a matching column at the same time. So if my table has:
MyID Transaction Amount Date Status
1 1 2 02/08/2016 0
1 1 4 02/08/2016 0
2 4 1 02/08/2016 0
2 3 2 02/08/2016 0
3 10 1 02/08/2016 0
3 6 4 02/08/2016 0
I want to update Status to 1 on approximately the first 5 rows, but I don't want to split up matching MyID values, how can I do that? I could update the first 4 or 6 in this example.
Here is one method:
update t
set status = 1
where myId in (select top 5 MyId from t order by MyId);
I have 1 Virtual cube consists of 2 cubes.
Example of fact table of 1st cube.
id object_id time_id date_id state
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 0
5 11 4 1 0
6 11 7 1 1
7 10 8 1 0
8 11 5 1 0
9 10 7 1 1
10 10 9 1 2
Where State: 0 - Ok, 1 - Down, 2 - Unknown
For this cube I have one measure StateCount it should count States for each object_id.
Here for example we have such result:
for 10 : 3 times Ok , 2 times Down, 1 time Unknown
for 11 : 3 times Ok , 1 time Down
Second cube looks like this:
id object_id time_id date_id status
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 1
5 11 4 1 1
Where Status: 0 - out, 1 - in. I keep this in StatusDim.
In this table I keep records that should not be count. If object have status 1 that means that I have exclude it from count.
If we intersect these tables and use StateCount we will receive this result:
for 10 : 2 times Ok , 1 times Down, 1 time Unknown
for 11 : 2 times Ok , 1 time Down
As far as i know, i must use calculated member with IIF condition. Currently I'm trying something like this.
WITH MEMBER [Measures].[StateTimeCountDown] AS(
iif(
[StatusDimDown.DowntimeHierarchy].[DowntimeStatus].CurrentMember.MemberValue
<> "in"
, [Measures].[StateTimeCount]
, null )
)
The multidimensional way to do this would be to make attributes from your state and status columns (hopefully with user understandable members, i. e. using "Ok" and not "0"). Then, you can just use a normal count measure on the fact tables, and slice by these attributes. No need for complex calculation definitions.
This might be hard to describe in the title, here's a sample data:
id pub_type general_suppl book_suppl catalogue_suppl magazine_suppl
1 book 10 10 0 0
2 book 11 11 0 0
3 catalogue 10 0 10 0
4 magazine 9 0 0 9
5 other 10 0 0 0
6 magazine 8 0 0 10
Each of the item is of a specific publication type with a general supplier and a supplier for the type of publication. other items only have a general_suppl. If I want to get all items on supplier value 10, the following conditions will have to be met:
if pub_type == 'book'
match on book_suppl == 10
elif pub_type == 'catalogue'
match on catalogue_suppl == 10
elif pub_type == 'magazine'
match on magazine_suppl == 10
else
match on general_suppl == 10
As you can see above, if pub_type falls in book,catalogue,magazine, I ignore the column general_suppl.
The expected output on supplier value 10 will be:
id pub_type general_suppl book_suppl catalogue_suppl magazine_suppl
1 book 10 10 0 0
3 catalogue 10 0 10 0
5 other 10 0 0 0
6 magazine 8 0 0 10
I can achieve the above by retrieving all the rows and perform filtering at the code level. Is there a single SQL way to get the above results? The database design and data are beyond my control, so I can't re-design the DB and will have to work with the above table structure.
It's ugly, but you can throw that logic into a CASE structure.
SELECT *
FROM table
WHERE 10 = CASE WHEN pub_type = 'book' THEN book_suppl
WHEN pub_type = 'catalogue' THEN catalogue_suppl
WHEN pub_type = 'magazine' THEN magazine_suppl
ELSE general_suppl END
and to the rescue!
select *
from table
where (pub_type='book' and book_suppl=10)
or (pub_type='catalogue' and catalogue_suppl=10)
or (pub_type='magazine' and magazine_suppl=10)
or (pub_type not in ('book','catalogue','magazine') and general_suppl=10)