If equals or greather than 3 rows - sql-server

I'm trying to make a review section, but I only want it to show if there's 3 or more reviews posted.
When I do this I just seem to get an error.
#{
var reviewCount = db.Query("SELECT COUNT(DISTINCT ID) FROM Reviews");
}
#if (reviewCount >= 3)
{
<section class="col-md-12 reviews">
<h2>Anmeldelser</h2>
#foreach (var row in db.Query("SELECT TOP 3 * FROM Reviews ORDER BY DateTime"))
{
<article class="col-md-4">
<p>" #row.Text "</p> <p>- #row.FirstName #row.LastName</p>
</article>
}
</section>
}

If you just want a single value returned from your database operation, you should use the QueryValue method instead of Query. The first returns a scalar value whereas the second returns a collection of DynamicRecord objects.
#{
var reviewCount = db.QueryValue("SELECT COUNT(DISTINCT ID) FROM Reviews");
}
#if (reviewCount >= 3){
...

Related

How to structure a multi array to process and sort WooCommerce Order Item data?

How can I extract details from an order into an array, while on the "Order Edit" page and then loop through them?
My array structure isn't correct as can't handle a 3rd item-detail. So each order item somehow needs to be placed into its own sub-array.
Then I want to be able to sort them on item-name and print them out with an echo.
How to achieve the correct array structure to input them and extract?
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'additional_admin_order_data_block_after_shipping_address', 100 );
function additional_admin_order_data_block_after_shipping_address(){
echo '</div><div class="order_data_column order_packing_column">
<h3>' . esc_html__( 'Packing Items', 'woocommerce' ) . '</h3>';
// here goes your code and content
//custom array to list products:
$products_array = array();
$product_tally = 0;
global $post; // <=== Alway at the beginning
// Get an instance of the WC_Order Object
$order = wc_get_order( $post->ID );
// let's decide what order item types we would like to get
$types = array( 'line_item');
// defaults to line_item which allows to get products from order
foreach( $order->get_items( $types ) as $item_id => $item ) {
// order ID
$item_order_id = $item->get_order_id();
// product only and no bundle 'summary' item
if( empty( $item['woosb_ids']) && $item->is_type( 'line_item' ) ) {
// order item name (product title, name of a shipping method or coupon code)
$item_name = $item->get_name();
if ( array_key_exists($item_name, $products_array)) {
$products_array[$item_name] += $item['qty'];
} else {
$products_array[$item_name] = $item['qty'];
}
$product_tally = $product_tally + $item->get_quantity();
}
}
arsort($products_array);
echo '<div class="woocommerce_order_items" id="packing-table">
<div class="thead">
<div class="tr">
<div class="item sortable">Product</div>
<div class="quantity sortable">Qty</div>
</div>
</div>
<div class="tbody" id="order_line_items">';
//display list:
foreach ($products_array as $title => $quantity) {
echo '<div class="lineitem">
<div class="item">';
echo $title.'</div>
<div class="quantity">'.$quantity.'</div>
</div>';
}
echo '<div class="lineitem tally">
<div class="item">Tally of Items</div>
<div class="quantity">'.$product_tally.'</div>
</div>';
echo '</div>
</div>';
}

Drilldown method always return null with Cube JS

I have a problem with cubejs library. when i call the method drilldown from a measure.
i was puting the drill members field on my schema but always return null.
this is my measure from the schema
compromisoFinalMuyAlta: {
sql: `
CASE
WHEN (
(sum(
CASE
WHEN (${CUBE}.id_tipo_cobertura = 4 AND ${CUBE}.id_bi_rango_sla = 5) THEN ${CUBE}.total_cuentas_rango
END
) - ${inactivasTotalMuyAlta}) > 0
) THEN ROUND(SUM(
CASE
WHEN (${CUBE}.id_tipo_cobertura = 4 AND ${CUBE}.id_bi_rango_sla = 5) THEN ${CUBE}.total_cuentas_rango
END
)) - ${inactivasTotalMuyAlta}
ELSE 0
END
`,
type: `number`,
title: 'Resultado final',
drillMembers:[`${CUBE}.id_tipo_cobertura`,`${CUBE}.total_cuentas_rango`]
}
this the method what i have the resultset and try to call the drilldown method
public BuscarDetalle(columnValue) {
console.log(this.resultQuery);
const queryDetail = this.resultQuery.drillDown({
xValues: ["0 - 3 días"],
yValues: ["compromiso_real.id_cliente", "SlaCompromisos.compromisoFinalMuyAlta"]
});
console.log(queryDetail);
}
and this is the table from cube.
enter image description here
First of all, Thanks.

How to find select distinct values from mongodb

How can I use find select distinct in angularJS then counting it, I'm new to mean-stack and I don't know how to use distinct method.
Here is my table structure with sample data:
donation_no:"3124"
donors_name:"Jeremy Adrian De Vera"
date:"12/26/2018"
time:"8:30"
blood_group:"B"
volume:"2"
branch:"Pasig"
I already tried it using php like this but how can I do this in angular:
$query = "SELECT DISTINCT fruits, COUNT(*) as counter FROM my_table WHERE date_tested BETWEEN '$date_from' AND '$date_to' GROUP BY fruits";
Here is my code for finding the values from my db
api.js
router.get('/blooddonationmanagement', function(req, res) {
Blooddonation.find({},function(err, blooddonations) {
res.json({ success: true, blooddonations: blooddonations });
});
});
blooddonationCtrl.js
angular.module('blooddonationControllers', [])
.controller('blooddonationCtrl', function(Blooddonation,$scope) {
var app = this;
function getBlooddonations() {
Blooddonation.getUsers().then(function(data) {
app.blooddonations = data.data.blooddonations;
});
}
chapters.html
<tr ng-repeat="person in blooddonationmanagement.blooddonations">
<td>{{ person.branch}}</td>
</tr>
How can I count the distinct values then counting it from my db, Im expecting like this:
branch count
Pasig 29
Manila 16
Pasay 19
You should try distinct()
Blooddonation.distinct('branch',... );
Just read the docs here: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/
You can use:
db.collection.distinct()

How to skip the locator which does not exist in selenium webdriver?

I have around 50 rows in a page. But those items are in sequence.
The problem is when someone entered and deleted that table row. That id would not be there in page..
Example:
User added 1st record: id 101 added.
User added 2nd record: id 102 added
User added 3rd record: id 103 added.
If user deletes 2nd record, then two records would be there on the page but with id 101, 103.
I am trying to write that if that id is present then get the text else leave that in the for loop. I am getting only records till it found if that id is not found getting NoSuchElementException is displayed.
Please correct the code. But i want to the solution that if that id not exist, skip and run the else part.
for (int i = counterstart; i <= counterend; i++) {
if(driver.findElement(By.xpath("//*[#id='" + i + "']/a")).isDisplayed()){
System.out.println(i+" is present");
String Projects = driver.findElement(By.xpath("//*[#id='" + i + "']/a")).getText();
System.out.println(Projects);
} else{
System.out.println(i+" is NOT present");
}
}
The exception that I get:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //*[#id='7593']/a (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 503 milliseconds
Try this method isPresent instead of isDisplayed.
public boolean isPresent(WebElement e) {
boolean flag = true;
try {
e.isDisplayed();
flag = true;
}
catch (Exception e) {
flag = false;
}
return flag;
}
How about this:
for (int i = counterstart; i <= counterend; i++) {
WebElement element;
try{
element = driver.findElement(By.xpath("//*[#id='" + i + "']/a"));
}catch(NoSuchElementException n)
{
element = null;
}
if(element !=null){
System.out.println(i+" is present");
String Projects = driver.findElement(By.xpath("//*[#id='" + i + "']/a")).getText();
System.out.println(Projects);
}else{
System.out.println(i+" is NOT present");
}
}
Find all the parent of all the elements you want to get the text from and use it to drill down, this way you won't get the exception
Assuming the html looks like this
<div id="parent">
<div id="11">
<a>text</a>
</div>
<div id="12">
<a>text</a>
</div>
<div id="13">
<a>text</a>
</div>
</div>
You can do this
// get all the elements with id
List<WebElement> ids = driver.findElements(By.cssSelector("#parent > div"));
// get all the texts using the id elements
for (WebElement id :ids) {
String projects = id.findElement(By.tagName("a")).getText();
System.out.println(projects);
}

Laravel 5 form another array from database result

I'm new to Laravel and I've been struggling too long with this now, tried to search SO and google for information but can't solve this.
I'm creating blog and need to make some kind of navigation/archive which displays year and months and how many blog posts there have been per year and invidual month. By clicking those years/months I would then display posts during that time period on different view.
I want them to be displayed in the view like this:
2015 (10)
January(3)
February(3)
March(3)
April(1)
2014 (2)
May(1)
June(1)
And so on.
I got database query like this:
$links = \DB::table('posts')
->select(\DB::raw('YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name, COUNT(*) id'))
->where('active',1)
->groupBy('year')
->groupBy('month')
->orderBy('year', 'desc')
->orderBy('month', 'desc')
->get();
Which gives me table like this:
array:3 [
0 => {#275
+"year": "2015"
+"month": "10"
+"month_name": "October"
+"id": "3"
}
1 => {#274
+"year": "2015"
+"month": "9"
+"month_name": "September"
+"id": "1"
}
2 => {#273
+"year": "2014"
+"month": "8"
+"month_name": "August"
+"id": "1"
}
]
How can I print it on my view like I described?
If I go through the array in views like this:
#foreach($links as $link)
<h3 class="text-uppercase">{{ $link->year }}</h3>
<p><small class="blog_date">{{ $link->month_name }} ({{ $link->id }}) </small>
#endforeach
I tried to use foreach-loop in my Controller to create another array from DB-results where structure would be in correct form and I could just use foreach to print it on the view, but couldn't get it work.
I know I'm near the solution, but I'm still learning. Please someone tell me which is the best way to do this.
Try this in your blade:
<?php $first_loop = 0; ?>
#foreach($links as $link)
#if($first_loop == 0)
<?php
$first_loop = 1;
$current_year = $link->year;
?>
<h3 class="text-uppercase">{{ $link->year }}</h3>
<p><small class="blog_date">{{ $link->month_name }} ({{ $link->id }}) </small></p>
#else
#if($current_year == $link->year)
<p><small class="blog_date">{{ $link->month_name }} ({{ $link->id }}) </small></p>
<?php
$current_year = $link->year;
?>
#else
<h3 class="text-uppercase">{{ $link->year }}</h3>
<p><small class="blog_date">{{ $link->month_name }} ({{ $link->id }}) </small></p>
<?php
$current_year = $link->year;
?>
#endif
#endif
#endforeach
You can do some preparation on your data before outputting it. For example:
$links = // .. your query;
$years = array_pluck($links, 'year');
$results = [];
foreach($years as $year)
{
$posts_count = 0;
$posts = array_where($links, function($key, $value) use ($year, $posts_count){
if($value['year'] == $year)
{
$posts_count += $value['id'];
return true;
}
return false;
});
$results[$year] = ['posts' => $posts, 'posts_count' => $posts_count];
}
You can do the above in some repository/service class, or even in the controller if you want (although not recommended)
And then in your view you can have something like:
#foreach($results as $year => $result)
{{ $year }} {{ data_get($result, 'posts_count') }}
<ul>
#foreach(data_get($result, 'posts') as $monthly_post)
<li>{{ data_get($monthly_post, 'month_name') }} {{ data_get($monthly_post, 'id') }}</li>
#endforeach
</ul>
#endforeach
This code is untested, so use it as inspiration for your approach, not a copy-paste solution.

Resources