通过使用DECODE函数,将日期更改为任何字符串?

时间:2018-10-26 13:29:55

标签: sql oracle decode

我有2张桌子:分享交易股份包含公司ID,名称等。交易包含交易日期,ShareID。我需要一个选择查询,该查询列出交易的日期和COMPANYNAME,但如果公司的股份没有交易,它仍应出现在列表中,但在日期列中必须写“ No trade”。

示例:

份额

shareid companyname
0       Apple
1       Microsoft
2       Samsung
3       Nokia

交易:

transactionid   shareid  date
0               0        2016-12-4
1               1        2015-5-3
2               2        2014-10-22
3               2        2014-11-11

我需要的查询应列出以下列表:

Apple      2016-12-4
Microsoft  2015-5-3
Samsung    2014-10-22
Samsung    2014-11-11
Nokia      No trade

任务说明日期格式必须为YYYY-MM-DD,并且需要使用函数DECODE和TO_CHAR。

1 个答案:

答案 0 :(得分:1)

对我来说就像一个简单的外部连接。我会使用 $(function () { $(document).on("change", "#Project", function () { projectName = $('#Project').val(); $('#Datatable1').dataTable().fnDestroy(); BindDatatable1(); });}); function BindDatatable1() { var projectName1 = $('#Project').val(); projectName = projectName1; var url; var strdefectstatus = ""; for (var i = 0 ; i <= defectStatus.length - 1; i++) { if (i == 0) { strdefectstatus = "Status eq " + "'" + defectStatus[i] + "'"; } else strdefectstatus += " or Status eq " + "'" + defectStatus[i] + "'"; } var siteUrl = '@System.Configuration.ConfigurationManager.AppSettings["SiteUrl"]'; if (projectName == "" && defectStatus == "") { url = siteUrl + "odata/DefDTO"; } else if (projectName == "" && defectStatus != "") { url = siteUrl + "odata/DefDTO?$filter=(" + strdefectstatus + ")"; } else if (projectName != "" && defectStatus == "") { url = siteUrl + "odata/DefDTO?$filter=(ProjectName eq '" + projectName + "')"; } else { url = siteUrl + "odata/DefDTO?$filter=(ProjectName eq '" + projectName + "' and (" + strdefectstatus + "))"; } $('#Datatable1').DataTable({ "aLengthMenu": [[50, 100, -1], ["50", "100", "All"]], "bProcessing": true, "scrollX": false, "autoWidth": false, "bStateSave": true, "bServerSide": true, "sAjaxSource": url, "aaSorting": [[1, "desc"]], "aoColumns": [ { "mData": "Defectidstr", "defaultContent": "", "iDataSort": 1, "sType": "num-html", "mRender": function (data, type, row) { return "<a title='View' target='_blank' href='View?id=" + row.ID + "'>" + '#' + row.ID + '</a>'; //return "<a title='View' onclick='View(\"" + row.ID + "\")'>" + "#" + row.ID + '</a>'; } }, { "mData": "ID", "bVisible": false, className: 'never', "bSearchable": false, "sType": "numeric" }, { "mData": "ProjectName", "sType": "string" }, { "mData": "TaskTitle", "sType": "string", "className": "addwordBreak", "mRender": function (data) { if (data != null && data != "") { return FormatMultilineTextToHtml(limitCharaterLength(data, 250)); } return ""; } }, { "mData": "Comments", "sType": "string", className: 'none' }, { "mData": "Priority", "sType": "string", className: 'none' }, { "mData": "Owner", "sType": "string", "width": "100px" }, { "mData": "CreatedDate", "defaultContent": "", "width": "100px", "mRender": function (data, type, row) { return formatDate(row.CreatedDate); // return row.CreatedDate; } }, { "mData": "RespondedDate", "defaultContent": "", "width": "100px", "mRender": function (data, type, row) { if (data != null && data != "") { return formatDate(row.RespondedDate); } return ""; // return row.CreatedDate; } }, { "mData": "CreatedBy", "sType": "string", className: 'none' }, { "mData": "DefectStatus", "defaultContent": "", "width": "90px", "className": 'all', "mRender": function (data, type, row) { return ProTime.createStatusForDefect(row); } }, { "mData": "Description", "sType": "string", className: 'none' }, { "mData": "ID", "bSortable": false, "defaultContent": "", "className": 'all', "width": "100px", "mRender": function (data, type, row) { var jobtitle = '@Model.JobTitleName'; if (jobtitle == 'Administrator' || jobtitle == 'Jr SQA') { return "<div class='btn-toolbar'>" + "<button id='btnEdit' class='btn btn-info btn-xs' title='Edit' onclick='EditDefect(\"" + row.ID + "\")'><i class='fa fa-pencil fa-lg'></i></button>" + "<a class=\"btn btn-danger btn-xs\" href='javascript:void(0);' onclick='confirmDeleteDefect(\"" + row.ID + "\")' title='Delete'><i class='fa fa-trash fa-lg modal-link'></i></a>" + "<button id='btnView' class='btn btn-success btn-xs' title='View' onclick='View(\"" + row.ID + "\")'><i class='fa fa-eye fa-lg'></i></button>" + "</div>"; } else { return "<div class='btn-toolbar'>" + "<button id='btnEdit' class='btn btn-info btn-xs' title='Edit' onclick='EditDefect(\"" + row.ID + "\")'><i class='fa fa-pencil fa-lg'></i></button>" + "<button id='btnView' class='btn btn-success btn-xs' title='View' onclick='View(\"" + row.ID + "\")'><i class='fa fa-eye fa-lg'></i></button>" + "</div>"; } } } ], "responsive": true, "fnServerData": fnServerOData, "iODataVersion": 4, "bUseODataViaJSONP": false, // set to true if using cross-domain requests "sDom": 'Tfrt<"bottom"lip>', "tableTools": { aButtons: [ { "sExtends": "text", "bFooter": false, "sButtonText": '<i class="fa fa-plus"></i>' + ' Create', "sButtonClass": "DTTT_button_xls project-xlsxp-btn", "fnClick": function () { return AddNewDef(); } } ] } }); }; 而不是coalesce()

decode()

外部联接将返回select s.companyname, coalesce(to_char(t.date, 'yyyy-mm-dd'), 'No trade') from shares s left join transactions t on s.shareid = t.shareid order by s.companyname; 表中的所有行,以及shares列中的null值,用于那些没有交易的股票。 transactions.date将日期转换为字符串,而to_char()将选择第一个非空值。因此,如果coalesce()为空,则返回t.date