SQL - return rows with a duplicate value, where one row in each group has a certain attribute and not another - sql

I am a SQL novice at best - appreciate any help on this answer. It's specific to the program MediaMonkey, but more broadly applicable I think since MediaMonkey's database is I believe SQLite 3. The table I'm querying is called Songs and all the columns I'm referencing are in this table.
I am using the Custom4 field in MediaMonkey to store a song's Work ID from Musicbrainz, and the Custom5 field to store the AcoustID. I'm trying to create a playlist of cover / alternate / live versions of songs that I've rated 5 stars, but where the 5 star song is not a classical work or a jazz recording (I use the Grouping field (GroupDesc) to indicate if a track is classical or jazz).
Parameters for the SQL query need to be something like this:
Same Work ID as a 5 star recording (Rating = 100), where the Grouping
of the 5 star recording is not 'Classical' or 'Jazz' (I am OK with
classical or jazz covers of 5 star popular works)
Different AcoustID from any 5 star recording (eliminate duplicate
recordings)
Rating < 5 stars (I don't want to include the 5 star songs
themselves)
I cobbled together the following SQL query from some examples I found:
Songs.Custom4 IN (SELECT Custom4 FROM Songs GROUP BY Custom4 HAVING Count(*) > 1 AND Max(Rating) > 99 AND Custom4 <> '' AND GroupDesc <> 'Classical' AND GroupDesc <> 'Jazz')
What this appears to include is the following:
Same WorkID as a 5 star recording
Classical or jazz tracks only if the classical or jazz track is rated
5 stars and there is another track with the same Work ID that is not
classical or jazz, or if the classical or jazz track has the same
Work ID as a 5 star recording that is not classical or jazz
Includes duplicates of the 5 star recording, and includes the 5 star
recordings themselves
Somehow then I need to filter out the 5 star classical or jazz tracks and the tracks that have the same Work IDs as those tracks, and I need to exclude the duplicates and the 5 star recordings.
Let's say you have a table like this:
<table class="blueTable">
<thead>
<tr>
<th> Song</th>
<th>Grouping</th>
<th>Rating</th>
<th>Work ID</th>
<th>AcoustID</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td>Classical </td>
<td>100 </td>
<td>a12 </td>
<td>b12 </td>
</tr>
<tr>
<td>A2</td>
<td>Jazz </td>
<td>100 </td>
<td>a21 </td>
<td>b21 </td>
</tr>
<tr>
<td>A3</td>
<td> NULL</td>
<td>100 </td>
<td>a31 </td>
<td>b31 </td>
</tr>
<tr>
<td>A4</td>
<td>NULL </td>
<td>NULL </td>
<td>a12 </td>
<td>b41 </td>
</tr>
<tr>
<td>A5</td>
<td>NULL </td>
<td>NULL </td>
<td>a31 </td>
<td>b31 </td>
</tr>
<tr>
<td>A6</td>
<td>Classical </td>
<td>NULL </td>
<td>a31 </td>
<td>b61 </td>
</tr>
<tr>
<td>A7</td>
<td>NULL </td>
<td>NULL </td>
<td>a31 </td>
<td>b71 </td>
</tr>
</tbody>
</table>
I'd want my query to include the following songs: A6, A7
Here's the logic: A1 and A2 would be excluded since they are Classical and Jazz tracks and are not covers of a popular song with a rating of 100. A3 would be excluded since it has a rating of 100. A4 would be excluded since it's a popular cover of a Classical track that is rated 100. A5 would be excluded since it's a duplicate of A3. A6 is included - it's a Classical cover of A3. A7 is included - it's a cover of A3.

Related

Pivot table with counts - Oracle SQL

I have this table:
table1
id e_nm val count
2572 Fruit Date 20180115 13
2572 Fruit Date 20180504 21
2573 Salad Date ABC 50
2574 Test Date 20181115 14
2574 Test Date 19991001 29
This table has all the distinct values present for each e_nm (element names) and their counts. This table has thousands of values available with more than 500 element names.
Is there a way I am able to visualise it like the following using Pivot:
id_2572 id_2572_e_nm id_2573 id_2573_e_nm id_2574 id_2574_e_nm
20180115 Fruit Date ABC Salad Date 20181115 Test Date
20180504 Fruit Date 19991001 Test Date
Please note the table column needs to be generated dynamically by reading id from table1.
Although not exactly as you pictured it, I have come up with a solution. Basically you will need to create a new column in the source table named unique id. This is the unique identifier under which you will be pivoting the table.
After that, you will need to use the following query to get the nearly desired output:
SELECT * FROM
(
SELECT
unique_id,
e_nm,
val
FROM table1
)
PIVOT
(
listagg(val) within group (order by null) as id, listagg(e_nm) within group (order by null) as id_e_nm
FOR e_nm IN ('Fruit Date','Salad Date','Test Date')
)
This will give you the following output:
body {
margin: 0;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
line-height: 20px;
color: #333;
background-color: #fff;
}
<table class="results table table-bordered table-striped">
<tbody>
<tr>
<th>UNIQUE_ID</th>
<th>'Fruit Date'_ID</th>
<th>'Fruit Date'_ID_E_NM</th>
<th>'Salad Date'_ID</th>
<th>'Salad Date'_ID_E_NM</th>
<th>'Test Date'_ID</th>
<th>'Test Date'_ID_E_NM</th>
</tr>
<tr>
<td>1</td>
<td>20180115</td>
<td>Fruit Date</td>
<td>ABC</td>
<td>Salad Date</td>
<td>20181115</td>
<td>Test Date</td>
</tr>
<tr>
<td>2</td>
<td>20180504</td>
<td>Fruit Date</td>
<td>(null)</td>
<td>(null)</td>
<td>19991001</td>
<td>Test Date</td>
</tr>
</tbody>
</table>
Here is the link to the SQL Fiddle query I created to answer your question.
Hope this points you to the right direction :)

Is there a way to colspan a table header with GitHub flavored markdown?

HTML allows extending a table's header row across multiple columns using colspan:
<table>
<tr><th colspan=2>Logical Operators</th></tr>
<tr><td>&&</td><td>Logical and</td></tr>
<tr><td>||</td><td>Logical or</td></tr>
<tr><td>!</td><td>Logical not</td></tr>
<tr><td>? :</td><td>Logical ternary</td></tr>
</table>
On GitHub, when this HTML is rendered in a readme.md file it looks like this:
...but using markdown syntax to create a table, I can't span the table's header row across columns, e.g. I can only split up the header text:
| Logical | Operators |
|:---:| --- |
| `&&` | Logical and |
| `\|\|` | Logical or |
| `!` | Logical not |
| `? :` | Logical ternary |
...and rendering the GFM table on GitHub looks like:
I tried emulating this solution for using colspan in the table's data rows, but I could not get it to work with the header row. Is there a way to span the GFM table's header row across more than one column with GitHub flavored Markdown?
I've posted the question to the folks at the GH MD repo.
I did it this way in order to generate a table with 5 columns with headers spanning respectively over 3 and 2 columns:
<table>
<tr>
<td colspan=3>a <td colspan=2>b
<tr>
<td colspan=1>col1 <td colspan=1>col2 <td colspan=1>col3<td colspan=1>col4 <td colspan=1>col5
</table>
My two cents.

How to locate a name using xpath out of three items?

I've written an xpath to locate a name from th tag but the thing is there are three items attached within a "th" tag separated by "br" tag. How to locate only the first item which is in this case the name among three? Here is what I tried with:
//td[#class='data']/table//th
Elements within the items are:
<td class="data" valign="top" nowrap="">
<table width="350">
<tbody><tr>
<td valign="top" nowrap="">Owner Name &<br>Mailing Address:</td>
<th align="left" valign="top" nowrap="">
<!-- ---------- OWNER NAME ---------- -->
PADILLA ISAI & JOSEFINA<br>
<!-- ---------- MAILING ADDRESS ---------- -->
<!-- ---------- MAILING ADDRESS (MAIL TO) ---------- -->
<!-- ---------- MAILING ADDRESS (ADDR1 AND ADDR2) ---------- -->
8227 FINDLAY ST<br>
<!--
------------------------------------------------
'RA 09/11/2012:
Changed order of Owner / Address to :
Owner Name(s)
MailTo
Addr_1
Addr_2
City
State
Zip
Country
------------------------------------------------
8227 FINDLAY ST
------------------------------------------------
-->
<!-- ---------- MAILING ADDRESS (CITY-STATE-ZIP OR COUNTRY)---------- -->
HOUSTON TX 77017-2328<br>
<!--<br />-->
</th>
</tr>
</tbody></table>
</td>
With my expression the result I'm getting is:
DURAN FRANCISCO & AURORA
4010 BALMORHEA AVE
HOUSTON TX 77039-2510
But I expect to get only:
DURAN FRANCISCO & AURORA
In general cases I would probably go for:
//td[#class='data']/table//th/text()[1]
But, selenium doesn't support text indexing either. What can I do to get the name only from the th tag?
Try below code and let me know in case it won't return expected result:
element = driver.find_element_by_xpath("//td[#class='data']/table//th")
print(driver.execute_script("return arguments[0].childNodes[2].textContent", element).strip())
I have the following way to execute the xpath which doesn't supported by selenium directly :
JavascriptExecutor js = (JavascriptExecutor)driver;
Object message = js.executeScript("var value = document.evaluate(\"//td[#class='data']/table//th/text()[1]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");
System.out.println(message.toString());
Still having issue let me know :)
Get more details from this answer

how to get the index value from the table?

I'm trying to get the index value for the column that I create index. How to do this? What I mean is something like below:-
index value indexed field data
______________ ____________________
0 apple
1 orange
2 manggo
Use $index.
More info: http://www.youtube.com/watch?v=jEpbjve5iHk
...
<tbody>
<tr ng-repeat="fruit in fruitBasket">
<td>{{$index}}</td> <!-- --------See this line! -->
<td>{{fruit}}</td>
</tr>
</tbody>
...

How to write sql query to select records from this table

In my program I have generated a table like:
id Roll Subject Marks LetterGrade GradePoint
1 1 Physics 40 D 1
2 1 Chemistry 50 C 2
3 1 Mathematics 60 B 3
4 2 Physics 40 D 1
5 2 Chemistry 50 C 2
6 2 Biology 59 C 2
I am using VB6 and MS Access as database. Now I want to show in Datagrid like:
Roll Sub Marks LetterG Gp Sub Marks LetterG Gp Sub Marks LetterG Gp GPA GPAExcl
1 Physics 40 D 1 Chemistry 50 C 2 Mathematics 60 B 3 2.00 1.5
2 Physics 40 D 1 Chemistry 50 C 2 Biology 59 C 2 1.5 1.5
and more rows for more Roll
The subject with the greatest id for each roll is additional subject. If its Gp is greater than 2 then it is added with GPA, GPAExcl is only with first two subject
So for Roll 1 GPA is (1+2+1)/2=2.00
(Gp in Math is greater then 2 and 3-2=1)
and GPAExcl is (1+2)/2=1.5
and for Roll 2 both GPA and GPAExcl=1.5
How to write the sql query so I can get the records like this and can show it in datagrid
Is it possible to write such a query?
It seems to me that you need to use a DataList rather than a DataGrid. The DataList allows you to specify RepeatColumns and RepeatDirection.
In this case, it appears that you would want the RepeatDirection to be vertical. Then you just figure out how many columns you want.
I'm not sure you need the SQL to be any different. This seems to be a format issue--not a query issue.
OK, I think I was looking at that wrong. Sorry about that. I see now that you're wanting the Role for all three courses on the left, then all courses for that role on the right.
There are probably several ways to deal with this. One way would be to use two repeaters. This will create the table (you'll need to add the header row in). If you don't have the same amount of courses per role, you'll have to take care of columns via columnspan.
<asp:Repeater ID="repRoles" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Role") %></td>
<asp:Repeater id="repCourses" runat="server">
<ItemTemplate>
<td>
<%# Eval("Subject") %>
</td>
<td>
<%# Eval("Marks") %>
</td>
<td>
<%# Eval("LetterGrade") %>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
You can use the ItemDataBound event on the first repeater to then bind the appropriate courses to the second repeater.