laravel - get subtotal from rows having similar column values under another column with same values - database

The DB has a table namely records. Among the fields are product_id,quantity,store_id.
I need to get the sum total of quantity of those rows which belong to a certain store_id and then have same product_id values.
For example , the table has values : (2,3,4),(2,1,5),(1 2,2,4) Then I need to get the sum total of quantity along with other columns from 1st and 3rd rows. And the 2nd row will also be present in the result.
Let us assume the Controller name to be RecordController and the model name to be Record.
How should I write the query ?
Edit : the table has values : (2,3,4),(2,1,5),(1 2,2,4)

It can be done using a single query.
Try This:-
DB::table('records')
->select('product_id',DB::raw('sum(quantity) as quantity'),'store_id')
->groupBy('product_id')
->groupBy('store_id')
->get();
Explanation:
This query will fetch all the records & group them by product_id & store_id so will only get a single row for a single product in the store. And then it will display the sum of quantity in the output.
Update:-
Using ORM:-
\App\YourModelName::select('product_id',DB::raw('sum(quantity) as quantity'),'store_id')
->groupBy('product_id')
->groupBy('store_id')
->get();
But you need to write the raw query in any case, it can't be done using default ORM functionality

Related

Create a cross reference table using Golden record and associate other records to the record

I have a table where I have ranked all the rows based on the created date column and have the rank indicated on the table as below
main table
I would like to create a cross-reference table with the golden record as the recurring column and the other two records as associated records as below.
output desired
I would like to know how I can achieve this using SQL.
I have tried creating a seperate table with all ID numbers (Rank = 1) and then joining it with the main table to get the ones with rank 1,2 and 3 associated with it. But it doesnt seem to work as I intend to.
output
I have not tested but something like this should work. You might want to add a name_id field.
select b.id_number,a.id_number
from table a
join table b on a.name=b.name
where b.rank=1

Load Data to Hive array column

I have two Hive tables as shown below, along with their columns
Tbl_Customer
Id
Name
Tbl_Cntct
Id
Phone
One Id can have many phone numbers so I have a table
Tbl_All
Id
Name
Phn_List ARRAY
My question is on how to load data from Tbl_Custome and Tbl_Cntct into Tbl_All.
I can do it in PIG, but want to do same in Hive.
Thanks
Insert overwrite table Tbl_All
select cus.id,cus.name,collect_set(ctc.phone)
from Tbl_Customer cus join Tbl_Cntct ctc on cus.id = ctc.id
group by cus.id,cus.name
The collect_set UDAF is a function collects the column into an array with no duplicates.If you want to remain all the value include duplicated ones,use collect_list function

How to show data in column in SSRS

I'm using SSRS for my reporting, my reporting solution is in Visual Studio 2008 Business Intelligence Development Studio.
I have a report in which the data should be displayed in this format.
I have added a Column Group in my table which is having the values of Customer Name and details, the data is coming fine in the vertical format i.e column after column.
My Issue :
There should be only three columns in each row, after three records the next row should begin and again not more than three records should be displayed as shown in the image above.
My attempts :
I tried to add a row group and in that gave the expression
= Ceiling(Fields!Row_Count.Value/3)
here Row_Count is a field which is coming from my query which holds the serial number of the records.
My SQl Query
SELECT Row_Number() over(order by table_ID) AS Row_Count, Field_1,Field_2 from MyTable
In my Column group i have Customer Name and in my Row Group i have other details of the customer. The data is getting populated column wise but the issue is its not breaking the current row after three records.
Below is my table of report.
You were on the right track. Say you have data like this:
I have created a tablix like this:
The Row Group expression is:
=Ceiling(Fields!Row_Count.Value / 3)
This works together with the Column Group expression to split over three columns:
=(Fields!Row_Count.Value - 1) Mod 3
The other thing to note compared to your tablix is that CustomerName is not in a table header row, but rather there are two row header rows, one for CustomerName and one for Details.
This is looking OK to me, obviously you can format to taste:

Sql query to check combinations and match it to the given combination

I've a scenario in Sql where I've following schema.
If I have 3 items in the Item table then one unique combination of all the items will be assigned to a user. For ex:
Items:
1
2
3
Then combinations will be: {1}, {2}, {3}, {1,2}, {1,2,3}, {1,3}, {2,3) all are unique combinations.
Any of these combination will be assigned to a single user.
Now I want to find out given combination belongs to which user, how can I find that? For ex: I'll pass items list {2,3} then it should return the userid who is having that combination from the table UserItemCombinations. {2,3} is passed as comma separated string to a SP. I've taken 3 items as example, this table may contain n number of items. Users number will be dependent on the number of combinations. For ex: For three items there are 7 combinations so 7 users will be there user table.
UserItemCombinations will have one row for each user-item, and one user can have only one combination, so if the query combination is {2,3}
select userid from user where userid not in
(select distinct userid from UserItemCombinations where itemid in
(select itemid from item where itemid not in (2,3));
If there's not too much updates in UserItemCombinations and the performance of the desired query is critical enough, you would make additional field in User table, i.e. Items and create SP that fills in those values per every user. Stored proc will select sorted items per each user in loop and concatenate them into one string to put in User.Items field. You can also make trigger on UserItemCombinations for INSERT, UPDATE, DELETE and recalc the value again.
You may also create index on that field.
select userid from UserItemCombinations where itemid in (2,3) and itemid not in
(select itemid from Item where itemid not in (2,3));

Storing multiple employee IDs in one column of data

Web app is being written in classic ASP with a MSSQL backend. On this particular page, the admin can select 1 or any/all of the employees to assign the project to. I'm trying to figure out a simple way to store the employee IDs of the people assigned to it in one column.
The list of employees is generated from another table and can be dynamic (firing or hiring) so I want the program to be flexible enough to change based on these table changes.
Basically need to know how to assign multiple people to a project that can later be called up on a differen page or from a different query.
Sorry for the n00bish question, but thanks!
Don't store multiple ID's in one column! Create another table with the primary key of your existing table and a single ID that you want to store. You can then insert multiple rows into this new table, creating a 1:m (one to many) relationship. For example, let's look at an order table:
order:
order_id
order_date
and I have a product table...
product:
product_id
product_name
Now, you could go down the road of adding a column to order that let you list the products in the order, but that would be bad form. What you want instead is something like..
order_item:
order_item_id
order_id
product_id
quantity
unit_price
You can then perform a join to get all of the products for a particular order...
select
product.*
from orders
inner join order_item on order_item.order_id = order.order_id
inner join product on product.product_id = order_item.product_id
where orders.order_id = 5
Here's an example order_id of 5, and this will get all of the products in that order.
You need to create another table that stores these values such as. So this new table would store one row for each ID, and then link back to the original record with the original records ID.

Resources