Jump to content

Recommended Posts

Posted (edited)

I have an MS SQL DB with Table which have columns :   [Tabel ID], [SIGNATURE ID ], [DATE], [TEXT]

this is an simple SQL QUERY

  Quote

SELECT [SIGNATURE ID ], [DATE] FROM [TABLE]
WHERE [TEXT] like '%urz%skarb%'
ORDER BY [SIGNATURE ID ], [DATE] ASC

Expand  

This query is only first step in my task.

I was trying to get unique [SIGNATURE ID] but only for the latest [DATE]

Can anybody show me the proper SQL Query ?

 

Regards,
mLipok

Remarks:
It may sound strange even stupid, because although I know ADO and other technologies well, I do not work with complicated SQL Queries, I usually have very simple cases.

 

EDIT:
I know that I can use .GetRows from this SQL Query which I post, and then loop through this table, analyze them and create new result table, but I was trying to do this with one query without any further AutoIt analysis, which should be much faster for large amount of data (about 10000 rows which I get from my first query ).

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
SELECT [SIGNATURE ID ], [DATE] FROM [TABLE]
WHERE [TEXT] like '%urz%skarb%'
ORDER BY [DATE] ASC LIMIT 1

Will return one row with the requested columns for the oldest date satisfying text search.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)
  On 12/20/2018 at 12:34 PM, jchd said:

Will return one row with the requested columns for the oldest date satisfying text search.

Expand  

My intention was to get latest [DATE] for each unique [SIGNATURE ID] ,   I mean not single row.

  On 12/20/2018 at 1:28 PM, user4157124 said:

To group by [SIGNATURE ID] showing last [DATE] for each:

SELECT
    [SIGNATURE ID],
    MAX([DATE])
FROM
    [TABLE]
WHERE
    [TEXT] LIKE '%urz%skarb%'
GROUP BY
    [SIGNATURE ID]
ORDER BY
    [DATE] ASC
;

 

Expand  

I think/hope this should be what I was looking for.
I will check it soon and back with results.

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Probably not that then: grouping is done first, which result in one (random) row being selected for each signature; then the resultset is sorted on date ascending. SQL doesn't explicitely specify which row is selected by GROUP BY, so assume the choice is random and not repeatable. Hence the resulting row may not be the oldest date for each signature.

In the OP I understood "unique signature" as "one row".

 

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

Is there any way to make any QUERY < SUBQUERY < SUBQUERY solution ?

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

That can be done with a CTE:

with [sigs] as (select distinct [signature id] [s] from [table] where [TEXT] like '%urz%skarb%') 
select [s], (select min([date]) from [table] [d] where [d.sign] = [s]) from [sigs];

Edit: can as well be written as a self-join, but I find CTEs clearer.

Edited by jchd
  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

@jchd Aggregate function MAX() on [DATE] explicitly defines selected value per group. Exact record should be irrelevant ([SIGNATURE ID] is equal for each group's record; they're grouped). ORDER BY aims to sort result (not records in each group). Described behavior would indeed occur if no aggregation were applied (incorrect use of GROUP BY). Did I misunderstand something?

Posted

OK, you're right. I was also confused by MAX while you actually mean MIN. The order by clause is irrelevant, unless output order is meant by date or any other criterion.

I'm an SQLite fan and I know engines differ on how grouping-aggregation work: some bark other not, SQLite being overly friendly in trying to be kind. Not having ever used MS SQL I suspected it could behave differently.

Sorry for noise I should have paid more attention.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • 3 months later...
Posted

I can't understand properly because i am new in this filed to learning SQL as a beginner. I have also some query can you suggest the best forum. I follow https://hackr.io/tutorials/learn-sql to learn SQL programming. is there any other better way

Posted

Which engine are you using? SQL has a number of variants (even deviant) implementations.

My recommendation to learn SQL would be to start with a "simple" yet powerful engine you can play with easily: SQLite. No installation nor maintenance needed as it's an embedded engine (not client-server). You can download and play with some free third-pary SQLite DB manager (I recommend SQLite Expert).

Create some DBs and learn the basics by guided experimentation (W3school is a good reference; also sqlite.org website). Assign yourself a sensible goal: i.e. create a cute complete directory of relatives and friends (adding more details at each design step). Then build an appointments scheduler coupled with your contacts with features similar to what the apps in your smartphone offer.

Another direction: supply chain management. Build something to handle suppliers, items, buys, stock, orders, sales, deliveries, payments. You can just do with what you buy and use in your own home (real or simulated). All products come with a barcode, add designation, type; enter date bought, price, quantity, date used.  Create views to profile your consumption of various types of items: food, or wearables, or ..., etc. grouped by period, or maker, or whatever criterion of your choice.

If you're serious about learning the possibilities are endless.

Don't waste your time making programmatic applications: just enter directly data in base tables then use the built-in SQL tabs to issue SQL statements and/or use ad-hoc views to access your data. This way you can concentrate your time on manipulating SQL, accumulating working knowledge during your learning curve.

Don't forget to try large DBs (samples available on the net) and see how to optimize both the DB design and queries.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)
  On 12/20/2018 at 11:05 AM, mLipok said:

I was trying to get unique [SIGNATURE ID] but only for the latest [DATE]

Expand  
SELECT sig
        WHERE date in 
        (select max(date) from table);

Or

SELECT distinct sig, date 
    from [table]
    order by date desc

==> Should return a two column list of sigs and dates, with the top items being most recent dates. DISTINCT should give you a single instance of each sig

 

Am I too late to the party?

Is it possible to provide SQLite example? (the SQL should be very similar)

Edited by Skysnake
another option

Skysnake

Why is the snake in the sky?

  • 8 months later...
Posted

SELECT distinct
    [SIGNATURE ID ]
FROM 
    [TABLE]
WHERE 
    [TEXT] like '%urz%skarb%'
    and DATE in
    (
        select 
            max([date])
        from
            [table]
        where
            [TEXT] like '%urz%skarb%'
    )
 

Posted

Thank you for you answer.

And :welcome: to the AutoIt forum.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

This works with SQL[ite]:

CREATE TABLE "Signatures" (
  "Signature" CHAR, 
  "Stamp" CHAR DEFAULT (Datetime('now', 'utc')));

insert into Signatures values 
('aaa','2019-12-10 13:30:18'),
('bbb','2019-12-10 13:30:22'),
('aaa','2019-12-10 13:30:26'),
('ccc','2019-12-10 13:30:30'),
('aaa','2019-12-10 13:30:32'),
('ddd','2019-12-10 13:30:37'),
('bbb','2019-12-10 13:30:39'),
('ccc','2019-12-10 13:30:41'),
('aaa','2019-12-10 13:30:44'),
('eee','2019-12-10 13:30:47'),
('fff','2019-12-10 13:39:40'),
('ggg','2019-12-10 13:39:44'),
('bbb','2019-12-10 13:39:47'),
('bbb','2019-12-10 13:39:49'),
('ddd','2019-12-10 13:39:53'),
('aaa','2019-12-10 13:39:54'),
('hhh','2019-12-10 13:39:57');

select
  distinct Signature Who,
  stamp LastDate
from
  signatures
group by
  who
having
  stamp = max(stamp)
order by
  LastDate asc;

gives:

Who LastDate
ccc 2019-12-10 13:30:41
eee 2019-12-10 13:30:47
fff 2019-12-10 13:39:40
ggg 2019-12-10 13:39:44
bbb 2019-12-10 13:39:49
ddd 2019-12-10 13:39:53
aaa 2019-12-10 13:39:54
hhh 2019-12-10 13:39:57

 

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

You're right, don't know why I used a having clause here. Anyway, no need of a subquery.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...