I am trying to flag the occurrences of a data item in column [Customer] determined by column [Division] and set three values for it in column [flag]. If data item [Customer] has both ‘Q’ and ‘Z’ data item values for column [Division] in separate rows I want the value of the data item [flag] set to 2 for every repeated occurrence of data item [Customer] where customer is a member of both ‘Q’ AND ‘Z’ Division. If this condition exists I want the value of the [flag] column equal to 2 for all rows where the [Customer] column value occurs. If data item value [Division] for [Customer] is only ‘Q’ then set [flag] to 1 and if data item value [Division] for [Customer] is only ‘Z’ then set [flag] to 0. There are thousands of possible values for the [Customer] column data item and multiple rows can contain the desired [customer] / [flag] combinations. How would I create column [flag] in the example below?
I want Customer separated by unique values. Notice Customer X is repeated 3 times and Customer A is repeated twice and the value in the [flag] column for each is 2 - ALL [flag] values where a customer is repeated In a row is 2.
DESIRED RESULTS:
Customer Rep division Count
A : 1 : Q : 1
B : 2 : Z : 0
X : 3 : Q : 2
X : 4 : Z : 2
X : 4 : Q : 2
D : 5 : Z : 0
S : 3 : Q : 2
H : 4 : Q : 1
S : 4 : Z : 2
Try this based on your expanded explanation:
CASE count(distinct [division] for [Customer])
WHEN 2 THEN 2
WHEN 1 AND [division] = 'Q' THEN 1
WHEN 1 AND [division] = 'Z' THEN 0
END
We count the distinct values of 'division' for every value of 'Customer'. If the count is 2 we know that both 'Q' and 'Z' are represented and we output the integer 2. If the count is 1 and the value of 'division' is 'Q' then we output 1. Lastly, if the count is 1 and 'division' is 'Z' then we output 0.
Assuming 'Q' and 'Z' are the only possible values for 'division' you can safely change the last test to: ELSE 0 to simplify. I included the full logic for clarity.
Related
I have a table with 3 columns :
ID
productId
customerId
1
5
1
2
4
1
3
5
1
4
4
1
I want to add a new column called ID_MOD and its value by default will be ID%X (X is a number).
Expected result for X=3 :
ID
productId
customerId
ID_MOD
1
5
1
1
2
4
1
2
3
5
1
0
4
4
1
1
I have X instances of my app and I want each instance to query specific ID_MOD values(0/1/2.../X-1).
Is it possible to use default values for columns? If it is, can the default value be calculated based on other columns ?
what you can do is create a stored function that receives x as an input parameter, and extends your table with a calculated column (at query time).
For example:
.create-or-alter function FunctionName(x:int)
{
TableName
| extend ID_MOD = ID % x
}
If you decide x always has the same value and shouldn't be parameter, you can name the function using the same name as the table, and it will 'hide' the original table.
If the logic of calculating the extended column is well-defined in advance, you can invoke it at ingestion time, using an update policy
Is it possible to order a SQLite select result in way that highest value is in the center and lower value are distributed around center till start and end of result set?
data set:
5
3
2
1
1
1
1
desired result:
1
1
3
5
2
1
1
Use ROW_NUMBER() window function to assign a row number to each value and with MAX() window function get the difference of each value from the max value of the table so they can be distributed around the max value:
SELECT *
FROM tablename
ORDER BY CASE WHEN ROW_NUMBER() OVER (ORDER BY col) % 2 = 1 THEN -1 ELSE 1 END *
(MAX(col) OVER () - col)
Change col to the column's name.
See the demo.
My question is about using case statement in where clause to check for date and assign values to columns. My sample code include.
select * from table
where
column 1 > 10 and
case when column 2 = 1
then
column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01'
else
column 4 between (1st day of this month) and (yesterday)
end ;
when I am running this code. I am getting 3706 syntax error:expected something in between field and '='.
How to fix this ?
A CASE statement will always return a value or NULL (if none of the conditions matches), so you can use it in your WHERE clause. There are a couple ways to format your CASE statement:
Format 1
CASE
WHEN <condition> THEN <some_expression>
WHEN <another_condition> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE
WHEN col1 = 10 THEN 'Y'
WHEN col1 = 20 THEN 'N'
ELSE 'N/A'
END
Format 2
CASE <expression>
WHEN <value> THEN <expression>
WHEN <another_value> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE col1
WHEN 10 THEN 'Y'
WHEN 20 THEN 'N'
ELSE 'NA'
END
I'm not sure what you're trying to do with your sample code, but it looks more like pseudo-code and will not work as-is. Your CASE statement is not formatted properly and your column references like column 1 will not work that way. If your column is actually named column 1, then you need to put double-quotes around it:
select * from table where "column 1" > 10
Can you please describe a little more clearly what exactly you are trying to do?
A CASE expression can't be used to create some kind of dynamic conditions. Write it as a bunch of AND/OR conditons:
select * from table
where
column 1 > 10 and
(
( column 2 = 1 and
(column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01')
)
or
column 4 between (1st day of this month) and (yesterday)
);
Double check the logic, the precedence of logical operators is
parenthesis
NOT
AND
OR
I have a table like:
ID MONTH VALUE
1 06/2014 3
1 07/2014 -2
1 08/2014 1
2 03/2014 1
2 04/2014 -1
(...)
What I want is to create a new column which hierarchically sum the values, like:
ID MONTH VALUE BALANCE
1 06/2014 3 3 <-- 3 + "0" (no previous)
1 07/2014 -2 1 <-- -2 + 3 (previous balance plus current value)
1 08/2014 1 2 <-- 1 + 1 (previous balance plus current value)
2 03/2014 1 1 <-- (...)
2 04/2014 -1
(...)
Possibly a way to use a connect by clause here, just couldn't get my head around it.
I am using Oracle 11gR2
Ideas?
The analytical function sum (...) over (...) is the perfect candidate:
create table tq84_t (
id number,
month date,
value number
);
insert into tq84_t values (1, date '2014-06-01', 3);
insert into tq84_t values (1, date '2014-07-01', -2);
insert into tq84_t values (1, date '2014-08-01', -1);
insert into tq84_t values (2, date '2014-03-01', 1);
insert into tq84_t values (2, date '2014-04-01', -1);
select
id,
month,
value,
sum(value) over (partition by id order by month) balance
from
tq84_t;
I have a query in Teradata. I want to add an additional column that would be a VARCHAR.
It should say whether the selected record is even or odd
select id, name, CASE newColumn WHEN --- ???
from my table
Like this
id name newColumn
1 asdf odd
2 ts df even
32 htssdf odd
4 asdfsd even
23 gftht odd
How can I do this
Based on your example, I can't tell how you are sorting the results. You would need to define a sort order. Let's assume you would do it based on the id number.
SELECT id, name,
ROW_NUMBER() OVER(ORDER BY id) row_id,
CASE WHEN ROW_NUMBER() OVER(ORDER BY id) MOD 2 = 0 THEN 'Even' ELSE 'Odd' END newColumn
FROM my table
The row_id is incrementally assigned based on the id field being sorted ascending. You then use the MOD function to determine if there's a remainder after dividing the number by a value (in this case 2). Result would look like the following:
id name row_id newColumn
1 asdf 1 Odd
2 ts df 2 Even
4 asdfsd 3 Odd
23 gftht 4 Even
32 htssdf 5 Odd