Musicbrainz SQL query to get tracks for release_group - postgresql

I am using the musicbrainz.org postgresql database, which I have installed locally and have accessed via python.
The database is a list of music artists and associated criteria. Here is the schema :
How can I create a SQL query that outputs all tracks for a release group? I am able to get the proper artist info and releases associated with a specific band but the tracks for the release are wrong using the below query's:
strsql_band = "SELECT artist.id, artist.gid, artist.name, artist.comment FROM artist WHERE artist.name=%s AND type>1 ORDER BY type, artist.last_updated DESC"
strsql_memberid = "SELECT entity0, link.begin_date_year, link.end_date_year FROM l_artist_artist l JOIN link ON l.link=link.id WHERE entity1=%s AND link_type=103"
strsql_release = "SELECT id, release_group.name FROM release_group WHERE artist_credit=%s"
# This does not return the correct tracks for the release
strsql_track = "SELECT id, position, name, length FROM track WHERE artist_credit=%s LIMIT 15"
Any help would be greatly appreciated, I have been banging my head on this for hours now.

medium and track table will give you this information
Here is the query that worked for my case:
SELECT
array_agg(track.name) as track_list,
release.gid as release_gid,
release.release_group as release_group_id
FROM track
INNER JOIN medium
ON track.medium=medium.id
INNER JOIN release
ON medium.release=release.id
GROUP BY release.gid, release.release_group limit 5;
Sample Results
release_gid release_group track_list
00000363-74c9-4c3b-bef7-a433eb9687e3 950559 {"Small Screen",Alice,Dauntless,Lullaby,"Grinding the Mill","Four Colors","2000 Gods","This Life","More Than Meets the Eye","The World"}
00000ba0-d5e2-41d7-9c5d-48e8eb04226a 388941 {"Ai, Vanna","Dod māmiņa, kam dodama","Mēs sūdzēsimies Strazdbūrā","Ar Eiropu plecos","Mīļais mans (Oi ļuļi, oi ļuļi)","Tu tik lūr, monamūr","Kocēni, Dikļi un Tožas","Sev augstu laimi vēlēju","Happy mērsrags","Tur es dzēru, tur man tika","Pritonā pie eglītes","Kur ir manas dāvaniņas","Iespraud svecīti","Ints un Ģirts","Mazs putniņš kājām... (Vai Rīga gatava?)","Melngalvju blūzs",Siergriezējs,"Zeva dziesma","Kentaura skaistumkopšanas salonā","Es sakūru uguntiņu (Gaisā skrēja)","Tu nesit... (Nestandarta žoklis)"}
00000ff3-8918-400f-89d5-ec80f52465bf 530779 {"SING IT","Precious Days",トキメキ}
0000172d-6e43-4d7d-8647-da718593a97a 2382060 {"J'ai besoin d'un doliprane","Cœur brisé","Mon proprio","J'me demande","Extra extra terrestre","Bienvenue chez moi (dans ma coloc)","L'amour c'est du pipeau","La caissière du casino",Jean-Marc,"Toutes des salopes"}

Related

Add select COUNT events in common between the current user and the others in DQL query

I have this DQL query in Symfony 4 project, it does select all users who are registered in all the same events as current user
$query = $this->_em->createQuery(
"select u as user, er.dateRegistration from App:EventRegistration er
left join App:User u with er.user = u.id
where er.user != :user_id and
er.event in (select identity(er2.event) from App:EventRegistration er2
where er2.user = :user_id)")
->setParameter('user_id', $user->getId());
return $query->getResult();
Now, in the same query, I'd like to select COUNT how many times each one of the other users has registered in the same event of the current user.
I mean the number of events in common between the current user and the others.
if I add count(er.id) to the select
select u as user, er.dateRegistration, count(er.id)
I get this error :
SQLSTATE[42803]: Grouping error: 7 ERREUR: la colonne « u0_.username » doit apparaître dans la clause GROUP BY ou doit être utilisé dans une fonction d'agrégat
LINE 1: SELECT u0_.username AS username_0, u0_.username_canonical AS..
You need to add group by;
$query = $this->_em->createQuery(
"select u as user, count(er.id) as register_count from App:EventRegistration er
left join App:User u with er.user = u.id
where er.user != :user_id and
er.event in (select identity(er2.event) from App:EventRegistration er2
where er2.user = :user_id) group by u.id")
->setParameter('user_id', $user->getId());
return $query->getResult();
It will give you register count of each user but in this case you can not expose the "er.dateRegistration" because now you are grouping counts for every user.

Trying to isolate the hours given a date range (YYYY:MM:DD HH:MM:SS) in SQL and group them. by specific hour intervals regardless of the date

I am struggling trying to extract information out of the database I created in SQL. The views work great and all data is displayed but I am trying to isolate the following:
Isolate time frames from 07:00:00 to 09:00:00.
Still new to coding, so help is appreciated.
SELECT ch.name,
t.date,
t.amount,
t.card AS "Credit Card",
t.id_merchant,
m.name AS "Merchant",
mc.name AS "merchant category"
FROM transaction AS t
JOIN credit_card AS cc
ON (t.card = cc.card)
JOIN card_holder AS ch
ON (cc.cardholder_id = ch.id)
JOIN merchant AS m
ON (t.id_merchant = m.id
JOIN merchant_category AS mc
ON (m.id_merchant_category = mc.id);

save diferent outputs in array from select

I have query with output where first 4 elements would be the same and the others different. I want to save these elements in array.
for example I have output:
where sid,name,time_from, time_to are the same values and id,type,count,from, to have different values.
I have query:
select asch.id as sid,
asch.name,
asch.time_from,
asch.time_to,
array_agg(ad.id) as ad_id,
array_agg(ad.type) as ad_type,
array_agg(ad.count)as ad_count,
array_agg(ad.from)as ad_from,array_agg(ad.to) as ad_to
from ad ad
join ad_ad_group aag on aag.ad_id=ad.id
join ad_group ag on ag.id=aag.ad_group_id
join ad_schedule asch on asch.ad_group_id=ag.id
join ad_shedule_device asd on asd.ad_schedules_id=asch.id
join device d on d.device_id=asd.devices_id
where d.device_id=4
group by asch.id
where depends on device_id Im selecting all ads on this device divide by ad shedule in which ad is located.
which output is:
but I want something like this:
Im really sorry for my bad English.
select asch.id as sid,
asch.name,
asch.time_from,
asch.time_to,
jsonb_agg(jsonb_build_object(
'ad_id'::text, ad.id,
'ad_type'::text, ad.type,
'ad_count'::text, ad.count,
'ad_from'::text, ad.from,
'ad_to'::text,ad.to
))
from ad ad
join ad_ad_group aag on aag.ad_id=ad.id
join ad_group ag on ag.id=aag.ad_group_id
join ad_schedule asch on asch.ad_group_id=ag.id
join ad_shedule_device asd on asd.ad_schedules_id=asch.id
join device d on d.device_id=asd.devices_id
where d.device_id=4
group by asch.id

SELECT attribute with MIN value from set of attribute instances that are part of specific row?

I know my question is probably very vague and hard to understand at first glance, and i've sat 30 minutes thinking about a proper title. However my database knowledge is very limited so I have a hard time formulating myself properly yet.
It is part of a school assignment I'm currently doing, where the following is what I'm trying to achieve:
and an ER diagram I made of the system:
What I'm trying to accomplish is, selecting the quantity of the component within the computer_system that has the lowest quantity (current stock) so that I in the dataset I am printing out, am able to state exactly how many of each computer_system the store is able to sell, based on the lowest current quantity of any component the computer_system consists of.
This is the query that i am currently working with to accomplish it, but I've had multiple problems with quantity being ambiguous and other errors every time I try to make a fix. I have consulted a dozen of friends from class, but without luck.
SELECT
computer_system.NAME,
cpu.name as cpu,
gpu.name as gpu,
board.name as mainboard,
pccase.name as pc_case,
ram.name as ram,
component.quantity as qty,
(cpu.price *1.3+ board.price*1.3 + pccase.price*1.3 + ram.price*1.3 + gpu.price*1.3) as computer_system_price
FROM computer_system, component
join component cpu on cpu.id = computer_system.cpu
join component gpu on gpu.id = computer_system.gpu
join component board on board.id = computer_system.mainboard
join component pccase on pccase.id = computer_system.pc_case
join component ram on ram.id = computer_system.ram
JOIN component qty ON qty.quantity = (SELECT MIN(component.quantity) FROM component WHERE component.id IN
(computer_system.pc_case,
computer_system.mainboard,
computer_system.cpu,
computer_system.gpu,
computer_system.ram))
The following code fixed it for me:
SELECT computer_system.name AS "System name",
cpu.name AS "CPU",
gpu.name AS "GPU",
pc_case.name AS "Case",
mainboard.name AS "Mainboard",
ram.name AS "RAM",
FLOOR((cpu.price + mainboard.price + pc_case.price + ram.price + coalesce(gpu.price, 0))*1.3/100)*100+99 AS "System price",
SYSTEM.maxamount
FROM computer_system
join (SELECT name,
id,
price
FROM component) AS cpu
ON cpu.id = computer_system.cpu
left join (SELECT name,
id,
price
FROM component) AS gpu
ON coalesce(gpu.id,0) = computer_system.gpu
join (SELECT name,
id,
price
FROM component) AS pc_case
ON pc_case.id = computer_system.pc_case
join (SELECT name,
id,
price
FROM component) AS mainboard
ON mainboard.id = computer_system.mainboard
join (SELECT name,
id,
price
FROM component) AS ram
ON ram.id = computer_system.ram
join (SELECT computer_system.name,
Min(component.quantity) AS maxamount
FROM computer_system,
component
WHERE computer_system.cpu = component.id
OR computer_system.gpu = component.id
OR computer_system.mainboard = component.id
OR computer_system.pc_case = component.id
OR computer_system.ram = component.id
GROUP BY computer_system.name) AS SYSTEM
ON SYSTEM.name = computer_system.name;

How to determine the size of a Full-Text Index on SQL Server 2008 R2?

I have a SQL 2008 R2 database with some tables on it having some of those tables a Full-Text Index defined. I'd like to know how to determine the size of the index of a specific table, in order to control and predict it's growth.
Is there a way of doing this?
The catalog view sys.fulltext_index_fragments keeps track of the size of each fragment, regardless of catalog, so you can take the SUM this way. This assumes the limitation of one full-text index per table is going to remain the case. The following query will get you the size of each full-text index in the database, again regardless of catalog, but you could use the WHERE clause if you only care about a specific table.
SELECT
[table] = OBJECT_SCHEMA_NAME(table_id) + '.' + OBJECT_NAME(table_id),
size_in_KB = CONVERT(DECIMAL(12,2), SUM(data_size/1024.0))
FROM sys.fulltext_index_fragments
-- WHERE table_id = OBJECT_ID('dbo.specific_table_name')
GROUP BY table_id;
Also note that if the count of fragments is high you might consider a reorganize.
If you are after a specific Catalogue
Use SSMS
- Clik on [Database] and expand the objects
- Click on [Storage]
- Right Click on {Specific Catalogue}
- Choose Propertie and click.
IN General TAB.. You will find the Catalogue Size = 'nn'
I use something similar to this (which will also calculate the size of XML-indexes, ... if present)
SELECT S.name,
SO.name,
SIT.internal_type_desc,
rows = CASE WHEN GROUPING(SIT.internal_type_desc) = 0 THEN SUM(SP.rows)
END,
TotalSpaceGB = SUM(SAU.total_pages) * 8 / 1048576.0,
UsedSpaceGB = SUM(SAU.used_pages) * 8 / 1048576.0,
UnusedSpaceGB = SUM(SAU.total_pages - SAU.used_pages) * 8 / 1048576.0,
TotalSpaceKB = SUM(SAU.total_pages) * 8,
UsedSpaceKB = SUM(SAU.used_pages) * 8,
UnusedSpaceKB = SUM(SAU.total_pages - SAU.used_pages) * 8
FROM sys.objects SO
INNER JOIN sys.schemas S ON S.schema_id = SO.schema_id
INNER JOIN sys.internal_tables SIT ON SIT.parent_object_id = SO.object_id
INNER JOIN sys.partitions SP ON SP.object_id = SIT.object_id
INNER JOIN sys.allocation_units SAU ON (SAU.type IN (1, 3)
AND SAU.container_id = SP.hobt_id)
OR (SAU.type = 2
AND SAU.container_id = SP.partition_id)
WHERE S.name = 'schema'
--AND SO.name IN ('TableName')
GROUP BY GROUPING SETS(
(S.name,
SO.name,
SIT.internal_type_desc),
(S.name, SO.name), (S.name), ())
ORDER BY S.name,
SO.name,
SIT.internal_type_desc;
This will generally give numbers higher than sys.fulltext_index_fragments, but when combined with the sys.partitions of the table, it will add up to the numbers returned from EXEC sys.sp_spaceused #objname = N'schema.TableName';.
Tested with SQL Server 2016, but documentation says it should be present since 2008.