0% found this document useful (0 votes)
23 views2 pages

Notepads

Uploaded by

sunasrasufiyan07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Notepads

Uploaded by

sunasrasufiyan07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//group by closing in sequelize

User.findAll({
group: ['field']
})

//min agregate function in sequelize

exports.getMinPrice = () => Item.findAll({


attributes: [[sequelize.fn('min', sequelize.col('price')), 'minPrice']],
raw: true,
});

//max agregate function in sequelize

exports.getMaxPrice = () => Item.findAll({


attributes: [[sequelize.fn('max', sequelize.col('price')), 'maxPrice']],
raw: true,
});

//count agregate function in sequelize

exports.getItemSaleCount = () => SaleItem.findAll({


attributes: ['itemId', [sequelize.fn('count', sequelize.col('itemId')),
'count']],
group : ['SaleItem.itemId'],
raw: true,
order: sequelize.literal('count DESC')
});

//sum agregate function in sequelize

exports.getBestSellerItems = () => SaleItem.findAll({


attributes: ['itemId', [sequelize.fn('sum', sequelize.col('amount')),
'total']],
group : ['SaleItem.itemId'],
raw: true,
order: sequelize.literal('total DESC')
});

//avg agreagte function in sequelize

sequelize.sync({logging: false}).then(()=>{
return Model.Rating.findAll({
attributes: [[Sequelize.fn('avg', Sequelize.col('stars')),'rating']]
})
}).then(res => {
res = res.map(r => r.get())
console.log(res);
})

//However, when trying to do the same through the association of "User", I get
separate values instead of getting average.

sequelize.sync({logging: false}).then(()=>{
return Model.User.findOne({
where: {id: 7},
include : [{
model: Model.Rating, as: 'seller_rating',
attributes: [[Sequelize.fn('avg', Sequelize.col('stars')),'rating']]
}],
attributes: {
exclude: ['password']
},
group: ['seller_rating.id', 'user.id'],
})
}).then(res => {
res = res.get()
res.seller_rating = res.seller_rating.map(r => r.get())
console.log(res)
})

//distinct function in sequelize

Project.findAll({
attributes: [
// specify an array where the first element is the SQL function and the
second is the alias
[Sequelize.fn('DISTINCT', Sequelize.col('country')) ,'country'],

// specify any additional columns, e.g. country_code


// 'country_code'

]
}).then(function(country) { })

<tr>
<div class="mb-3">
<td><label for="" class="form-label mt-3">City</label></td>
<td><select class="form-select form-select-lg mt-3" name="city"
id="city" required>
<option selected required>Select one</option>
<option value=""name="city" id="city">New Delhi</option>
<option value=""name="city" id="city">Istanbul</option>
<option value=""name="city" id="city">Jakarta</option>
</select></td>
</div>
</tr>

You might also like