Percentage of missing tracks and time length











up vote
1
down vote

favorite












For each compilation album, output its title, artist, and the following information (in the given order):

•number of tracks, as indicated in the Albums table;

•number of tracks actually present in the Tracks table;

•percentage of missing tracks, formatted as a decimal number with 1 decimal place;

•total running time in seconds (int), calculated as the average length of the album’s tracks listed in the Tracks table, multiplied by the number of tracks reported in the Albums table. Observe that for albums without missing tracks this will be the same as the sum of all track lengths.The output table will have 6 columns, and precisely one row for each compilation album in the Albums table. Compilation albums without tracks in the Tracks table will be included in the output: they will have 100% missing tracks and NULL running time.



Data format:

Artists



- name (unique string)
- type (either person or band)
- country (unique 3 digit code)


Albums



- title (string; may not be unique)
- artist (unique string)
- year (int)
- type (either studio, live, or Compilation)
- rating (int 1 thru 5)
- num of tracks (int)


Tracks



- reference to album (actually two columns: album's title & artist unique string)
- number (int; can be used with album title and artist for identification)
- title (string)
- time length (int, in seconds)


Expected output:



# of tracks according to Albums | # of tracks according to Tracks | % of missing tracks (to 1 decimal place)  | total running time (avg time in Tracks * # of tracks in Albums)


Note: # of each track will not be greater than the total number of tracks of the corresponding album reported in the Albums table





What I have done so far:



SELECT albums.tracks, tracks.NUMBER,
CASE WHEN tracks.NUMBER < albums.tracks THEN (1-tracks.Number/albums.tracks) else 0 end,
(sum(tracks.length)/count(tracks.number)) * albums.tracks
FROM albums join tracks on tracks.artist = albums.artist
WHERE type = 'COMPILATION'
and albums.artist = tracks.artist
group by albums.tracks, tracks.number, albums.title


So, this runs, but the percentage of missing tracks is off because it just returns "1" or "0" in the columns, which shouldn't be the case. The returned time length seems reasonable, but how to include albums missing the tracks on the tracks table and outputting Null?

Thanks










share|improve this question






















  • For percentage of missing tracks just multiply one of the values by 1.0 because it's float
    – JustMe
    Nov 19 at 13:54















up vote
1
down vote

favorite












For each compilation album, output its title, artist, and the following information (in the given order):

•number of tracks, as indicated in the Albums table;

•number of tracks actually present in the Tracks table;

•percentage of missing tracks, formatted as a decimal number with 1 decimal place;

•total running time in seconds (int), calculated as the average length of the album’s tracks listed in the Tracks table, multiplied by the number of tracks reported in the Albums table. Observe that for albums without missing tracks this will be the same as the sum of all track lengths.The output table will have 6 columns, and precisely one row for each compilation album in the Albums table. Compilation albums without tracks in the Tracks table will be included in the output: they will have 100% missing tracks and NULL running time.



Data format:

Artists



- name (unique string)
- type (either person or band)
- country (unique 3 digit code)


Albums



- title (string; may not be unique)
- artist (unique string)
- year (int)
- type (either studio, live, or Compilation)
- rating (int 1 thru 5)
- num of tracks (int)


Tracks



- reference to album (actually two columns: album's title & artist unique string)
- number (int; can be used with album title and artist for identification)
- title (string)
- time length (int, in seconds)


Expected output:



# of tracks according to Albums | # of tracks according to Tracks | % of missing tracks (to 1 decimal place)  | total running time (avg time in Tracks * # of tracks in Albums)


Note: # of each track will not be greater than the total number of tracks of the corresponding album reported in the Albums table





What I have done so far:



SELECT albums.tracks, tracks.NUMBER,
CASE WHEN tracks.NUMBER < albums.tracks THEN (1-tracks.Number/albums.tracks) else 0 end,
(sum(tracks.length)/count(tracks.number)) * albums.tracks
FROM albums join tracks on tracks.artist = albums.artist
WHERE type = 'COMPILATION'
and albums.artist = tracks.artist
group by albums.tracks, tracks.number, albums.title


So, this runs, but the percentage of missing tracks is off because it just returns "1" or "0" in the columns, which shouldn't be the case. The returned time length seems reasonable, but how to include albums missing the tracks on the tracks table and outputting Null?

Thanks










share|improve this question






















  • For percentage of missing tracks just multiply one of the values by 1.0 because it's float
    – JustMe
    Nov 19 at 13:54













up vote
1
down vote

favorite









up vote
1
down vote

favorite











For each compilation album, output its title, artist, and the following information (in the given order):

•number of tracks, as indicated in the Albums table;

•number of tracks actually present in the Tracks table;

•percentage of missing tracks, formatted as a decimal number with 1 decimal place;

•total running time in seconds (int), calculated as the average length of the album’s tracks listed in the Tracks table, multiplied by the number of tracks reported in the Albums table. Observe that for albums without missing tracks this will be the same as the sum of all track lengths.The output table will have 6 columns, and precisely one row for each compilation album in the Albums table. Compilation albums without tracks in the Tracks table will be included in the output: they will have 100% missing tracks and NULL running time.



Data format:

Artists



- name (unique string)
- type (either person or band)
- country (unique 3 digit code)


Albums



- title (string; may not be unique)
- artist (unique string)
- year (int)
- type (either studio, live, or Compilation)
- rating (int 1 thru 5)
- num of tracks (int)


Tracks



- reference to album (actually two columns: album's title & artist unique string)
- number (int; can be used with album title and artist for identification)
- title (string)
- time length (int, in seconds)


Expected output:



# of tracks according to Albums | # of tracks according to Tracks | % of missing tracks (to 1 decimal place)  | total running time (avg time in Tracks * # of tracks in Albums)


Note: # of each track will not be greater than the total number of tracks of the corresponding album reported in the Albums table





What I have done so far:



SELECT albums.tracks, tracks.NUMBER,
CASE WHEN tracks.NUMBER < albums.tracks THEN (1-tracks.Number/albums.tracks) else 0 end,
(sum(tracks.length)/count(tracks.number)) * albums.tracks
FROM albums join tracks on tracks.artist = albums.artist
WHERE type = 'COMPILATION'
and albums.artist = tracks.artist
group by albums.tracks, tracks.number, albums.title


So, this runs, but the percentage of missing tracks is off because it just returns "1" or "0" in the columns, which shouldn't be the case. The returned time length seems reasonable, but how to include albums missing the tracks on the tracks table and outputting Null?

Thanks










share|improve this question













For each compilation album, output its title, artist, and the following information (in the given order):

•number of tracks, as indicated in the Albums table;

•number of tracks actually present in the Tracks table;

•percentage of missing tracks, formatted as a decimal number with 1 decimal place;

•total running time in seconds (int), calculated as the average length of the album’s tracks listed in the Tracks table, multiplied by the number of tracks reported in the Albums table. Observe that for albums without missing tracks this will be the same as the sum of all track lengths.The output table will have 6 columns, and precisely one row for each compilation album in the Albums table. Compilation albums without tracks in the Tracks table will be included in the output: they will have 100% missing tracks and NULL running time.



Data format:

Artists



- name (unique string)
- type (either person or band)
- country (unique 3 digit code)


Albums



- title (string; may not be unique)
- artist (unique string)
- year (int)
- type (either studio, live, or Compilation)
- rating (int 1 thru 5)
- num of tracks (int)


Tracks



- reference to album (actually two columns: album's title & artist unique string)
- number (int; can be used with album title and artist for identification)
- title (string)
- time length (int, in seconds)


Expected output:



# of tracks according to Albums | # of tracks according to Tracks | % of missing tracks (to 1 decimal place)  | total running time (avg time in Tracks * # of tracks in Albums)


Note: # of each track will not be greater than the total number of tracks of the corresponding album reported in the Albums table





What I have done so far:



SELECT albums.tracks, tracks.NUMBER,
CASE WHEN tracks.NUMBER < albums.tracks THEN (1-tracks.Number/albums.tracks) else 0 end,
(sum(tracks.length)/count(tracks.number)) * albums.tracks
FROM albums join tracks on tracks.artist = albums.artist
WHERE type = 'COMPILATION'
and albums.artist = tracks.artist
group by albums.tracks, tracks.number, albums.title


So, this runs, but the percentage of missing tracks is off because it just returns "1" or "0" in the columns, which shouldn't be the case. The returned time length seems reasonable, but how to include albums missing the tracks on the tracks table and outputting Null?

Thanks







postgresql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 10:40









Anon Li

356




356












  • For percentage of missing tracks just multiply one of the values by 1.0 because it's float
    – JustMe
    Nov 19 at 13:54


















  • For percentage of missing tracks just multiply one of the values by 1.0 because it's float
    – JustMe
    Nov 19 at 13:54
















For percentage of missing tracks just multiply one of the values by 1.0 because it's float
– JustMe
Nov 19 at 13:54




For percentage of missing tracks just multiply one of the values by 1.0 because it's float
– JustMe
Nov 19 at 13:54

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372844%2fpercentage-of-missing-tracks-and-time-length%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372844%2fpercentage-of-missing-tracks-and-time-length%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen