Linq to SQL select one of every different result - sql

Let's say I have a table with two columns, name and template. Let's say i have multiple rows with the name foo and multiple rows with the name bar. All of them have template 3.
How do i perform a linq to SQL select that returns only one of each name with template number 3.
(from f in g_lisFilters where f.Template == "3" orderby f.Sortering select f).ToList();
above code returns a list of all items with template number 3. How do i select only one of every name?
Thanks in advance

You simply have to group the data by template, and take the first element of each group.
var firsts = from e in g_lisFilters
where f.Template == "3"
group by e.Sortering
into groups
select groups.First();
Or
var firsts = g_lisFilters
.Where(e.Template="3")
.GroupBy(e=>e.Sortering ,(key,g)=>g.First());

Have you tried using the 'Distinct' method? It ignores multiple results of the same.
Maybe this could help you:
http://msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx

Related

How to combine rows in BigQuery that share a similar name

i'm having trouble creating a query that'll group together responses from multiple rows that share a similar name and count the specific response record in them.
the datatable i currently have looks like this
test_control
values
test
selected
control
selected
test us
not selected
control us
selected
test mom
not selected
control mom
selected
what i'd like, is an output like the below that only counts the number of "selected" responses and groups together the rows that have either "control" or "test" in the name"
test_control
values
test
3
control
1
The query i have below is wrong as it doesn't give me an output of anything. The group by section is where im lost as i'm not sure how to do this. tried to google but couldn't seem to find anything. appreciate any help in advance!!!
SELECT distinct(test_control), values FROM `total_union`
where test_control="%test%" and values="selected"
group by test_control, values
use below
SELECT
REGEXP_EXTRACT(test_control, r'^(TEST|CONTROL) ') AS test_control,
COUNTIF(values = 'selected') AS values
FROM `total_union`
GROUP BY 1
As mentioned by #Mikhail Berlyant, you can use REGEX_EXTRACT to match the expression and COUNTIF to get the count of the total number of matching expressions according to the given condition. Try below code to get the expected output :
Code
SELECT
REGEXP_EXTRACT(test_control, r'^(test|control)') AS test_control,
COUNTIF(values = "selected") AS values
FROM `project.dataset.testvalues`
group by 1
Output

SQL Query getting not results

I have a Select statement that gets the items based on the greatest date. it works but these are selected value from asp.net controls and one of the controls is a multiselect listbox
as you can see DynamicAtrributeID 937766 is show one time:
I need it to show all the rows that were in 937766 as well as the other rows which could be just one row. Is this possible
SELECT VotingValueDynamicId,
DecisionValueID,
DynamicAttributeID,
VotingValue,
DecisionSurveyID,
VALUEDATEUPDATED,
RECORDSTATUS,
FROM Adjudicate.ONCOLOGY_DynamicDecisionValuesForCaseManager AS a
WHERE (VALUEDATEUPDATED =
(SELECT MAX(VALUEDATEUPDATED) AS Expr1
FROM Adjudicate.ONCOLOGY_DynamicDecisionValuesForCaseManager AS b
WHERE (a.DecisionValueID = DecisionValueID) AND a.DynamicAttributeID = DynamicAttributeID) ) AND (RECORDSTATUS <> 'D')
In the example shown, all the rows with DynamicAtrributeID = 937766 have different values on VALUEDATEUPDATED, so the query is showing one row for that DynamicAtrributeID because that's what you're doing, filtering and getting only the row wich has the max VALUEDATEUPDATED.
In other words, you can't get more than one row for a DynamicAtrributeID if you're filtering by his max(VALUEDATEUPDATED).
As #necoflecap1 said , the issue is your filter condition, For VALUEDATEUPDATED , you are filtering max(VALUEDATEUPDATED) which any way going to give 1 value per DynamicAttributeID and DecisionValueID combination,since your filter in subquery is
WHERE (a.DecisionValueID = DecisionValueID) AND (a.DynamicAttributeID =
DynamicAttributeID)
I can see two possibilities here , either add one more filter for VotingValue column
WHERE (a.DecisionValueID = DecisionValueID) AND (a.DynamicAttributeID =
DynamicAttributeID) and (a.VotingValue=b.VotingValue)
or Do a
group By VotingValue
inside filter subquery for max(VALUEDATEUPDATED) so that aggregation will perform for every VotingValue (which is the column I can see coming with a different value for DynamicAtrributeID 937766)

Countif query in access

I am trying to run a query that calculate with a countif function but I am having trouble with it. I have used the count and the iif functions in the builder but I think something weird is going on. I am trying to count the number of times a certain value occurs in a column so I do not want a specific value to equal to if that's possible?
Thanks!
To count the number of times a value appears you can use something like.
If you want to know how many times each value appears just omit the WHERE clause (without a sample of data I've used a table in the database I'm working on).
SELECT ProcessID,
COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
if you need just the value you can use:
SELECT COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
Another way is:
SELECT DCOUNT("ProcessID","tbl_PrimaryData_Step1","ProcessID = 4")
Edit:
In reply to your comment on your original post this SQL will give the result you're after:
SELECT Concatenate,
COUNT(Concatenate)
FROM MyTable
GROUP BY Concatenate

Edit first item of dynamic rows in ssrs?

My ssrs has three row groups nested, and in one of the rows the report runs many times.
I'd like to add something simple to the rows, such as "%" at the end, but only on the first row returned, not the rest of the dynamic rows. My idea was to use:
=RowNumber("detailsGroup") but all that returns is one for each row. Is there another SSRS method?
I was also thinking of using the "is" operator and comparing the dynamic values to the First operator, but running the report gave #ERROR.
If you are using groups you should be able to use Previous() function.
EXample:
Group 1 - ClientName
Group 2 - Region
Group 3 - SubRegion
Detail - data for above 3 groups
If I just want to add something in one of the detail data then I would use ..
=Fields!CallReason.Value + IIF(Previous(Fields!ClientName.Value) <> Fields!ClientName.Value , " Add Text For First Line", "")

Sql statement with multi ANDs querying the same column

I don't know if the title of the post is the appropriate. I have the following table
and an Array in php with some items, parsed_array. What I want to do is to find all the SupermarketIDs which have all the items of the parsed_array.
For example, if parsed_array contains [111,121,131] I want the result to be 21 which is the ID of the Supermarket that contains all these items.
I tried to do it like that:
$this->db->select('SupermarketID');
$this->db->from('productinsupermarket');
for ($i=0; $i<sizeof($parsed_array); $i++)
{
$this->db->where('ItemID', $parsed_array[$i]);
}
$query = $this->db->get();
return $query->result_array();
If there is only one item in the parsed_array the result is correct because the above is equal to
SELECT SupermarketID
FROM productinsupermarket
WHERE ItemID=parsed_array[0];
but if there are more than one items, lets say two, is equal to
SELECT SupermarketID
FROM productinsupermarket
WHERE ItemID=parsed_array[0]
AND ItemID=parsed_array[1];
which of course return an empty table. Any idea how can this be solved?
There are at least two ways of generating the result you want, either a self join (no fun to generate with a dynamic number of items) or using IN, GROUP BY and HAVING.
I can't really tell you how to generate it using CodeIgniter, I assume you're better at that than I am :)
SELECT SupermarketID
FROM productinsupermarket
WHERE ItemID IN (111,121,131) -- The 3 item id's you're looking for
GROUP BY SupermarketID
HAVING COUNT(ItemId) = 3; -- All 3 must match
An SQLfiddle to test with.
EDIT: As #ypercube mentions below, if the ItemId can show up more than once for a SupermarketID, you'll want to use COUNT(DISTINCT ItemId) to count only unique rows instead of counting every occurrence.
You can use where_in in codeigniter as below,
if(count($parsed_array) > 0)
{
$this->db->where_in('ItemID', $parsed_array);
}
Active record class in codeigniter
Try an IN clause or multiple ORs:
SELECT SupermarketID
FROM productinsupermarket
WHERE ItemID=parsed_array[0]
OR ItemID=parsed_array[1];