Home » SQL & PL/SQL » SQL & PL/SQL » fetch the same value with & without using the trunc ??
fetch the same value with & without using the trunc ?? [message #632568] Tue, 03 February 2015 04:51 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

SELECT pointed_date     pointed_date, 
       SUM (Nvl(quantity, 0)) quantity ,
       item_id
 FROM  table_1
 WHERE pointed_date >= Add_months (Trunc(SYSDATE), -4)
GROUP  BY pointed_date


SELECT Trunc(pointed_date)   pointed_date,
       SUM (Nvl(quantity, 0)) qty,
       item_id
 FROM  table_1
 WHERE pointed_date >= Add_months (Trunc(SYSDATE), -4)
GROUP  BY Trunc(pointed_date),item_id


Without Trunc 
Date            Qty    Item_id
----            ---    -------
4/28/2014	-37	43694
4/28/2014 2:35	-50	34966
4/28/2014 2:37	-30	34988
4/28/2014 2:57	-50	34966
4/28/2014 2:57	-30	34963
4/28/2014 3:01	-50	34966
4/28/2014 4:31	-3	43694
4/28/2014 5:40	-14	28393
4/28/2014 5:54	-10	28491
4/28/2014 6:12	-12	32686
4/28/2014 6:24	-6	19423

With trunc
4/28/2014	-50	34958
4/28/2014	-250	34965
4/28/2014	-6	32688
4/28/2014	-8	14653
4/28/2014	-30	1354
4/28/2014	-10	36119


Why i am getting different Item_id's for both queries??

I need to fetch same values


Edited by Lalit : edited a typo in topic title, it was truc instead of trunc]

[Updated on: Tue, 03 February 2015 04:59] by Moderator

Report message to a moderator

Re: fetch the same value with & without using the truc ?? [message #632569 is a reply to message #632568] Tue, 03 February 2015 04:56 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
I would have expected your code to return an "ORA-00979: not a group by expression" error.
Re: fetch the same value with & without using the truc ?? [message #632570 is a reply to message #632568] Tue, 03 February 2015 04:57 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mist598 wrote on Tue, 03 February 2015 16:21

GROUP  BY pointed_date

GROUP  BY Trunc(pointed_date),item_id




Different queries, different results.

Quote:
I need to fetch same values


What does this mean? How could you expect same results for two completely different operations.
Re: fetch the same value with & without using the truc ?? [message #632571 is a reply to message #632570] Tue, 03 February 2015 05:00 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
What does this mean? How could you expect same results for two completely different operations.


But dates are same right?, why it is giving d/f Item_id's , how to get as same as 1st Query ??
Re: fetch the same value with & without using the truc ?? [message #632572 is a reply to message #632569] Tue, 03 February 2015 05:04 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
I would have expected your code to return an "ORA-00979: not a group by expression" error.

No getting out put , no errors John.. Smile
Re: fetch the same value with & without using the truc ?? [message #632573 is a reply to message #632571] Tue, 03 February 2015 05:07 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mist598 wrote on Tue, 03 February 2015 16:30
Quote:
What does this mean? How could you expect same results for two completely different operations.


But dates are same right?, why it is giving d/f Item_id's , how to get as same as 1st Query ??


Can you prove the two dates "SYSDATE and TRUNC(SYSDATE)" are same. If you group by trunc(date), you lose the time portion, thus all the rows with same date values are grouped.

[Updated on: Tue, 03 February 2015 05:08]

Report message to a moderator

Re: fetch the same value with & without using the truc ?? [message #632574 is a reply to message #632569] Tue, 03 February 2015 05:23 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
John Watson wrote on Tue, 03 February 2015 10:56
I would have expected your code to return an "ORA-00979: not a group by expression" error.


There are bugs in versions (11.1 springs to mind) that let some missing group bys slide.

I know this because it was the DBAs fault that invalid sql broke after the upgrade to 11.2 Laughing

So they made us turn that fix off Shocked
Re: fetch the same value with & without using the truc ?? [message #632576 is a reply to message #632574] Tue, 03 February 2015 05:36 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
To expand Johns point - the 1st query is invalid. It should throw a "not a GROUP BY expression" error.
If it doesn't then you've got a bug. You should always the ignore the results of a query that only runs because of a bug.
Add item_id to the group by of the first query and then compare.

EDIT: typo

[Updated on: Tue, 03 February 2015 05:36]

Report message to a moderator

Re: fetch the same value with & without using the trunc ?? [message #632577 is a reply to message #632568] Tue, 03 February 2015 05:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I don't believe you.
You didn't post what you actually executed.
Prove I am wrong by posting a COMPLETE TEST CASE we can reproduce as we already have asked you many times (as well as your complete Oracle version), and your SQL*Plus session.

Note: "SUM (Nvl(quantity, 0))", this proves you don't read the documentation and have not the basic of SQL. SUM(quantity) does the same and is the correct expression.

Re: fetch the same value with & without using the trunc ?? [message #632578 is a reply to message #632577] Tue, 03 February 2015 06:08 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

SELECT pointed_date     pointed_date, 
       SUM (Nvl(quantity , 0)) quantity ,
       item_id
 FROM  table_1
 WHERE 1=1
 and pointed_date >= Add_months (SYSDATE, -4)
GROUP  BY pointed_date,item_id
order by  item_id desc

output
------

3/28/2014 7:56	-1	43694
4/2/2014 4:40	-9	43694
4/7/2014 4:26	-3	43694 


SELECT pointed_date,qty,item_id
from (select  Trunc(pointed_date)   pointed_date,
       SUM (Nvl(quantity ,0)) qty,
       item_id
 FROM  table_1
 WHERE 1=1 
 and  pointed_date >=  Add_months (SYSDATE, -4)
GROUP  BY Trunc(pointed_date),inventory_item_id)
order by  item_id desc

output
------
3/28/2014  -37	43694
4/2/2014   -33	43694
4/7/2014   -27	43694

Both output's

3/28/2014 7:56	-1	43694          3/28/2014  -37	43694       
4/2/2014 4:40	-9	43694          4/2/2014	  -33	43694
4/7/2014 4:26	-3	43694          4/7/2014	  -27	43694


Why the SUM's are different??
Re: fetch the same value with & without using the trunc ?? [message #632579 is a reply to message #632578] Tue, 03 February 2015 06:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Why you didn't post a test case?

Re: fetch the same value with & without using the trunc ?? [message #632580 is a reply to message #632578] Tue, 03 February 2015 06:14 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
One of those queries groups by a column table_1.item_id and the other groups by a column table_1.inventory_item_id.
If you are going to tell lies, at least make them believable.


--update: typo, sorry about that. I was in a hurry, because I was cross. Never post anything unless you are calm!

[Updated on: Tue, 03 February 2015 06:17]

Report message to a moderator

Re: fetch the same value with & without using the trunc ?? [message #632581 is a reply to message #632578] Tue, 03 February 2015 06:14 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
What you posted in original question and the previous post are different. Which one is supposed to be followed?
Re: fetch the same value with & without using the trunc ?? [message #632582 is a reply to message #632578] Tue, 03 February 2015 06:21 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mist598 wrote on Tue, 03 February 2015 17:38

Why the SUM's are different??



Michel Cadot wrote on Tue, 03 February 2015 17:27

Prove I am wrong by posting a COMPLETE TEST CASE we can reproduce as we already have asked you many times

Michel Cadot wrote on Tue, 03 February 2015 17:40

Why you didn't post a test case?



More?

Littlefoot wrote on Thu, 21 August 2014 12:56
If you could provide test case and explain the problem, someone might assist better than I can.
pablolee wrote on Thu, 21 August 2014 14:12
provide a test case as has been asked of you innumerable times in the past

Lalit Kumar B wrote on Fri, 22 August 2014 19:51
@mist598,

Invalid test cases repeatedly, that too after 900+ posts?





Bookmark this link TEST CASE
Re: fetch the same value with & without using the trunc ?? [message #632583 is a reply to message #632582] Tue, 03 February 2015 06:28 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
create table table_1(pointed_date date,quantity number,item_id number)

insert into table_1 values(to_date('3/28/2014 7:56','mm/dd/yyyy hh:ss'), -1,43694)
insert into table_1 values(to_date('4/2/2014 4:40','mm/dd/yyyy hh:ss'), -9,43694)
insert into table_1 values(to_date('4/7/2014 4:26','mm/dd/yyyy hh:ss'), -3,43694)

--drop table table_1

SELECT pointed_date     pointed_date, 
       SUM (Nvl(quantity , 0)) quantity ,
       item_id
 FROM  table_1
 WHERE 1=1
 --and pointed_date >= Add_months (SYSDATE, -4)
GROUP  BY pointed_date,item_id
order by  item_id desc


SELECT pointed_date,qty,item_id
from (select  Trunc(pointed_date)   pointed_date,
       SUM (Nvl(quantity ,0)) qty,
       item_id
 FROM  table_1
 WHERE 1=1 
  --and pointed_date >= Add_months (SYSDATE, -4)
GROUP  BY Trunc(pointed_date),item_id)
order by  item_id desc


Both are giving me the same result. I don't know why my original query d/f

Thanks every body.. Smile
Re: fetch the same value with & without using the trunc ?? [message #632584 is a reply to message #632583] Tue, 03 February 2015 06:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I don't know why my original query


Michel Cadot wrote on Tue, 03 February 2015 12:57

I don't believe you.
You didn't post what you actually executed.
Prove I am wrong by posting a COMPLETE TEST CASE we can reproduce as we already have asked you many times (as well as your complete Oracle version), and your SQL*Plus session.

Note: "SUM (Nvl(quantity, 0))", this proves you don't read the documentation and have not the basic of SQL. SUM(quantity) does the same and is the correct expression.

Re: fetch the same value with & without using the trunc ?? [message #632585 is a reply to message #632584] Tue, 03 February 2015 06:38 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
mist598

mm/dd/yyyy hh:ss

What a strange date format ...
Re: fetch the same value with & without using the trunc ?? [message #632591 is a reply to message #632585] Tue, 03 February 2015 07:09 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Unless you've got your server clock set to 7th August last year or earlier both those queries should give no rows.
Re: fetch the same value with & without using the trunc ?? [message #632595 is a reply to message #632591] Tue, 03 February 2015 08:01 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Filter is commented out.
Re: fetch the same value with & without using the trunc ?? [message #632597 is a reply to message #632595] Tue, 03 February 2015 08:18 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
True, though it was apparently present in the previous examples, so most of what's posted on this thread makes no sense.
Re: fetch the same value with & without using the trunc ?? [message #632598 is a reply to message #632597] Tue, 03 February 2015 08:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>so most of what's posted on this thread makes no sense.
so most of what's posted by OP in any thread makes no sense.
OP is candidate for forum's IGNORE feature, IMO
Re: fetch the same value with & without using the trunc ?? [message #632603 is a reply to message #632598] Tue, 03 February 2015 08:43 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I just googled "test case mist598 OraFAQ", and I am surprised to see the number of responses.
Re: fetch the same value with & without using the trunc ?? [message #632605 is a reply to message #632603] Tue, 03 February 2015 08:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I am surprised to see the number of responses.
Then you are a VERY slow learner!
Re: fetch the same value with & without using the trunc ?? [message #632607 is a reply to message #632605] Tue, 03 February 2015 08:59 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>Then you are a VERY slow learner!

Sometimes, a Google search is faster and closer than similar search on OraFAQ.
Re: fetch the same value with & without using the trunc ?? [message #633310 is a reply to message #632607] Sun, 15 February 2015 08:21 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@mist598, any feedback?
Previous Topic: Split row into two or more rows
Next Topic: "Client copy" with PL/SQL
Goto Forum:
  


Current Time: Thu Apr 18 07:43:52 CDT 2024