Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
SQL PROBLEM: checking shipments from yesterday, today and tomorrow with exceptions on Monday and Fri

is it possible to create a data model in BI publisher (OTM) that looks at the shipments of:
- yesterdays date
- today date
- tomorrow date
With the exception that on Friday instead of looking at tomorrow we look at next Monday and on Monday instead of looking at yesterday we look at last Friday.
The code that we have so far to look at the shipments for yesterday, today and tomorrow is, but we would like to put in the exception. Can anyone help us with this? thanks in advance for the help!!!
SELECT
S.SHIPMENT_GID SHIPMENT
FROM
SHIPMENT S,
SHIPMENT_INVOLVED_PARTY INV
WHERE
S.PERSPECTIVE = 'B'
AND
S.SHIPMENT_GID = INV.SHIPMENT_GID
AND
INV.INVOLVED_PARTY_QUAL_GID = 'BILL-TO'
AND
INV.INVOLVED_PARTY_CONTACT_GID = 'RSK.50144'
AND
(TRUNC(sysdate)+ 1 = TRUNC(S.START_TIME) OR TRUNC(sysdate) = TRUNC(S.START_TIME) OR TRUNC (sysdate) -1 = TRUNC(S.START_TIME))
Best Answer
-
Oops,
It should be CASE TRUNC(T.TODAY) - TRUNC(TODAY,'IW'), not CASE TRUNC(T.TODAY,'IW') - TRUNC(TODAY):
SELECT S.SHIPMENT_GID SHIPMENT FROM SHIPMENT S, SHIPMENT_INVOLVED_PARTY INV WHERE S.PERSPECTIVE = 'B' AND S.SHIPMENT_GID = INV.SHIPMENT_GID AND INV.INVOLVED_PARTY_QUAL_GID = 'BILL-TO' AND INV.INVOLVED_PARTY_CONTACT_GID = 'RSK.50144' AND ( TRUNC(S.START_TIME) = TRUNC(SYSDATE) -- today OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE) - TRUNC(SYSDATE,'IW') WHEN 0 THEN TRUNC(SYSDATE) - 3 -- Friday, if today is Monday ELSE TRUNC(SYSDATE) - 1 -- yesterday END OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE) - TRUNC(SYSDATE,'IW') WHEN 4 THEN TRUNC(SYSDATE) + 3 -- Monday, if today is Friday ELSE TRUNC(SYSDATE) + 1 -- tomorrow END ) /
SY.
Answers
-
SELECT S.SHIPMENT_GID SHIPMENT FROM SHIPMENT S, SHIPMENT_INVOLVED_PARTY INV WHERE S.PERSPECTIVE = 'B' AND S.SHIPMENT_GID = INV.SHIPMENT_GID AND INV.INVOLVED_PARTY_QUAL_GID = 'BILL-TO' AND INV.INVOLVED_PARTY_CONTACT_GID = 'RSK.50144' AND ( TRUNC(S.START_TIME) = TRUNC(SYSDATE) -- today OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE,'IW') - TRUNC(SYSDATE) WHEN 0 THEN TRUNC(SYSDATE) - 3 -- Friday, if today is Monday ELSE TRUNC(SYSDATE) - 1 -- yesterday END OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE,'IW') - TRUNC(SYSDATE) WHEN 4 THEN TRUNC(SYSDATE) + 3 -- Monday, if today is Friday ELSE TRUNC(SYSDATE) + 1 -- tomorrow END ) /
SY.
-
The code obtained unfortunately does not work, the S.START_TIME is a timestamp is it perhaps because of that that the code does not work?
-
Hi, @User_63WSS
Whenever you have a question, please post a little sample data (CREATE TABLE and INSERT statements for all tables involved, relevant columns only) so the people who want to help you can re-create the problem and test their ideas. Also post the exact results you want from that data, and explain why you want those results from that data. Always post your complete Oracle version (e.g. 18.4.0.0.0).
Since you didn't give any sample data, Solomon couldn't test his idea. Perhaps instead of
TRUNC(SYSDATE,'IW') - TRUNC(SYSDATE)
he meantTRUNC (SYSDATE) - TRUNC( SYSDATE, 'IW')
. If that doesn't solve the problem, then point out exactly where his solution is wrong with the sample data you post. -
Ho, @User_63WSS
What if today is Saturday or Sunday? What if today is Monday or Friday, and there were shipments made on the nearest Saturday or Sunday? If things like this are possible in you application, then include examples when you post the sample data and desired results.
-
Oops,
It should be CASE TRUNC(T.TODAY) - TRUNC(TODAY,'IW'), not CASE TRUNC(T.TODAY,'IW') - TRUNC(TODAY):
SELECT S.SHIPMENT_GID SHIPMENT FROM SHIPMENT S, SHIPMENT_INVOLVED_PARTY INV WHERE S.PERSPECTIVE = 'B' AND S.SHIPMENT_GID = INV.SHIPMENT_GID AND INV.INVOLVED_PARTY_QUAL_GID = 'BILL-TO' AND INV.INVOLVED_PARTY_CONTACT_GID = 'RSK.50144' AND ( TRUNC(S.START_TIME) = TRUNC(SYSDATE) -- today OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE) - TRUNC(SYSDATE,'IW') WHEN 0 THEN TRUNC(SYSDATE) - 3 -- Friday, if today is Monday ELSE TRUNC(SYSDATE) - 1 -- yesterday END OR TRUNC(S.START_TIME) = CASE TRUNC(SYSDATE) - TRUNC(SYSDATE,'IW') WHEN 4 THEN TRUNC(SYSDATE) + 3 -- Monday, if today is Friday ELSE TRUNC(SYSDATE) + 1 -- tomorrow END ) /
SY.
-
@Solomon Yakobson Thanks so much for the help, this was exactly what we were looking for!