MSSQL DISTINCT multiple fields throws up odd results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ukchat
    New Member
    • Sep 2006
    • 15

    MSSQL DISTINCT multiple fields throws up odd results

    I.m creating a dynamic query to pull out workbooks from my database the table structure is below.

    Table: curricworkbooks
    Columns: ID, curric, assessment, topic, workbook, filename

    Example data

    Code:
    [ID]	[curric]	[assessment]	[topic]		[workbook]	[filename]
    1	N1/E1.1 	Numeracy E1	Count		1		workbooks/Num Entry 1/Unit 1/04 N1E1.1-3 Worksheets Num.pdf
    10	MSS1/E1.6	Numeracy E1	Capacity 	13		workbooks/Num Entry 1/Unit 13/04 MSS1E1.6 Worksheets Num.pdf
    100	MSS1/L1.7	Numeracy L1	Conversions	14		workbooks/Numeracy Level 1/wkbk_14 AoN L1 2D 3D & Scale.pdf
    So my system will output each workbook in a table which works fine. using this query.

    Code:
    SELECT distinct(curricworkbooks.filename),workbook FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL ORDER BY workbook ASC
    but i need to also select the ID field from the table, so i thought the query would be somthing like this:


    Code:
    SELECT distinct(curricworkbooks.filename),workbook, ID FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL ORDER BY workbook ASC
    But when i use that query i get non distinct results. I need the distinct on filename as there can be multiple curriculum reference per each workbook which can relate to the same file (Trust me just beleive me on this on the structure is totaly unlogical but it's data that im unable to change.)

    All support greatlt appreciated.

    Posted simular to this yesterday but think the site got restored again.

    regards
    Liam
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Could you please post the following for reference:

    1. Sample data
    2. The output data of your query
    3. The sample output that you require

    Comment

    • ukchat
      New Member
      • Sep 2006
      • 15

      #3
      Originally posted by amitpatel66
      Could you please post the following for reference:

      1. Sample data
      2. The output data of your query
      3. The sample output that you require
      Table data (narrowed down to only 1 assessment)



      Query 1


      Query 2


      I have noticed that Microsoft SQL studio automatically take away my brackets from DISTINCT(filena me) Is this normal? As this would select distinct everything which means ID is never the same so all the same.

      I think I’ve kind of answered my own question but is there any way around this to select by just 1 distinct value? I’m use to MySQL and I would use unique(filename ) even although mssql manager highlights this as a built in function it returns saying it’s not a function.

      Regards
      Liam

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        I checked the sample data and I see that the ID is different for the same workbook. So your query is working perfectly since when you say distinct, it is getting distinct records because ID is different for each record of same workbook. Now when you say you want one record only each for workbook, then what should be the ID value.

        For Eg: For workbook = 2, the ID are 36,37,38,39, so which value you want to display? If all the four, then the workbook and filename will repeat for all the records.

        Comment

        • ukchat
          New Member
          • Sep 2006
          • 15

          #5
          Originally posted by amitpatel66
          I checked the sample data and I see that the ID is different for the same workbook. So your query is working perfectly since when you say distinct, it is getting distinct records because ID is different for each record of same workbook. Now when you say you want one record only each for workbook, then what should be the ID value.

          Dor Eg: For workbook = 2, the ID are 36,37,38,39, so which value you want to display? If all the four, then the workbook and filename will repeat for all the records.

          ID has been added as i have no other unique identifier.

          This is why im wanting the distinct on filename as there could be multiple curriculum references to each file.

          So all in all i need to do is select by distinct filename and i can sort out the rest.

          regards
          Liam

          Comment

          • amitpatel66
            Recognized Expert Top Contributor
            • Mar 2007
            • 2358

            #6
            Originally posted by ukchat
            ID has been added as i have no other unique identifier.

            This is why im wanting the distinct on filename as there could be multiple curriculum references to each file.

            So all in all i need to do is select by distinct filename and i can sort out the rest.

            regards
            Liam
            Then your first query will give you the distinct file names group by work book.

            [code=sql]
            SELECT MAX(curricworkb ooks.filename), workbook FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL GROUP BY workbook ORDER BY workbook ASC
            [/code]

            Comment

            • ukchat
              New Member
              • Sep 2006
              • 15

              #7
              Originally posted by amitpatel66
              Then your first query will give you the distinct file names group by work book.

              [code=sql]
              SELECT MAX(curricworkb ooks.filename), workbook FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL GROUP BY workbook ORDER BY workbook ASC
              [/code]

              that wont let me select any extra fields e.g. ID and curric which i'll be needing to do.

              So all in all i need a query that will select every field with a distinct filename.

              when i added extra fields to your query i got this error.



              Regards
              Liam

              Comment

              • amitpatel66
                Recognized Expert Top Contributor
                • Mar 2007
                • 2358

                #8
                [code=sql]

                SELECT x.filename,x.wo rkbook,cbw.ID FROM
                (SELECT MAX(curricworkb ooks.filename) AS "filename",work book FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL GROUP BY workbook ORDER BY workbook ASC) x, curricworkbooks cbw WHERE x.workbook = cbw.workbook AND x.filename = cbw.filename

                [/code]

                Comment

                • ukchat
                  New Member
                  • Sep 2006
                  • 15

                  #9
                  Originally posted by amitpatel66
                  [code=sql]

                  SELECT x.filename,x.wo rkbook,cbw.ID FROM
                  (SELECT MAX(curricworkb ooks.filename) AS "filename",work book FROM curricworkbooks WHERE assessment = 'Literacy E2' AND workbook IS NOT NULL GROUP BY workbook ORDER BY workbook ASC) x, curricworkbooks cbw WHERE x.workbook = cbw.workbook AND x.filename = cbw.filename

                  [/code]

                  Hi thanks but it's still not working i have posted returned data here: http://www.bksb.co.uk/bksb/data/query3.php


                  Qorkbook 1 is listed twice yet has the same filename.

                  regards
                  Liam

                  Comment

                  • amitpatel66
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 2358

                    #10
                    Originally posted by ukchat
                    Hi thanks but it's still not working i have posted returned data here: http://www.bksb.co.uk/bksb/data/query3.php


                    Qorkbook 1 is listed twice yet has the same filename.

                    regards
                    Liam
                    Ofcourse it will be listed twice becuase you have different ID value for both the records.Are you looking at something like this????

                    Code:
                    filename           Workbook                 ID
                      1                        1                         20
                    NULL                  NULL                    22

                    Comment

                    • ukchat
                      New Member
                      • Sep 2006
                      • 15

                      #11
                      Originally posted by amitpatel66
                      Ofcourse it will be listed twice becuase you have different ID value for both the records.Are you looking at something like this????

                      Code:
                      filename           Workbook                 ID
                        1                        1                         20
                      NULL                  NULL                    22
                      All i want is a distinct filename and that will leave me with the records i need. of corse ID is different else it couldnt be a Primary Key. The ID is only their so i can pass it to a PHP script to download the correct file without passing the location of the file through GET or POST (all testing of SQL is being done direct in SQL Studio)

                      Do you understand what im after?

                      Regards
                      Liam

                      Comment

                      • amitpatel66
                        Recognized Expert Top Contributor
                        • Mar 2007
                        • 2358

                        #12
                        Originally posted by ukchat
                        All i want is a distinct filename and that will leave me with the records i need. of corse ID is different else it couldnt be a Primary Key. The ID is only their so i can pass it to a PHP script to download the correct file without passing the location of the file through GET or POST (all testing of SQL is being done direct in SQL Studio)

                        Do you understand what im after?

                        Regards
                        Liam
                        Are you looking at:

                        SELECT DISTINCT filename from table_name WHERE id = (pass ID value from PHP) ?

                        Comment

                        • ukchat
                          New Member
                          • Sep 2006
                          • 15

                          #13
                          Originally posted by amitpatel66
                          Are you looking at:

                          SELECT DISTINCT filename from table_name WHERE id = (pass ID value from PHP) ?
                          No that is a seperate part what im doing is listing the unique filenames referenced by workbook number.

                          see attached picture



                          but this show 1 1 which i dont want it to as this is the same filename. How ever i cant list by distinct workbook as there are multiple files to each workbook in parts.

                          I need to select data where filename is unique. That's the extent of my situation.

                          Regards
                          Liam

                          Comment

                          • amitpatel66
                            Recognized Expert Top Contributor
                            • Mar 2007
                            • 2358

                            #14
                            What i understand from the above picture is that for Numeracy Level1, it is present in so many work books. So you want the work books to be displayed once instead of repeating?

                            Comment

                            • ukchat
                              New Member
                              • Sep 2006
                              • 15

                              #15
                              Originally posted by amitpatel66
                              What i understand from the above picture is that for Numeracy Level1, it is present in so many work books. So you want the work books to be displayed once instead of repeating?
                              yes----ish.. or a less there is a different filename for the workbook.

                              Trust me i can understand your difficulty in understanding this as i've had to go through the same process for over 200 workbooks.

                              the structure is totaly unlogical but unfortunatly i'm not the one that writes the workbooks, so i've had to just put up with it get it in database and now im trying to do this which i thought would be simple as im sure i can do this in MySQL by simply using unique(filename ) then it would only select reccords with a unique filename but MSSQL is being a swine and saying everything has to be distinct rather than just 1 value.

                              regards
                              Liam

                              Comment

                              Working...