473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query: How can I get this result?

Hello all,

I tryed to simplify the problem as much as possible. I'll start with the
DDL:

----------------------------------
CREATE TABLE #MyTable(
NoID INT,
Type CHAR,
DateTransaction DATETIME
)

INSERT INTO #MyTable (NoID, Type, DateTransaction)
SELECT 1 AS NoID, 'A' AS Type, '2004-01-01' AS DateTransaction UNION ALL
SELECT 2, 'C', '2004-01-01' UNION ALL
SELECT 3, 'B', '2004-01-01' UNION ALL
SELECT 4, 'C', '2004-01-02' UNION ALL
SELECT 5, 'B', '2004-01-02' UNION ALL
SELECT 6, 'C', '2004-01-02' UNION ALL
SELECT 7, 'A', '2004-01-03' UNION ALL
SELECT 8, 'B', '2004-01-03' UNION ALL
SELECT 9, 'A', '2004-01-03' UNION ALL
SELECT 10, 'C', '2004-01-03' UNION ALL
SELECT 11, 'B', '2004-01-03'
----------------------------------

What I want is all the same Type which, for a same DateTransaction, as a
different Type inserte beetween them when data is sorted by DateTransaction
and NoID. In this case I would like:

Type DateTransaction
------ -----------------
C 2004-01-02 /* B is between two C (NoID = 5) */
A 2004-01-03 /* B is between tow A (NoID = 8) */
B 2004-01-03 /* A and C are between two B (NoID = 9 and
10) */

All of these have for the corresponding date at least one transaction with a
different type between.

In the real situation NoID is an autoincrement field, the PK. And the
DateTransaction hasn't any time, just a round date.

Any suggestion?

Thanks for your time.

Yannick
Jul 20 '05 #1
7 1139
This is what I think you are asking for:

SELECT DISTINCT M2.type, M2.datetransaction
FROM #MyTable AS M1
JOIN
(SELECT type, datetransaction,
MIN(noid) AS minid, MAX(noid) AS maxid
FROM #MyTable
GROUP BY type, datetransaction
HAVING COUNT(*)>1) AS M2
ON M1.type <> M2.type
AND M1.datetransaction = M2.datetransaction
AND M1.noid > M2.minid
AND M1.noid < M2.maxid

However, I think it's unwise to base your sequence on an IDENTITY column
(NoId). The point of an IDENTITY column is that it's a system-generated
surrogate key which isn't supposed to have a meaning in your data model. For
multiple row inserts you can't gurarantee in what order the values will be
assigned so the result you get may not be what you expected. In short, it
appears that you may be missing the necessary information to define the
correct sequence of rows.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
"Yannick Turgeon" <no****@nowhere.com> wrote in message
news:T%********************@news20.bellglobal.com. ..
Hello all,

I tryed to simplify the problem as much as possible. I'll start with the
DDL:

----------------------------------
CREATE TABLE #MyTable(
NoID INT,
Type CHAR,
DateTransaction DATETIME
)

INSERT INTO #MyTable (NoID, Type, DateTransaction)
SELECT 1 AS NoID, 'A' AS Type, '2004-01-01' AS DateTransaction UNION ALL
SELECT 2, 'C', '2004-01-01' UNION ALL
SELECT 3, 'B', '2004-01-01' UNION ALL
SELECT 4, 'C', '2004-01-02' UNION ALL
SELECT 5, 'B', '2004-01-02' UNION ALL
SELECT 6, 'C', '2004-01-02' UNION ALL
SELECT 7, 'A', '2004-01-03' UNION ALL
SELECT 8, 'B', '2004-01-03' UNION ALL
SELECT 9, 'A', '2004-01-03' UNION ALL
SELECT 10, 'C', '2004-01-03' UNION ALL
SELECT 11, 'B', '2004-01-03'
----------------------------------

What I want is all the same Type which, for a same DateTransaction, as a
different Type inserte beetween them when data is sorted by DateTransaction
and NoID. In this case I would like:

Type DateTransaction
------ -----------------
C 2004-01-02 /* B is between two C (NoID = 5) */
A 2004-01-03 /* B is between tow A (NoID = 8) */
B 2004-01-03 /* A and C are between two B (NoID = 9 and
10) */

All of these have for the corresponding date at least one transaction with a
different type between.

In the real situation NoID is an autoincrement field, the PK. And the
DateTransaction hasn't any time, just a round date.

Any suggestion?

Thanks for your time.

Yannick


TYPE is a Standard SQL key word and should be avoided when naming.

SELECT DISTINCT T1."Type", T1.DateTransaction
FROM #MyTable AS T1
INNER JOIN
#MyTable AS T2
ON T1.DateTransaction = T2.DateTransaction AND
T1."Type" = T2."Type" AND
T1.NoID < T2.NoID - 1 AND
EXISTS (SELECT *
FROM #MyTable AS T3
WHERE T1.DateTransaction = T3.DateTransaction AND
T3."Type" <> T1."Type" AND
T3.NoID > T1.NoID AND
T3.NoID < T2.NoID)
ORDER BY T1.DateTransaction, T1."Type"

Type DateTransaction
C 2004-01-02 00:00:00.000
A 2004-01-03 00:00:00.000
B 2004-01-03 00:00:00.000

--
JAG
Jul 20 '05 #3
This is what I think you are asking for:

SELECT DISTINCT M2.type, M2.datetransaction
FROM #MyTable AS M1
JOIN
(SELECT type, datetransaction,
MIN(noid) AS minid, MAX(noid) AS maxid
FROM #MyTable
GROUP BY type, datetransaction
HAVING COUNT(*)>1) AS M2
ON M1.type <> M2.type
AND M1.datetransaction = M2.datetransaction
AND M1.noid > M2.minid
AND M1.noid < M2.maxid

However, I think it's unwise to base your sequence on an IDENTITY column
(NoId). The point of an IDENTITY column is that it's a system-generated
surrogate key which isn't supposed to have a meaning in your data model. For
multiple row inserts you can't gurarantee in what order the values will be
assigned so the result you get may not be what you expected. In short, it
appears that you may be missing the necessary information to define the
correct sequence of rows.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #4
"David Portas" <RE****************************@acm.org> wrote in message
news:f5********************@giganews.com...
This is what I think you are asking for:

SELECT DISTINCT M2.type, M2.datetransaction
FROM #MyTable AS M1
JOIN
(SELECT type, datetransaction,
MIN(noid) AS minid, MAX(noid) AS maxid
FROM #MyTable
GROUP BY type, datetransaction
HAVING COUNT(*)>1) AS M2
ON M1.type <> M2.type
AND M1.datetransaction = M2.datetransaction
AND M1.noid > M2.minid
AND M1.noid < M2.maxid

However, I think it's unwise to base your sequence on an IDENTITY column
(NoId). The point of an IDENTITY column is that it's a system-generated
surrogate key which isn't supposed to have a meaning in your data model. For
multiple row inserts you can't gurarantee in what order the values will be
assigned so the result you get may not be what you expected. In short, it
appears that you may be missing the necessary information to define the
correct sequence of rows.

--
David Portas
SQL Server MVP
--


David, like the GROUP BY approach. Following that through, we can simplify
your query to

SELECT "Type", DateTransaction
FROM #MyTable
GROUP BY "Type", DateTransaction
HAVING MIN(NoID) < MAX(NoID) - 1

--
JAG
Jul 20 '05 #5
John,

Yannick said he wanted Types with a *different* Type inserted beetween them.
Your simplified GROUP BY will give us any group with more than 3 rows of the
*same* type. Also I think you've assumed there will be no gaps in the NoId
sequence - which is another reason why one might not want to rely on NoId to
drive this query.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #6
David,

Your solution worked perfectly. Thanks for that. You were right too saying
that I was looking for a *different* type and that you cannot assumed there
was no gaps between NoId.

Yes our NoId has a meaning in our data and programming code which is "the
inserted order". The insertion is always one at a time. Sort of Invoice #.
But, just to express of I feel since I work here (4 years now), this table
has a much more important design problem: there is a field (PreviousBalance)
which contains data dependent on "previous" rows. This is a pain since I'm
here and, actually, this design flaw is at the source of the problem
conserning this thread and some other before. The NoID I can easily leave
with but the other one... it's tougher!

Thanks again for your time to both of you.

Yannick
"David Portas" <RE****************************@acm.org> wrote in message
news:va********************@giganews.com...
John,

Yannick said he wanted Types with a *different* Type inserted beetween them. Your simplified GROUP BY will give us any group with more than 3 rows of the *same* type. Also I think you've assumed there will be no gaps in the NoId
sequence - which is another reason why one might not want to rely on NoId to drive this query.

--
David Portas
SQL Server MVP
--

Jul 20 '05 #7
"David Portas" <RE****************************@acm.org> wrote in message
news:va********************@giganews.com...
John,

Yannick said he wanted Types with a *different* Type inserted beetween them.
Your simplified GROUP BY will give us any group with more than 3 rows of the
*same* type. Also I think you've assumed there will be no gaps in the NoId
sequence - which is another reason why one might not want to rely on NoId to
drive this query.
Yes, sorry, you're absolutely right David. I suppose I'd have to do the
following with this approach to make it work:

SELECT "Type", DateTransaction
FROM #MyTable AS T1
GROUP BY "Type", DateTransaction
HAVING MIN(NoID) < MAX(NoID) - 1 AND
EXISTS (SELECT *
FROM #MyTable AS T2
WHERE T2.DateTransaction = T1.DateTransaction AND
T2."Type" <> T1."Type" AND
T2.NoID > MIN(T1.NoID) AND
T2.NoID < MAX(T1.NoID))

--
JAG
--
David Portas
SQL Server MVP
--

Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wm | last post by:
I have a query that I expect to return 3 or 4 entries -- but I seem to be getting only the most recent entry, repeated 4 times. What am I doing wrong here? $query="SELECT...
0
by: Phil Powell | last post by:
I have a very simple mySqlQuery object that takes two parameters: 1) the string query 2) the db connection resource I tested and was certain everything is passing correctly (the string query...
1
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
2
by: Mike Poe | last post by:
Hi, Consider the following: <? $username = "foo"; $password = "bar"; $host = "db";
2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
4
by: Bob Bedford | last post by:
We have no access to a mysql NG on my provider's server, so we ask here: We have a long query (long in text) with a UNION between 2 select. We have been informed that some times the query...
1
by: muelli75 | last post by:
Hi! Im getting insane by solving a problem .... I try to define a function which uses a code-snippet from another file. My base are the codes from the great book "WebDataBase-Book by H....
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.