Query distinct row in Query Builder - mysql

I have a table like this:
I want to get distinct row from this. I use:
DB::table('dmspro_mys_campaign_product')->distinct()->get();
But it still give result with 3 rows. I expected 2 rows.
How I can fix this?

Simply pass the field name inside the distinct function. like this(in case if you want distinct based on 'compeign_code' column)
DB::table('dmspro_mys_campaign_product')->distinct('compeign_code')->get();
in case if you want to distinct the field based on more than one column value then you can use the select method.

In my opinion the answer is correct. District rows give you the results where every row is unique. Since there are slight variations between all three rows, you get all three because no row is exactly equal to another row.
I hope I could help,
Sebastian

You need a distinct value from dmspro_mys_campaign_product table so Database query needs pass column name.
DB::table('dmspro_mys_campaign_product')->distinct('product_id')->get();

You simply need to mention the column name in distinct('compeign_code').
DB::table('dmspro_mys_campaign_product')->distinct('compeign_code')->get();
Or you can try
DB::table('dmspro_mys_campaign_product')->groupby('product_id')->distinct()->get();
If you want to get only one column then you may use this.
DB::table('dmspro_mys_campaign_product')->distinct()->select('product_id');

Related

Running the SUBSTRING_INDEX query on a column

I have this column called location in a table called raw_data. Each row have lots of varchar data separated by [, ]. Examples
Row 1 dusseldorf, kjsbdfygs4, Germany
Row 2 768768745h, kiev, Ukraine
Row 3 %%%%666, Accra, Ghana
Yes some make no sense. Im trying to select the last part of the string which is the country and display it.
I tried using the substring index query and understand how it works but cant seem to understand how to apply it to a column.
You can use as per below-
SELECT SUBSTRING_INDEX(my_column,',',-1) FROM raw_data;
Note: If you need 1st part then use only 1 instead of -1.
use like this
SELECT SUBSTRING_INDEX(location ,',',2);
go to below link for explanation
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php

SSRS adding a percentage column based on a specifc column category in a matrix/tablix

I have a matrix/tablix set up so there are 4 row groups going down the left-hand side and a column group called RegCompCategory:
When the report is run, the RegCompCategory column group produces 3 distinct columns based on the categories in the underlying data:
What I'd like to do is add another column before the RegCompCategory column group that will display a percentage of the "fully-marked" column against the "total" column:
I'm guessing I will need to write an expression for the fields highlighted above, but I'm not sure how to reference the RegCompCategory to identify specifically the "Fully-Marked" category of data.
Could someone give me a few pointers? Many thanks.
Try:
=Count(IIF(Fields!RegCompCategory.Value="Fully-Market",Fields!RegCompCategory.Value,Nothing))
/
Count(Fields!RegCompCategory.Value)
It will count the number of Fully-Market rows and divide by the total rows. I think that is what you are expecting.
Let me know if this helps you.

Selecting multiple rows at once when I know what ID's I want

I have a table with 2 columns...ID and Content.
Is there a way to select data from multiple rows at once from a mysql database when I know what row ID's I want to retrieve data from.
Possibly putting the Content column results into an array or some sort if that is the best way. I am trying to prevent a ton of sql query's. There are just 2 columns, an ID and a content column.
Any input would be great.
You can use an IN statement to achieve this:
SELECT *
FROM mytable
WHERE ID IN (1,2,3,4)

Does MySQL "SELECT LIMIT 1" with multiple records select first record from the top?

I've searched and searched and can't find an answer to this question, I'm probably asking it in the wrong way.
I am querying an employee database.
I need to get details based on a position id, however there could be multiple records for that position id as this organisation has permanent employees and temporary employees that act against the same position.
So, in order to get the CURRENT occupant of the position id, I need my query to SELECT the FIRST record that matches the position string, from the TOP DOWN.
will this select the first matched record from the top?
SELECT * WHERE `position_id`="00000000" LIMIT 1;
Thanks in advance.
You need an ORDER BY clause to define the ordering between the individual records your table. If you do not use ORDER BY you can assume no fixed order between the records, and you could get a new order each time you executed the query.
From the manual:
With one argument, the value specifies the number of rows to return from the beginning of the result set
So with LIMIT 1 you get the first row from the result set. What the result set is depends on engine used and which indexes you have. If you want the first row added, you need to create another column to define that.
It just gets one at random*. There's no way to tell which one it will be, unless you add an ORDER BY clause.
* Not really at random, of course. It depends on the way the records are stored and repeated queries will probably return the same result every time, at least as long as you don't modify the table or its contents. I actually mean, you cannot be sure.

MYSQL - create single record out of similar rows, chose values of greatest length for most columns

Here is my case, I have a database table with below fields:
name
place_code
email
phone
address
details
estd
others
and example data
If you look at the above example table, first three records are talking about xyz and place code 1020.
I want to create a single record for these three records based on
substring(name,1,4)
place_code
(I am lucky here for all the similar records satisfies this condition and unique in the table.)
For the other columns which record column length has max. For example again for the above 3 records email should be test#te.com, phone should be 657890 and details should be "testdetails".
This should be done for all the table. (Some has single records and some has max 10 records.)
Any help on query that helps me to get the desired result?
Answer
Some one posted the below answer and deleted it . But that looks a good solution
SELECT max(name),
place_code,
max(email),
max(phone),
max(address),
max(details),
max(estd),
max(others)
FROM table_x
GROUP BY substring(name,1,4),place_code
Please let me know if you guys see any issues in it ?
Thank You all
Kiran
You need the awesome GROUP_CONCAT aggregate function.
SELECT place_code,
substring(name,1,4) name,
GROUP_CONCAT(email),
GROUP_CONCAT(Phone),
GROUP_CONCAT(details)
FROM table
GROUP BY place_code, substring(name,1,4)
It has options allowing you to control things like the order of items in the string and the separators. See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT max(name),
place_code,
max(email),
max(phone),
max(address),
max(details),
max(estd),
max(others)
FROM table_x
GROUP BY substring(name,1,4),place_code