Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. sql query problem

sql query problem

Scheduled Pinned Locked Moved Database
databasehelpquestion
5 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pitwi
    wrote on last edited by
    #1

    I have the following query in my own photo database:

    select distinct left(Nr, 8) as Date, Ort from Fotos where Typ='D' order by left(Nr, 8) desc, Ort

    Where Nr is a unique string with date + a hyphen + a sequential number in the form yyyymmdd-xxx. Ort is a string, the location of the photo. This works fine so far and I get a list like this:

    20220717 Lindabrunn, Symposion
    20220717 Feistritzsattel
    20220717 Pernitz, Schärfthal
    20220715 Mannswörth
    20220715 Wienerbergteich
    20220715 Zentralfriedhof
    20220712 V
    20220710 Himberg

    But what I really want is the first Nr for a date and a location like this:

    20220717-001 Lindabrunn, Symposion
    20220717-045 Feistritzsattel
    20220717-103 Pernitz, Schärfthal
    20220715-001 Mannswörth
    20220715-009 Wienerbergteich
    20220715-033 Zentralfriedhof
    20220712-001 V
    20220710-001 Himberg

    Can someone help? Thanks

    Richard DeemingR 1 Reply Last reply
    0
    • P pitwi

      I have the following query in my own photo database:

      select distinct left(Nr, 8) as Date, Ort from Fotos where Typ='D' order by left(Nr, 8) desc, Ort

      Where Nr is a unique string with date + a hyphen + a sequential number in the form yyyymmdd-xxx. Ort is a string, the location of the photo. This works fine so far and I get a list like this:

      20220717 Lindabrunn, Symposion
      20220717 Feistritzsattel
      20220717 Pernitz, Schärfthal
      20220715 Mannswörth
      20220715 Wienerbergteich
      20220715 Zentralfriedhof
      20220712 V
      20220710 Himberg

      But what I really want is the first Nr for a date and a location like this:

      20220717-001 Lindabrunn, Symposion
      20220717-045 Feistritzsattel
      20220717-103 Pernitz, Schärfthal
      20220715-001 Mannswörth
      20220715-009 Wienerbergteich
      20220715-033 Zentralfriedhof
      20220712-001 V
      20220710-001 Himberg

      Can someone help? Thanks

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Assuming SQL Server, try something like this:

      WITH cteOrderedData As
      (
      SELECT
      Nr,
      Ort,
      ROW_NUMBER() OVER (PARTITION BY Left(Nr, 8), Ort ORDER BY Nr) As RN
      FROM
      Fotos
      WHERE
      Type = 'D'
      )
      SELECT
      Nr,
      Ort
      FROM
      cteOrderedData
      WHERE
      RN = 1
      ;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Assuming SQL Server, try something like this:

        WITH cteOrderedData As
        (
        SELECT
        Nr,
        Ort,
        ROW_NUMBER() OVER (PARTITION BY Left(Nr, 8), Ort ORDER BY Nr) As RN
        FROM
        Fotos
        WHERE
        Type = 'D'
        )
        SELECT
        Nr,
        Ort
        FROM
        cteOrderedData
        WHERE
        RN = 1
        ;


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        J Offline
        J Offline
        jsc42
        wrote on last edited by
        #3

        Do you really need something as complex as a CTE? Couldn't you simply do the following (off the top of my head, not tested; so caveat emptor):

        SELECT TOP (100) PERCENT
        Nbr,
        Ort
        FROM
        Fotos
        WHERE
        Type = 'D'
        ORDER BY
        SUBSTRING(Nbr, 1, 8) DESC,
        Ort

        Richard DeemingR 1 Reply Last reply
        0
        • J jsc42

          Do you really need something as complex as a CTE? Couldn't you simply do the following (off the top of my head, not tested; so caveat emptor):

          SELECT TOP (100) PERCENT
          Nbr,
          Ort
          FROM
          Fotos
          WHERE
          Type = 'D'
          ORDER BY
          SUBSTRING(Nbr, 1, 8) DESC,
          Ort

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          The problem is that the OP wants to extract the first record for each unique 8-character prefix, whereas your code will return all records for each prefix. :) For example, given the input data:

          Nbr Ort
          20220717-001 A
          20220717-002 B
          20220717-003 C

          Your code would return all three, whereas the OP only wants the first one.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          J 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            The problem is that the OP wants to extract the first record for each unique 8-character prefix, whereas your code will return all records for each prefix. :) For example, given the input data:

            Nbr Ort
            20220717-001 A
            20220717-002 B
            20220717-003 C

            Your code would return all three, whereas the OP only wants the first one.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            J Offline
            J Offline
            jsc42
            wrote on last edited by
            #5

            Thanks. I hadn't realised that. OP's sample output has multiple 20220717 dates (minus the suffixes) in it, so I had read it as 'sort all by date + Ort, ignoring the sequence no after the date'/

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups