How to translate this SQL statement to ADF(Data Flow) Expression - tsql

Is there any way to translate this SQL statement into ADF expression(Window element)?
CASE
WHEN MAX(
IF (cd.index = 1, cd.value, NULL) IN ('product', 'productListing')) THEN
CASE
WHEN MAX(
IF (cd.index = 2,cd.value,NULL)) LIKE '%sunglasses%' THEN
'Sunglasses'
WHEN MAX(
IF (cd.index = 2,cd.value,NULL)) LIKE '%glasses%' THEN
'Glasses'
WHEN MAX(
IF (cd.index = 2,cd.value,NULL)) LIKE '%contact%lens%' THEN
'Contact_Lenses'
ELSE
'No_Mapped'
END
ELSE
'No_Mapped'
END AS PageCategory
I tried different ways with Window element in data flow but nothing with correct result.
Thanks!!!
<------Solved----->
Solution:
case(
max(toString(iif(toInteger(index) == 1, value)) == 'product'), case(
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%sunglasses%') ,'Sunglasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%glasses%') ,'Glasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%contact%lens%') ,'Contact_Lenses',
toString('No_Mapped')
),
max(toString(iif(toInteger(index) == 1, value)) == 'productListing'),case(
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%sunglasses%') ,'Sunglasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%glasses%') ,'Glasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%contact%lens%') ,'Contact_Lenses',
toString('No_Mapped')
),
max(toString(iif(toInteger(index) == 1, 'No_Mapped')))
)

Thank you Petar Stojiljkovic. Posting your discussions as answer to help other community members.
To translate the SQL statement into ADF expression. Below is the possible solution.
case(
max(toString(iif(toInteger(index) == 1, value)) == 'product'), case(
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%sunglasses%') ,'Sunglasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%glasses%') ,'Glasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%contact%lens%') ,'Contact_Lenses',
toString('No_Mapped')
),
max(toString(iif(toInteger(index) == 1, value)) == 'productListing'),case(
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%sunglasses%') ,'Sunglasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%glasses%') ,'Glasses',
like( max(toString(iif(toInteger(index) == 2, lower(value)))), '%contact%lens%') ,'Contact_Lenses',
toString('No_Mapped')
),
max(toString(iif(toInteger(index) == 1, 'No_Mapped')))
)

Related

how do i use getter and setter in flutter?

Im trying to create tic tac toe game, but im getting some logical error when Im trying to use
provider method.
in the home page i have column containing three widget. first for player scores , second for gird layout, third for winner result.
i did some research and i found out using provider calss is the best option to send data from a widget to another.
import 'package:flutter/cupertino.dart';
import 'package:tictactoe/widgets/scores.dart';
import '../widgets/layout.dart';
// import '../widgets/scores.dart';
class GameProvider with ChangeNotifier {
bool oTrue = true;
List displayXO = ['', '', '', '', '', '', '', '', ''];
String resultDetection = 'hey';
int oScores = 0;
int xScores = 0;
int failedScores = 0;
checkWinner() {
// checking 1 row
if ((displayXO[0] == displayXO[1]) &&
(displayXO[0] == displayXO[2]) &&
displayXO[0] != '') {
resultDetection = 'player ' + displayXO[0] + 'wins';
print('$resultDetection');
}
// checking 2 row
if ((displayXO[3] == displayXO[4]) &&
(displayXO[3] == displayXO[5]) &&
(displayXO[3] != '')) {
resultDetection = 'player ' + displayXO[3] + 'wins';
}
// checking 3 row
if ((displayXO[6] == displayXO[7]) &&
(displayXO[6] == displayXO[8]) &&
(displayXO[6] != '')) {
resultDetection = 'player ' + displayXO[6] + 'wins';
}
// checking 1 column
if ((displayXO[0] == displayXO[3]) &&
(displayXO[0] == displayXO[6]) &&
(displayXO[0] != '')) {
resultDetection = 'player ' + displayXO[0] + 'wins';
}
// checking 2 column
if ((displayXO[1] == displayXO[4]) &&
(displayXO[1] == displayXO[7]) &&
(displayXO[1] != '')) {
resultDetection = 'player ' + displayXO[1] + 'wins';
}
// checking 3 column
if ((displayXO[2] == displayXO[5]) &&
(displayXO[2] == displayXO[8]) &&
(displayXO[2] != '')) {
resultDetection = 'player ' + displayXO[2] + 'wins';
}
// checking 1 axis
if ((displayXO[6] == displayXO[4]) &&
(displayXO[6] == displayXO[2]) &&
(displayXO[6] != '')) {
resultDetection = 'player ' + displayXO[6] + 'wins';
}
// checking 2 axis
if ((displayXO[0] == displayXO[4]) &&
(displayXO[0] == displayXO[8]) &&
(displayXO[0] != '')) {
resultDetection = 'player ' + displayXO[0] + 'wins';
}
notifyListeners();
}
set upgrateScores(String winner) {
if (winner == 'O') {
oScores++;
} else if (winner == 'X') {
xScores++;
}
print('$oScores, $xScores');
notifyListeners();
}
get winnerfunction => checkWinner();
// get upgrarate => upgrateScores();
get result => resultDetection;
}
and this is my great layout
Widget build(BuildContext context) {
void _tap() {}
return GridView.builder(
itemCount: 9,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemBuilder: ((context, index) {
return GestureDetector(
onTap: () {
tapped(index);
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(width: 5, color: Colors.red),
color: Colors.yellow,
),
child: Center(
child: Text(
displayXO[index],
style: GoogleFonts.coiny(fontSize: 64, color: Colors.red),
),
),
),
);
}));
}
void tapped(int tap) {
setState(() {
if (oTrue && displayXO[tap] == '') {
displayXO[tap] = 'O';
oTrue = false;
} else if (!oTrue && displayXO[tap] == '') {
displayXO[tap] = 'X';
oTrue = true;
}
print('object');
GameProvider provider = Provider.of<GameProvider>(context, listen: false);
provider.checkWinner();
});
}

How to join the values in-between two values in a list

I have this list [null, 3, 5, null] and I want to join the values in-between nulls and put it into the same list
For example:
[null, 3, 5, null] into [null, 35, null]
I made this one extension that groups all the values between two nulls
extension GroupByNull<T> on List<T> {
List<List<T>> groupByNull() {
final groupedList = <List<T>>[];
var list = this;
for (var i = 0; i < list.length; i++) {
if (list[i] == null) {
if (i > 0) {
groupedList.add(list.sublist(0, i));
}
list = list.sublist(i + 1);
i = 0;
}
}
if (list.isNotEmpty) {
groupedList.add(list);
}
return groupedList;
}
}
Which returns [[3, 5]] for [null, 3, 5, null]... But I want it to be joined together and added to the original list in the same index
How can I solve this...
thank you.
Note that you can't operate on an arbitrary List<T> because there's no general way to combine two elements of some arbitrary type T. What you want could make sense for List<int?> or maybe List<String?>. (Or maybe it could work on an arbitrary List<T> input if you want a List<String?> output.)
Assuming that you want to operate on List<int?>, then basically as you iterate over your input list, keep track of your current accumulated value. If you encounter null, add the current value (if any) to your output List along with the null. Don't forget to add the current accumulated value (if any) when you're done iterating in case there isn't a final null element.
extension GroupByNull on List<int?> {
List<int?> groupByNull() {
var result = <int?>[];
int? currentValue;
for (var element in this) {
if (element != null) {
currentValue = (currentValue ?? 0) * 10 + element;
} else {
if (currentValue != null) {
result.add(currentValue);
currentValue = null;
}
result.add(currentValue);
}
}
if (currentValue != null) {
result.add(currentValue);
}
return result;
}
}
void main() {
print([null, 3, 5, null].groupByNull()); // Prints: [null, 35, null]
print([3, 5, null].groupByNull()); // Prints: [35, null]
print([3, 5, null, null].groupByNull()); // Prints: [35, null, null]
print([null, 3, 5].groupByNull()); // Prints: [null, 35]
print([null, null, 3, 5].groupByNull()); // Prints: [null, null, 35]
print([null, 0, 0, 0, null].groupByNull()); // Prints: [null, 0, null]
print([null, 1, 2, null, 3, 4, null]
.groupByNull()); // Prints: [null, 12, null, 34, null]
print([null].groupByNull()); // Prints: [null]
print([null, null].groupByNull()); // Prints: [null, null]}
}
You can use this solution.
extension GroupByNull<T> on List<T> {
List groupByNull() {
final groupedList = [];
var list = this;
list.removeWhere( (value) => value == null); // remove all null values
groupedList.add(int.parse(list.join(""))); // combine number left in list
groupedList.insert(0,null); // add null
groupedList.add(null);
return groupedList;
}
}

Can I sort List<dynamic> in dart

Can I sort List<dynamic> in dart?
List<dynamic> list= [9,10,'Plus One'];
list.sort();
print(list);
I expect the result like 9,10,'Plus One' Or 'Plus One', 9, 10
You just need to provide a callback to List.sort that orders heterogeneous types the wya you want. For example, assuming that your heterogeneous List contains only ints and Strings, you could do:
List<dynamic> list = [9, 10, 'Plus One'];
list.sort((a, b) {
if ((a is int && b is int) || (a is String && b is String)) {
return a.compareTo(b);
}
if (a is int && b is String) {
return -1;
} else {
assert(a is String && b is int);
return 1;
}
});
print(list);
If you need to potentially handle other types, you will need to adjust the callback appropriately.
If you want to sort dynamic list and want string before number(int or double) try
this code:
List<dynamic> list = [
'mahmoud',
14,
'zika',
9,
10,
'plus One',
5,
'banana',
1,
2.5,
'apple',
2,
1.2,
'ball'
];
list.sort(
(a, b) {
if ((a is num && b is num) || (a is String && b is String)) {
return a.compareTo(b);
}
// a Greater than b return 1
if (a is num && b is String) {
return 1;
}
// b Greater than a return -1
else if (a is String && b is num) {
return -1;
}
// a equal b return 0
return 0;
},
);
print(list);// [apple, ball, banana, mahmoud, plus One, zika, 1, 1.2, 2, 2.5, 5, 9, 10, 14]

simplify multiple filters in flutter

here is my code
value = data.where((i) => (i.type == 1 || i.type == 3)).toList();
if (data.length == 0) {
_final = null;
} else {
dataDefault = value .where((i) => i.isdefault == 1).toList();
}
if (dataDefault.length == 0) {
data[0].isdefault = 1;
}
finalvalue = dataDefault.where((i) => i.isdefault == 1).toList();
Here is my json
[
{
"id": 129,
"type": 3,
"is_default": 1
},
{
"id": 130,
"type": 1,
"is_default": 0
},
{
"id": 131,
"type": 5,
"is_default": 1
}
]
What i need is ,i need to check if the array is in type 1 or 3 if no array available i need to set first array and change as default and then i want to filter the whole array and check default field
It is working fine but i need to simplify the code and also needs to know the approach is correct
From your description, I get the original request is:
if(array not contains type1 or type3){
set array[0] to default
}
result <- get all 'is default' from array
Simplified version:
var result; // null as default
if (data != null) {
if(data.where((i) => (i.type == 1 || i.type == 3)).isEmpty) {
data[0].isdefault = 1;
}
result = data.where((i) => i.isdefault == 1).toList();
}
Or use null safety check ? and ??
if(data?.where((i) => (i.type == 1 || i.type == 3))?.isEmpty ?? false){
data[0].isdefault = 1;
}
final result = data?.where((i) => i.isdefault == 1)?.toList();

How to remove null and 0 from the map?

I tried to remove NULL from the map for a long time.....
sortedMap.removeWhere((key, value) => value == "NULL");
sortedMap.removeWhere((key, value) => value == NULL);
...however, this could not be done, and I decide to set the default value "No Data", this solved the problem.
sortedMap.removeWhere((key, value) => value == "No Data");
and, this solved the problem! Now, I can’t remove the value "0" from the map now.
sortedMap.removeWhere((key, value) => value == "0");
sortedMap.removeWhere((key, value) => value == 0);
This's my map:
{h_1: 0, h_2: 1, h_3: 1, h_4: 1, h_5: 0, h_6: 1}
Please, tell me.
How to solve this problem?
Thanks for any help
Is it same solution like yours?
In my case, it works well.
void main() {
Map data = {'h_1': 0, 'h_2': 1, 'h_3': 1, 'h_4': 1, 'h_5': 0, 'h_6': 1};
print(data);
data.removeWhere((k, v) => v == 0);
print(data);
}
What is NULL? Is it perhaps null you are looking for to remove?
(Null in the case below is a class from dart:core)
import 'package:flutter_test/flutter_test.dart';
void main() {
test(
'Remove null',
() {
Map<String, dynamic> map = {
'a': 1,
'b': 2,
'c': null,
'd': 4,
};
print(map);
map.removeWhere((key, value) => value == Null);
print(map);
map.removeWhere((key, value) => value == null);
print(map);
// assert
},
);
}
This prints:
{a: 1, b: 2, c: null, d: 4}
{a: 1, b: 2, c: null, d: 4}
{a: 1, b: 2, d: 4}
✓ Remove null