Search code examples
mysqlnode.jsorientdb

remove duplicate entry from single column which gives array in orientdb


SELECT medias,likes,description,createdon,title,postid,type,catagory,out('createdby').Name as Authors ,out('createdby').Nominator.Name as nominator, out('createdby').Nominator.school.Name as school  from post where isDeleted=false order by createdon desc 

I have to get details from each and every post. this is my relation

[post]-createdby->[student]

student table has a object name Nominator which holds the details of the nominator and table nominator has field called school which holds details of school

when i run the above command i get my output like this

[
        {
            "@type": "d",
            "@rid": "#-2:15",
            "@version": 0,
            "medias": "/audience/fileupload/uploads/images/d5f1c692c0a01bd0d308b0cc3aa090cb.jpg",
            "likes": 1,
            "description": "This is art of rabbit ",
            "createdon": "2017-10-23 22:00:27",
            "title": "Rabbit",
            "postid": "0hLU1",
            "type": "photo",
            "catagory": "Art",
            "Authors": [
                "SONISHA M"
            ],
            "nominator": [
                "karthikairani j"
            ],
            "school": [
                "Panchayat union middle school,keelamelkudi"
            ]
        },
        {
            "@type": "d",
            "@rid": "#-2:12",
            "@version": 0,
            "medias": "/audience/fileupload/uploads/images/fd1fe7227d1e6d6bfd455dffddb41db9.jpg",
            "likes": 1,
            "description": "This post of multiple students work on global warming issues ",
            "createdon": "2017-10-23 20:17:55",
            "title": "Global warming ",
            "postid": "BwOk7",
            "type": "photo",
            "catagory": "Art",
            "Authors": [
                "BALANIKUMAR M",
                "YUVARAJ R",
                "SOBIK RAJ C",
                "KALIDASS R"
            ],
            "nominator": [
                "Arokia raj",
                "Arokia raj",
                "Arokia raj",
                "Arokia raj"
            ],
            "school": [
                "Panchayat union middle school,keelamelkudi",
                "Panchayat union middle school,keelamelkudi",
                "Panchayat union middle school,keelamelkudi",
                "Panchayat union middle school,keelamelkudi"
            ]
        }

a single post can be done by many students under many nominators

so how can filter it for showing only single nominators name appearing once same for the school too

how can i change my code to get output like this is there any way i can make it more efficient

[
        {
            "@type": "d",
            "@rid": "#-2:15",
            "@version": 0,
            "medias": "/audience/fileupload/uploads/images/d5f1c692c0a01bd0d308b0cc3aa090cb.jpg",
            "likes": 1,
            "description": "This is art of rabbit ",
            "createdon": "2017-10-23 22:00:27",
            "title": "Rabbit",
            "postid": "0hLU1",
            "type": "photo",
            "catagory": "Art",
            "Authors": [
                "SONISHA M"
            ],
            "nominator": [
                "karthikairani j"
            ],
            "school": [
                "Panchayat union middle school,keelamelkudi"
            ]
        },
        {
            "@type": "d",
            "@rid": "#-2:12",
            "@version": 0,
            "medias": "/audience/fileupload/uploads/images/fd1fe7227d1e6d6bfd455dffddb41db9.jpg",
            "likes": 1,
            "description": "This post of multiple students work on global warming issues ",
            "createdon": "2017-10-23 20:17:55",
            "title": "Global warming ",
            "postid": "BwOk7",
            "type": "photo",
            "catagory": "Art",
            "Authors": [
                "BALANIKUMAR M",
                "YUVARAJ R",
                "SOBIK RAJ C",
                "KALIDASS R"
            ],
            "nominator": [
                "Arokia raj"
            ],
            "school": [
                "Panchayat union middle school,keelamelkudi"
            ]
        }

Solution

  • try this:

     SELECT medias,likes,description,createdon,title,postid,type,catagory,out('createdby').Name as Authors ,DISTINCT(nominator) as nominator, DISTINCT(school) as school  from (SELECT medias,likes,description,createdon,title,postid,type,catagory,out('createdby').Name as Authors ,out('createdby').Nominator.Name as nominator, out('createdby').Nominator.school.Name as school  from post where isDeleted=false order by createdon desc unwind nominator,school)