Select "lowest value" from table for multiple "ids" - mysql

I read up last night on using the sql MIN() and I understand how it works now. However, I was wondering, how do I run
SELECT MIN(card_price)
FROM card_lookup_values
and display the lowest "price" for each individual card. A lot of the cards have 5-9 prices in the card_price column. I guess I was looking for:
card_id card_price
0001 2
0002 99
0003 22.3

You need a group by card_name.
Consider reading this tutorial on aggregate functions:
http://www.postgresql.org/docs/9.0/static/tutorial-agg.html

Related

how to compute percentage in mysql/sql when 2 group by conditions are present

id title count organizer
1 music 4 2
2 sports 6 2
3 music 2 3
I have a derived table with the above structure. I need to compute the percentage of the number of events of each organizer for each title. i.e. I'm expecting a result like:
organizer title percentage
2 music 40%
2 sports 60%
3 music 100%
The way I'm doing it without organizer in consideration produces the percentage aggregating all the values easily but introducing organizer messes it all up. Can anyone help me here??
Taking your derived table as the actual data (may not be optimal) you could:
select to.organizer, to.title, 100.0*to.count/o.count as percentage
from
(select organizer, sum(count) as count from derivedtable group by organizer) o
inner join
derivedtable to
on
to.organizer = o.organizer
It works by summing the data per organizer to get a total, and joining this back to the data you have so you can do the particular event-organizer count divided by the total count for that organiser
There might have been a simpler way to do it with your source data, as is you'll have to plug your query in that creates your derived table, possibly making it messy. Probably simplest to do as a CTE, but do try to include original source data and "query so far" next time you ask a question, otherwise we have to build a solution on top of some solution we know nothing about and the result might not be optimal
You can try below - using scalar subquery
DEMO
select organizer,title,count*100/(select sum(count) from tablename b where a.organizer=b.organizer)
from tablename a
OUTPUT:
organizer title percentage
2 music 40.0000
2 sports 60.0000
3 music 100.0000

MySQL Sum and Case Query

I create a ReportViewer with VB.NET connecting to a MySQL database. The data appears like below.
IdProduct Quantity TotalPrice OrderDate
0001 1 10 29/09/2014
0002 2 40 29/09/2014
0001 4 40 29/09/2014
0001 2 20 29/09/2014
0001 2 20 29/09/2014
Based on the records above, I'd like the result to appear like below
0001 0002
9 2
90 40
What is Query Sum Case the best use here? Thanks in advance.
NOTE: It's not possible for a query to "dynamically" alter the number or datatype of the columns returned, those must be specified at the time the SQL text is parsed.
To return the specified resultset with a query, you could do something like this:
SELECT SUM(IF(t.IdProduct='0001',t.Quantity,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.Quantity,NULL)) AS `0002`
FROM mytable t
UNION ALL
SELECT SUM(IF(t.IdProduct='0001',t.TotalPrice,NULL)) AS `0001`
, SUM(IF(t.IdProduct='0002',t.TotalPrice,NULL)) AS `0002`
FROM mytable t
Note that the datatypes returned by the two queries will need to be compatible. This won't be a problem if Quantity and TotalPrice are both defined as integer.
Also, there's no specific guarantee that the "Quantity" row will be before the "TotalPrice" row; we observe that behavior, and it's unlikely that it will ever be different. But, to have a guarantee, we'd need an ORDER BY clause. So, including an additional discriminator column (a literal in the SELECT list of each query), that would give us something we could ORDER BY.
Note that it's not possible to have this single query dynamically create another column for IdProduct '0003'. We'd need to add that to the SELECT list of each query.
We could do this in two steps, using a query to get the list of distinct IdProduct, and then use that to dynamically create the query we need.
BUT... with all that said... we don't want to do that.
The normative pattern would be to return Quantity and TotalPrice as two separate columns, along with the IdProduct as another column. For example, the result returned by this statement:
SELECT t.IdProduct
, SUM(t.Quantity) AS `Quantity`
, SUM(t.TotalPrice) AS `TotalPrice`
FROM mytable t
GROUP BY t.IdProduct
And then the client application would be responsible for transforming that resultset into the desired display representation.
We don't want to push that job (of transforming the result into a display representation) into the SQL.
select idproduct, sum(quantity), sum(totalprice)
from your_table
group by idproduct

customizing the sort rows in a report

I'm trying to totally customize my report rows. Say I have a matrix report like this:
Category Smith Jones Johnson
Overhead 1,230.00 34.00 56.01
FTE (23.00) 105.00 2,389.00
RVU 3.00 787.00 89.00
Salary 44.00 782.00 9.00
Subsidy 11.00 4,561.00 389.00
TNS 4635.00 55.00 45.00
Is it possible to create a totally custom sort order? I would like to move RVU on top, then Salary, then TNS, etc. I found if I go into the Group Properties, I can define an expression like:
=iif(Fields!City.Value=”RVU”,”1″, iif(Fields!City.Value=”Salary”,”2″, iif(Fields!City.Value=”TNS”,”3″,”"...etc.
But that's very restrictive, and if I later want to move a row up or down, I have to go in and change all the numbers. What I'm looking for is a drag and drop approach that would let me change the order easily, and at any time.
Is this possible?
I would recommend adding a sort order to your dataset, it is the easiest to maintain, maybe a case sentence in your sql query,
Select Case when [Category]='RVU' then 1
when [Category]='Salary' then 2 end as Sortorder, [other], [columns]
then you can order by the sort order column
I can't think of an easy drag and drop solution to this problem

RowNumber for group in SSRS 2005

I have a table in a SSRS report that is displaying only a group, not the table details. I want to find out the row number for the items that are being displayed so that I can use color banding. I tried using "Rowcount(Nothing)", but instead I get the row number of the detail table.
My underlying data is something like
ROwId Team Fan
1 Yankees John
2 Yankees Russ
3 Red Socks Mark
4 Red Socks Mary
...
8 Orioles Elliot
...
29 Dodgers Jim
...
43 Giants Harry
My table showing only the groups looks like this:
ROwId Team
2 Yankees
3 Red Socks
8 Orioles
29 Dodgers
43 Giants
I want it to look like
ROwId Team
1 Yankees
2 Red Socks
3 Orioles
4 Dodgers
5 Giants
You can do this with a RunningValue expression, something like:
=RunningValue(Fields!Team.Value, CountDistinct, "DataSet1")
DataSet1 being the name of the underlying dataset.
Consider the data:
Creating a simple report and comparing the RowNumber and RunningValue approaches shows that RunningValue gives your required results:
You can easily achieve this with a little bit of vbcode. Go to Report - Properties - code and type something like:
Dim rownumber = 0
Function writeRow()
rownumber = rownumber + 1
return rownumber
End Function
Then on your cell, call this function by using =Code.writeRow()
As soon as you start using groups inside the tables, the RowNumber and RunningGroup functions start getting some weird behaviours, thus it's easier to just write a bit of code to do what you want.
I am not convinced all suggestions above provide are a one for all solution. My scenario is I have a grouping that has has multiple columns. I could not use the agreed solution RunningValue because I don't have a single column to use in the function unless I combine (say a computed column) them all to make single unique column.
I could not use the VBA code function as is for the same reason and I had to use the same value across multiple columns and multiple properties for that matter unless I use some other kind of smarts where if I knew the number of uses (say N columns * M properties) then I could only update the RowNumber on every NxM calls however, I could not see any count columns function so if I added a column I would also need to increase my N constant. I also did not want to add a new column as also suggested to my grouping as I could not figure out how to hide it and I could not write a vba system where I could call function A that returns nothing but updates the value (i.e. called only once per group row) then call another function GetRowNumber which simply returns the rownumber variable because the colouring was done before the call so I always had one column out of sync to the rest.
My only other 2 solutions I could think of is put the combined column as mentioned earlier in the query itself or use DENSE_RANK and sort on all group columns, i.e.
DENSE_RANK() OVER (ORDER BY GroupCol1, GroupCol2, ...) AS RowNumber

mysql combining records from one table

I have a single table that uses test# as the primary key. Here is what that table looks like:
Test# Name VerbalScore readingScore Notes
1 Bobby 92 Good job
2 Bobby 40 You Suck Bobby
The problem is I want to view and be able to see when there are multiple verbal scores for the same Name (so be able to see if the person took the same test more than once).
I want to have some kind of select statement to get this result from the above table:
1 Bobby 92 40 Good job, You Suck Bobby
Is that possible?
I am not totally sure I understand what you mean by "see when there are multiple verbal scores" but with mysql 5+, try
SELECT
Name,
GROUP_CONCAT(VerbalScore),
GROUP_CONCAT(readingScore),
GROUP_CONCAT(Notes)
FROM
myTable
GROUP BY
Name;
GROUP_CONCAT is a mysql specific grouping function.