Sequelize select all columns with few columsn with alias - sql

I have a table with say 10 columns. I want to fetch all columns from the table with 2 columns using alias. Is there any way to do this?
Basically, I want to convert following query to Sequelize
select *, processor_txn_id as processor_id from transaction

If you want to add all columns and some of them twice with a different name then you can use include option in attributes:
const results = await Model.findAll({
attributes: {
include: [['field_name', 'alias']]
}
})

Related

Scala, Doobie, PostgreSQL - how to select from array/jsonb column?

I have a simple db table:
create table if not exists players
(
id bigint,
name text,
results text[]
);
Now I would like to create select query, where I want only rows with passed results. So I created a scala code with doobie:
def getPlayers(id: Int, result: String): Query[Int] = {
sql"select id from players where results ? $result".query[Int]
}
But it didn't work as expected. My question is how to select from array column in postgresql? Currently I have results as an array, but I could change it to jsonb if it is easier.
You can use the following query:
select id from players where $result = any(results);
You can find more information here:
https://www.postgresql.org/docs/current/functions-comparisons.html

Laravel Eloquent Getting Many To Many Relations Per Pivot Table Field Condition

I have a problem with Laravel 7 Eloquent. I have 2 tables joined by many to many relation. Table user is connected to PaymentModule by pivot table PaymentModuleUser. I need to use where on pivot table on statement is_active = 1. When i use toSQL() in my model SQL returns good results but when i check my Eloquent Query in Controller, data that i revicive ignore my wherePivot method (return all data from table ignoring is_active = 1 field subscribers = null and get me this user.. I must do it if my pivotWhere = null dont show this user). Could you point me where i get wrong with my code?
My UserTable model:
public function subscriber(){
return $this->belongsToMany(PaymentsModule::class, 'payment_module_user', 'user_id')->withPivot('is_active');
}
MyController:
$users = User::with(['subscriber'=>function($query)
{
$query->wherePivot('is_active','=', 1);
}])->get();
print_r($users);
Try with
$users = User::with('subscriber' => function($query) {
$query->where('payment_module_user.is_active', 1);
})->get();
print_r($users);
The pivot table is already joined by eloquent, so just start using it
Found it here.
In your controller, try using this
$query->wherePivot('is_active', 1);
Instead of,
$query->wherePivot('is_active','=', 1);

access 2016 - compare two tables and return matched records using query

My goal is to create a query, macro or any solution that can do the task described below.
Lets say I have an access 2016 table named "student" with 12 records like this:
And then, lets say I have a second table named "matchme" with 4 records like this:
I need to find a way to
=> first, create a query that returns result of "graduation_date" are equal to Date "1/31/2017" from Table "student" .
=> second, from the result returned from first step, create a query that compare "email" from "student" table with "email" from "matchme" table, and return the [matched] record result.
So the desired result would be:
since the email gary#xxx.com and thomas#xxx.com exist in both tables.
How can I create a query like this?
you can download my access file from here: experiment.accdb
Simple:
select * from student
inner join matchme on student.email = matchme.email
where student.graduation_date = '1/31/2017'
Looking to your data sample you need a join on date and name between the two tables
select * from student
inner join matchme on student.graduation_date = matchme.graduation_date
and student.email = matchme.email
where student.graduation_date = '1/31/2017'

SQL: Merge 2 Columns or more from different tables into one Alias new Column

I have 2 tables: DisconnectionsData and DisconnectionsVoice.
both of them have columns: WeekNum,Month,Quarter,Year,IncomeLost and ID.
the names of the columns are different between the 2 tables, but the data inside them is parallel (Quarter and QuarterNumber is literally the same).
my wish is to FULL JOIN both tables into one table with only 6 columns.
i cant figure out how to make an alias, for example: how do i merge DisconnectionsData.Quarter and DisconnectionsVoice.QuarterNumber
into one column with the alias name of QuarterOfDisconnection.
that the desired result:
thank you.
You don't specify which sql are you using? You can try below.
SELECT *
FROM DisconnectionsData
UNION ALL
SELECT *
FROM DisconnectionsVoice

Check duplicates in nvarchar column of table in SQL Server

I have two tables: Import and ImportItem. One Import has multiple ImportItem. One ImportItem has one Import.
In my ImportItem I have a column CountryIDs. It's an NVARCHAR(MAX). It is a string of IDs with ; as delimiter so for example: 3; 4; 5;...
I need to determine duplicates, actually if I run on a duplicate I need to raise some error. So 3; 4; 3; ... 3 is duplicate.
I have a split function (I did not write that function, I was told to use it) that splits a string (nvarchar) and returns a table with columns ItemNo and Item (from previous example it would return 1 for ItemNo and 3 for Item, 2 for ItemNo and 4 for Item.
I need to write a procedure. My question is, how can I iterate through that column of CountryIDs for given ImportItem ID and split it and check for duplicates?
You can use apply to get the list of items, something like this:
select ii.itemno, s.c, count(*) as cnt
from importitem ii apply
dbo.split(ii.countryids) as s(c)
group by ii.itemno, s.c
having count(*) > 1;
Once you have the list, you can decide how to process it in the stored procedure.