In Eloquent, is there a way to use sql functions in relationship query - laravel

I am working in Eloquent with Laravel, and I have a table named invoices.
Each invoice has a prefix that identifies the dealer id.
For example,
01-1234 is an invoice from dealer 01. 02-5678 is an invoice from dealer 02.
Further complicating things is that the dealer's id is not 01 or 02, but DEALER01 or DEALER02.
I would like to know if it's possible to get a query like this to work, so that the invoice object has an associated dealer object, without having to write a lot of repetitive code to loop through the invoices and manually link them.
public function dealer() {
return $this->hasOne(Invoice::class, 'dealerId', 'concat(\'DEALER\', substr(Invoice.invoice, 1, 2))')
}
Here are a few fields from the table structure of invoice:
invoice
cust
date
01-1234
1
2021-06-05
02-1235
2
2021-06-06
And here are a few fields from the structure of dealer:
dealerId
county
default_sales_acct
DEALER01
Some county
1234
DEALER02
Some other county
5678
The only fields needed to link the two tables in this case are dealerId and the first two characters of invoice.

Related

How to correct the error, A table of multiple values was supplied where a single value was expected, when used with SELECTCOLUMNS in DAX

I have a table 1- Invoices report that looks like this:
Invoice Number
Customer Number
1
cus1
2
cus2
3
cus3
4
cus4
...
...
I want to make a column that has only the Customer Number.
I have used the following Expression:
Invoice Customers Number =
SELECTCOLUMNS(
'1- Invoices report',
"Customer Number",
'1- Invoices report'[Customer Number]
)
With this I am getting the error A table of multiple values was supplied where a single value was expected.
Any solution for this?
The result of SELECTCOLUMNS() is a table, not a column.
Make sure to use it as a calculated table, not a calculated column or measure:

How to put measures from multiple tables into one matrix in Power BI?

I have 8 tables with data of sold products. Each table is about a unique product. In Power BI, I want to create a matrix, containing the sold quantities (values) per product (rows), per month (columns), and the number of unique customers who bought the products.
Each of the 8 tables with the sales data has the following structure. So the App ID is different for each table, but is constantly the same within a table. Example for a Cars table:
Customer ID Month App ID
29273 2020-3 1
90283 2018-5 1
55824 2016-12 1
55824 2018-10 1
55824 2021-1 1
So, a bicycle table would have the same structure, but then the App ID's would be, for example 2, in the entire table.
I have two tables that are connected with the 8 product tables in a one-to-many relationship. The Calendar table based on the Month column, and the App table based on the App ID column.
The table Calendar:
Month
2015-1
2015-2
2015-3
2015-4
2015-5
...
...
The table Apps:
ID Name
1 Cars
2 Bicycle
3 Scooter
4 ...
So, the structure is:
I created the Calendar en Apps tables so that I could use them for the matrix, but it doesn't work like I want so far. At the end, I want to create a matrix like this (where P = the number of products sold, and C = the number of customers in that month for that product):
Product 2015-1 2015-2 2015-3 2015-4 2015-5 ...
P C P C P C P C P C
Cars 3 2 5 5 7 6 2 1 4 2
Bicycle 11 9 17 14 5 5 4 4 8 6
Scooter ...
Skateboard ...
As mentioned, I made that Calendar and App table so that I can use the columns from it to fill the labels in the rows and columns. What I am unable to do is create such a 'general variable' of the number of products sold per product, and the number of customers associated with it.
Can someone explain to me how I can fill the matrix with the numbers of products (and customers) sold, so that the matrix looks like the one described above?
I think this is pretty straight forward. You actually don't need the 'Calendar' table as it only contains the same info as is already in the 'Sales' table.
You should configure the matrix like this:
Rows: 'Name' (from the 'Apps' table)
Columns: 'Month' (from the
'Sales' table)
Values:
C = Count distinct of CustomerId (from 'Sales' table) [this counts the unique customers per month and app)
P = Count of CustomerId (from 'Sales' table) [this counts the rows of the 'Sales' table which is your number of products if every row represents 1 sale)
The different aggregations (count distinct, count) can be found under the Values' options:

How to query distinct column value when query all row data

I hava one MySQL Table
id name birthdate city
1 Owen 2011/01/01 USA
2 Mark 2012/05/01 UK
3 Marry 2011/01/01 JP
4 John 2011/01/01 JP
First,I uesd jqgrid to read all row data. But Now,I want to know when birthdate=2011/01/01,how many different city in the table.
Can don't used sql,only used jqgrid plugin?
You are looking for distinct function.
SELECT DISTINCT(city) FROM table WHERE birthday = "2011/01/01";

Select data from Multiple table and show merged data into view mvc3

I have two tables like
USER_LOGIN
USER_ID USER_NAME PASSWORD
--------------------------------------------
1 User 1 some data
2 User2 somedata
3 User3 Some Data
And
USER_DETAIL
ID USER_ID NAME ADDRESS
-------------------------------
1 2 Name 2 Address
2 3 Name Three
Now how can select data from this two table by user id and show merged data into view??
Do your join operation in SQL Server as a function
Bring the result of the function from database to application using LINQ
Apply your result to the grid datasource
Yes, You can make it through join concept in SQL server
select * from USER_LOGIN A join USER_DETAIL B on A.USER_ID=B.USER_ID
Now you'll get the all data from joining two tables and can pass to a datatable or dataset anything, then you can pass it to controller and to view. Still you can choose you join style it may be "leftjoin","rightjoin" like this.
Please refer these links for clear explanation
http://www.w3schools.com/sql/sql_join.asp
http://beginner-sql-tutorial.com/sql-joins.htm
Hope it helps!!!

Consolidating multiple rows to a single row

Is it possible to join multiple row values to a single row? The stored procedure from where I acquire the data that I use return multiple, almost identical rows except that the category-column dfferentiates for products that have been assigned multiple categories. I would like to consolidate these categories to one column, separated by new lines. Example data:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
------------------------------------------------
Product 1 2059102-1 20520 Retailer 2
------------------------------------------------
Product 1 2059102-1 20520 Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
------------------------------------------------
Product 2 2059102-2 5302 Retailer 3
Desired result:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
Retailer 2
Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
Retailer 3
Thank you!
Create an RDL table and setup a Grouping on the Detail section of the table with two grouping expressions (so you have a single Grouping in the Table Details, but with multiple grouping expressions). The grouping expressions should be one for each of the fields: Name, Article number.
Then put a column in the RDL table for each field (Name, Article number, Sales, Sales Category). The trick is putting a List control in the Sales Category cell. In the list add a textbox for the "Sales Category" field and I think you'll get the result you want.
There is also some nasty SQL tricks for "row concatenation", but that is not very maintainable IMHO.

Resources