Lets say I have an array of Match objects, each belonging to a round in a round robin tournament structure...
Matches
Round | Registrant_ID |Registrant_ID_2 |Winner_id
1 | 1 | 2 | 2
1 | 3 | 4 | 4
1 | 5 | 6 | 5
1 | 7 | 8 | 8
2 | 1 | 4 | 1
2 | 3 | 6 | 3
2 | 5 | 8 | 5
2 | 7 | 2 | 2
3 | 1 | 6 | 1
...
What I wish to do is group all of the matches by Round and loop through that round and list the matches.
The desired output would be something like...
<h1>Round 1</h1>
<table>
<thead>
<tr>
<th>Player 1</th>
<th>Player 2</th>
<th>Winner</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>5</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>8</td>
</tr>
</tbody>
</table>
The problem I'm facing is that I don't know how to loop through the match records by their round attribute. I'm not sure if something like in_groups_of can be used because the number of players that participate in a round will vary, it won't always be 8 as seen here.
Here is my code thus far which simply loops through all records and creates a table for every match(I'm looking for tables for individual rounds):
- #matches.each do |match|
%h1= "Round #{match.round}"
%table.table.table-bordered
%thead
%tr
%th.span4 Player 1
%th.span4 Player 2
%th.span4 Winner
%tbody
%tr
%td= match.register.user.username
%td= match.register_2.user.username
%td= match.winner.user.username unless match.winner.nil?
Here's what the output is meant to look like, notice separate tables for separate rounds:
Maybe you mean to group matches by round, like this:
- #matches.group_by(&:round).each do |round, matches|
%h1= "Round #{round}"
%table.table.table-bordered
%thead
%tr
%th.span4 Player 1
%th.span4 Player 2
%th.span4 Winner
%tbody
- matches.each do |match|
%tr
%td= match.register.user.username
%td= match.register_2.user.username
%td= match.winner.user.username unless match.winner.nil?
Related
I'm trying to query a set of data using the where clause. Here's the example of the data collection:
+-----------+------------+-------------+
| Driver ID | Paid Date | Paid Amount |
+-----------+------------+-------------+
| 000001 | 2020-08-01 | 170000 |
| 000001 | 2020-08-02 | 170000 |
| 000001 | 2020-08-03 | 170000 |
| 000001 | 2020-08-04 | 170000 |
| 000002 | 2020-08-02 | 170000 |
| 000002 | 2020-08-03 | 170000 |
| 000002 | 2020-08-04 | 170000 |
| 000002 | 2020-08-05 | 170000 |
+-----------+------------+-------------+
I want to sum the Paid Amount column, group by Driver ID, and using this where clause:
[where 'paid date' between min(paid date) and dateadd(day, 3, paid date)]
The problem that I have is, I cannot use the where clause above, because the date range for each Driver ID are different.
Is there any way to solve those problem?
You can use xquery to get the data as table and then apply filters and group data accordingly.
DECLARE #xmlvalue xml =
'<table style=''border: 1px solid; background: #ffffff; font-size: 12px; text-align: center; width: 100%''>
<tr >
<th >Driver ID</th>
<th >Paid Date</th>
<th >Paid Amount</th>
</tr>
<tr>
<td >000001</td>
<td >2020-08-01</td>
<td >170000</td>
</tr>
<tr>
<td >000001</td>
<td >2020-08-02</td>
<td >170000</td>
</tr>
<tr>
<td >000001</td>
<td >2020-08-03</td>
<td >170000</td>
</tr>
<tr>
<td >000001</td>
<td >2020-08-04</td>
<td >170000</td>
</tr>
<tr>
<td >000002</td>
<td >2020-08-02</td>
<td >170000</td>
</tr>
<tr>
<td >000002</td>
<td >2020-08-03</td>
<td >170000</td>
</tr>
<tr>
<td >000002</td>
<td >2020-08-04</td>
<td >170000</td>
</tr>
<tr>
<td >000002</td>
<td >2020-08-05</td>
<td >170000</td>
</tr>
</table>'
;WITH CTE_DriverData AS
(
SELECT DriverID, PaidDate, min(PaidDate) OVER ( PARTITION BY DriverID) as minPaidDate, PaidAmount
FROM
(
SELECT
t.cols.value('./td[1]','varchar(30)') as DriverID,
t.cols.value('./td[2]','date') as PaidDate,
t.cols.value('./td[3]','int') as PaidAmount
FROM
#XMLValue.nodes('/table/tr') as t(cols)
WHERE t.cols.exist('./td') = 1
) AS x
)
SELECT DriverID, sum(PaidAmount) as PaidAmount
FROM CTE_DriverData
WHERE PaidDate between minPaidDate and DATEADD(DD,3, minPaidDate)
group by DriverID
+----------+------------+
| DriverID | PaidAmount |
+----------+------------+
| 000001 | 680000 |
| 000002 | 680000 |
+----------+------------+
I have a comments column and the comments added to release are stored as rich text in comments column. Now i'm trying to process this data and get an human readable output. I'm providing 2 sample comment data i have in my oracle SQL db which i'm trying to process.
Example 1 :
<html>
<body>
<div align="left"><font face="Arial Unicode MS"><span style="font-size:8pt">Display the frulog on the count values</span></font></div>
</body>
</html>
Example 2: <not implemented in this release>
i used the below query to process the html characters
Select (REGEXP_REPLACE(comments),'<.+?>') from test_table;
Note: consider values provided in Example 1 and Example 2 are passed in as column comments in the above SQL command.
the query result for Example 1 was Display the frulog on the count values which is what i'm expecting. result for Example 2 was ''. Value in Example 2 was not an html tag but it still replaced the tags. How can i make the replace statement smart.
Feel free to drop your suggestions .
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE comments ( value ) AS
SELECT '<html>
<body>
<div align="left">
<font face="Arial Unicode MS">
<span style="font-size:8pt">
Display the frulog on the count values
</span>
</font>
</div>
</body>
</html>' FROM DUAL UNION ALL
SELECT '<not implemented in this release>' FROM DUAL UNION ALL
SELECT '<test a="1"
b=''2''
c = 3
d
e = ">" >test</test>' FROM DUAL;
Query 1:
SELECT value,
REGEXP_REPLACE(
value,
'\s*</?\w+((\s+\w+(\s*=\s*(".*?"|''.*?''|[^''">\s]+))?)+\s*|\s*)/?>\s*',
NULL,
1,
0,
'im'
) AS replaced
FROM comments
Results:
| VALUE | REPLACED |
|------------------------------------------|----------------------------------------|
| <html> | Display the frulog on the count values |
| <body> | |
| <div align="left"> | |
| <font face="Arial Unicode MS"> | |
| <span style="font-size:8pt"> | |
| Display the frulog on the count values | |
| </span> | |
| </font> | |
| </div> | |
| </body> | |
| </html> | |
|------------------------------------------|----------------------------------------|
| <not implemented in this release> | (null) |
|------------------------------------------|----------------------------------------|
| <test a="1" | test |
| b='2' | |
| c = 3 | |
| d | |
| e = ">" >test</test> | |
Note: <not implemented in this release> is a valid HTML custom element with tag name not and attributes implemented, in, this and release.
If you only want to replace specific HTML elements then list them at the start of the regular expression:
\s*</?(a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdi|bdo|bgsound|big|blink|blockquote|body|br|button|canvas|caption|center|cite|code|col|colgroup|command|content|data|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|element|em|embed|fieldset|figcaption|figure|font|footer|form|frame|frameset|h1|head|header|hgroup|hr|html|i|iframe|image|img|input|ins|isindex|kbd|keygen|label|legend|li|link|listing|main|map|mark|marquee|menu|menuitem|meta|meter|multicol|nav|nextid|nobr|noembed|noframes|noscript|object|ol|optgroup|option|output|p|param|picture|plaintext|pre|progress|q|rp|rt|rtc|ruby|s|samp|script|section|select|shadow|slot|small|source|spacer|span|strike|strong|style|sub|summary|sup|table|tbody|td|template|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video|wbr|xmp)((\s+\w+(\s*=\s*(".*?"|''.*?''|[^''">\s]+))?)+\s*|\s*)/?>\s*
I'm having a bs3 page with 3 columns layout as below:
| 1 | 2 | 3 |
3 X 6 X 3
on large screens this is fine, however, on small screens(mobile phones), i want the order to be: 2>1>3 like below:
| 2 |
| 1 |
| 3 |
what should I use for to achieve this functionality?
Try this:
<div class="col-xs-12 col-sm-6 col-sm-push-3">2</div>
<div class="col-xs-12 col-sm-3 col-sm-pull-6">1</div>
<div class="col-xs-12 col-sm-3">3</div>
I have th following result set for my query (exluding some select statments, joins, and where clauses because I just need the general method of how to accomplish this):
Select *
From hsi.itemdata
Where hsi.itemdata.itemtypenum in ('965','502','530','336','513','506','507','514','515','516')
ItemTypeNum | ItemType | DocTypeCount<br/>
502 | Consultation Report | 4 <br/>
506 | Discharge Summary | 10 <br/>
336 | ED Nurse Notes | 2 <br/>
513 | ED Provider Notes | 8 <br/>
514 | History and Physical | 15 <br/>
I want it to show all even if it doesn't exist in the in statement with a count of '0'. Like so...
ItemTypeNum | ItemType | DocTypeCount<br/>
502 | Consultation Report | 4 <br/>
506 | Discharge Summary | 10 <br/>
336 | ED Nurse Notes | 2 <br/>
513 | ED Provider Notes | 8 <br/>
514 | History and Physical | 15 <br/>
515 | *Appropriate Value* | 0 <br/>
516 | *Appropriate Value* | 0 <br/>
530 | *Appropriate Value* | 0 <br/>
507 | *Appropriate Value* | 0 <br/>
965 | *Appropriate Value* | 0 <br/>
In practice, you would use left join. The exact syntax depends on the database. Here is a version that works in SQL Server and Postgres, for instance:
Select v.itemtypenum,
coalesce(id.ItemType, '*Appropriate Value*') as ItemType,
coalesce(DocTypeCount, '') as DocTypeCount
From (values ('965'), ('502'), ('530'), ('336'), ('513'), ('506'), ('507'), ('514'), ('515'), ('516')
) v(itemtypenum) left join
hsi.itemdata id
on id.itemtypenum = v.itemtypenum;
Note: although not all databases support values in the from clause, almost all have some mechanism for creating a table on the fly. The idea is the same, but the syntax would be a bit different.
One way is by using CASE in your select for DocTypeCount:
SELECT ...
CASE
WHEN hsi.itemdata.itemtypenum IN ('965','502','530','336','513','506','507','514','515','516')
THEN DocTypeCount
ELSE 0
END AS DocTypeCount
FROM hsi.itemdata
Of course, this may get more complicated depending on your actual query and how you're getting these columns.
I have a data cube with one hierarchical dimension "MyTime", in each level the elements are ordered. This dimension is somehow a time dimension, but does not fit 100% to gregorian calendar.
There is one cube using this dimension.
Extract of my OLAP-schema:
<Dimension name="MyTime">
<Hierarchy hasAll="true">
<Level name="MyYear" type="Numeric" uniqueMembers="true"/>
<Level name="MyMonth" type="Numeric" uniqueMembers="true"/>
<Level name="MyDay" type="Numeric" uniqueMembers="true"/>
<Level name="MyShift" type="Numeric" uniqueMembers="true"/>
</Hierarchy>
</Dimension>
<Cube name="MyCube">
<DimensionUsage name="MyTime" source="MyTime"/>
<Measure name"Price" aggregator="avg"/>
</Cube>
DB-Tables look like this:
MyTimeDim
id | myYear | myMonth | myDay | myShift | ... other fields
---+--------+---------+-------+---------+-----------------
1 | 2014 | 6 | 3 | 1 |
2 | 2014 | 6 | 3 | 2 |
3 | 2014 | 6 | 3 | 3 |
4 | 2014 | 6 | 4 | 1 |
5 | 2014 | 6 | 4 | 2 |
6 | 2014 | 6 | 4 | 3 |
MyFact
id | timeDim | price
---+---------+------
1 | 1 | 20
2 | 2 | 9
3 | 3 | 25
4 | 4 | 3
5 | 5 | 37
6 | 6 | 5
The task is, to show a hierarchical evaluation to drill down by MyTime. On each level different aggregates of the price have to be build. Easy ones are min and max. But I also have to show the first and the last member.
That means on Day-level, the result should look like this:
Date | Min | Max | First | Last
-----------+-----+-----+-------+-----
2014-06-03 | 9 | 25 | 20 | 25
2014-06-04 | 20 | 37 | 3 | 5
I think, to provide this, I have to define a calculated member. But I could not figure out, how to setup a "user defined" aggregate.
There is no First or Last aggregate in Mondrian as of now.
The only way to do it, IMO, is on query time with a calculated member like this:
MEMBER [Measures].[Last Period Measure] as ( [Measures].[My Measures], [MyTime].CurrentMember.LastChild)