Changing multiple row colours in Report Builder - sql

I would like to change the row colours in the attached image so all rows match with the value in NAME column.
So I want the related 2 rows in CUST CODE, DEL TO, CUST NAME and then the related 3 rows in ORDER NO to be the same colour.
Then i would like the colours to alternate for the next NAME value and so on.
Is this possible? I know how to alternate row colour when each result is one row only, but unsure how to do it with this kind of result.
So to clarify, for NAME (AHe) all rows to be 'lightgrey', NAME (AHO) all rows to be 'whitesmoke', NAME (JH) all rows to be 'lightgrey' etc...

I've used something similar to the following in the background color expression:
=IIF(RUNNINGVALUE(Fields!Name.Value, COUNTDISTINCT,"MainDataSet") MOD 2 = 0,
"LightGrey",
"WhiteSmoke")
There may be another way to do this using ROWNUMBER() or another alternative as well.

Related

Postgresql: counting distinct changes within a json column across different records with the same reference ID

Is it possible to do this in postgresql?
Here's an example of what my data looks like:
I want to be able to return the number of times this customer, A1, changed their carb selection (should return 1, since they changed it once) or changed their vegetable selection (answer should be 0)
Is it possible to query for this? Thank you so much!

Grab Random Record and Mark As Being Used

Alright, so I have a table called Colors, within that table I have 5 records (Red, Blue, Green, Yellow and Orange). The color table currently has two fields (ID and Color Name). My overall goal is to randomly select a color and mark this color as being used. Rinse and repeat until all colors are used and them mark all colors as being unused.
Here is the SQL on the RandomColorsQuery:
SELECT TOP 1 Colors.[Color Name]
FROM Colors
ORDER BY Rnd(ColorID);
So far, I've been able to select a random color by using the following within VBA and works fine:
Dim RanColor As DAO.Recordset
Set RanColor = CurrentDb.OpenRecordset("RandomColorsQuery")
'MsgBox (RanColor.Fields(0))
Text1.SetFocus
Text1.Text = RanColor.Fields(0)
Obviously I would need to add a new field to the Colors table, say a field called "Used". I'd rather not use a Yes/No field and just add an "X" in the "Used" field when the color is used.
Any suggestions or similar examples on how to accomplish this?
Add a boolean (Yes/No) field.
Modify query to return only those that haven't been used:
SELECT TOP 1 Colors.[Color Name]
FROM Colors
WHERE USED=False
ORDER BY Rnd(ColorID);
In VBA, if the recordset returns a record, grab your color value, and set Used field to True.
If recordset returns doesn't return a record, ie rs.EOF=True, then update Used field in all records to false, and rerun the query to start over.

Solution for allowing user sorting in SQlite

By user sorting I mean that as a user on the site you see a bunch of items, and you are supposed to be able to reorder them (I'm using jQuery UI).
The user only sees 20 items on each page, but the total number of items can be thousands.
I assume I need to add another column in the table for custom ordering.
If the user sees items from 41-60, and and he sorts them like:
41 = 2nd
42 = 1st
43 = fifth
etc.
I can't just set the ordering column to 2,1,5.
I would need to go through the entire table and change each record.
Is there any way to avoid this and somehow sort only the current selection?
Add another column to store the custom order, just as you suggested yourself. You can avoid the problem of having to reassign all rows' values by using a REAL-typed column: For new rows, you still use an increasing integer sequence for the column's value. But if a user reorders a row, the decimal data type will allow you to use the formula ½ (previous row's value + next row's value) to update the column of the single row that was moved. You
have got two special cases to take care of, namely if a user moves a row to the very beginning or end of the list. In that case, just use min - 1 rsp. max + 1.
This approach is the simplest I can think of, but it also has some downsides. First, it has a theoretical limitation due to the datatype having only double-precision. After a finite number of reorderings, the values are too close together for their average to be a different number. But that's really only a theoretical limit you should never reach in practical applications. Also, the column will use 8 bytes of memory per row, which probably is much more than you actually need.
If your application might scale to the point where those 8 bytes matter or where you might have users that overeagerly reorder rows, you should instead stick to the INTEGER column and use multiples of a constant number as the default values (e.g. 100, 200, 300, ..). You still use the update formula from above, but whenever two values become too close together, you reassign all values. By tweaking the constant multiplier to the average table size / user behaviour, you can control how often this expensive operation has to be done.
There are a couple ways I can think of to do this. One would be to use a SELECT FROM SELECT style statement. As in something like this.
SELECT *
FROM (
SELECT col1, col2, col3...
FROM ...
WHERE ...
LIMIT n,m
) as Table_A
ORDER BY ...
The second option would be to use temp tables such as:
INSERT INTO temp_table_A SELECT ... FROM ... WHERE ... LIMIT n,m;
SELECT * FROM temp_table_A ORDER BY ...
Another option to look at would be jQuery plugin like DataTables
one way i can think of is:
Add a new column (if feasible) or create a new table for holding the order of the items.
On any page you will show around 20 items based on the initial ordering.
Using the jquery's Draggable you can send updates to this table
I think you can do this with an extra column.
First, you could prepopulate this new column with a default sort order and then allow the user to interactively modify it with the drag and drop of jquery-ui.
Lets say this user has 100 items in the table. You set the values in the order column to [1,2,3,...,99,100]. I suggest that you run a script on the original table to set all items to a default sort order.
Now going back to your example where the user is presented with items 41-60: the initial presentation in their browser would rank those at orders [41,42,43,...,59,60]. You might also need to save the lowest order that appears in this subset, in this case 41. Or better yet, save the entire array of rankings and restore the exact same numbers in the new order. This covers the case where they select a set of records that are not already consecutively ordered, perhaps because they belong to someone else.
To demonstrate what I mean: when they reorder them in the page, your javascript reassigns those same numbers back to the subset in the new order. Like this:
item A : 41
item B : 45
item C : 46
item D : 47
item E : 51
item F : 54
item G : 57
then the user changes them to this order, but you reassign the numbers like this:
item D : 41
item F : 45
item E : 46
item A : 47
item C : 51
item B : 54
item G : 57
This should also work if the subset is consecutive.

SQL Select statement to find a unique entry based on many attributes

To put this work in context... I'm trying to filter a database of objects and build descriptions which can be verbalized for a speech UI. To minimise the descriptions I want to find the shortest way to describe an object, based on the idea of Grices Maxims.
It's possible in code by iterating through the records, and running through all permutations, but I keep thinking there ought to be a way to do this in SQL... so far I haven't found it. (I'm using PostGRES.)
So I have a table that looks something like this:
id colour position height
(int) (text) (text) (int)
0 "red" "left" 9
1 "red" "middle" 8
2 "blue" "middle" 8
3 "blue" "middle" 9
4 "red" "left" 7
There are two things I wish to find based on the attributes (excluding the ID).
a) are any of the records unique, based on the minimum number of attributes?
=> e.g. record 0 is unique based on colour and height
=> e.g. record 1 is the only red item in the middle
=> e.g. record 4 is unique as its the only one which has a height of 7
b) how is a particular record unique?
=> e.g. how is record 0 unique? because it is the only item with a colour red, and height of 9
=> e.g. record 4 is unique because it is the only item with a height of 7
It may of course be that no objects are unique based on the attributes which is fine.
+++++++++++++++++++++++++
Answer for (a)
So the only way I can think to do this in SQL is to start off by testing a single attribute to see if there is a single match from all records. If not then add attribute 2 and test again. Then try attributes 1 and 3. Finally try attributes 1,2 and 3.
Something like this:-
single column test:
select * from griceanmaxims
where height=(Select height from griceanmaxims
group by height
having (count(height)=1))
or
relpos=
(Select relpos
from griceanmaxims
group by relpos
having (count(relpos)=1))
or
colour=
(Select colour
from griceanmaxims
group by colour
having (count(colour)=1))
double column tests:
(Select colour,relpos
from griceanmaxims
group by colour,relpos
having (count(colour)=1))
(Select colour,height
from griceanmaxims
group by colour,height
having (count(colour)=1))
etc
++++++++
I'm not sure if there's a better way or how to join up the results from the double column tests.
Also if anyone has any suggestions on how to determine the distinguishing factors for a record (as in question b), that would be great. My guess is that (b) would require (a) to be run for all of the field combinations, but I'm not sure if there's a better way.
Thanks in advance for any help on this one....
I like the idea of attacking the problem using a General Purpose Language eg C#:
1) Iterate through and see if any have 1 attribute which is unique eg ID = 4, which is unique because height is 7. Take ID 4 out of the 'doing' collection, and put into 'done' collection with appropriate attribute
Use a unit testing tool eg MSUNIT to prove the above works
2) Try and extend to n attibutes
Unit Test
3) See if any can be unique with 2 attributes. Take those IDs out of doing and into done with the pairs of attributes
Unit Test
4) Extend to m attributes
Unit Test
3) Refactor maybe using recursion
Hope this helps.

Using MDX, how to get only a few selected rows?

I have a Color dimension with many colors, but I want to show a table with just two rows (black and red). I tried this:
SELECT [Color].[black] || [Color].[red] ON ROWS,
{[Measures].defaultMember} ON COLUMNS
from [SalesAnalysis]
The result I was expecting was a table with one column and two rows. One cell for black sales, one cell for red sales. An error comes instead.
What MDX request should I write?
I also tried things called "aggregate" and "filter", but it seems that they are not what I am looking for.
OK, I have found:
SELECT {[Color].[black],[Color].[red]} ON ROWS,
{[Measures].defaultMember} ON COLUMNS
from [SalesAnalysis]
Or try something like this:
SELECT {[Color]} ON ROWS,
{[Measures].defaultMember} ON COLUMNS
FROM [SalesAnalysis]
WHERE {[Color].[black], [Color].[red]}