Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL Help on Equals

Author  Topic 

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 14:59:20
I am stuck:
I have DB that has items that the record could both have an 'r' or an 's' in it I want to return only the ones that are an 's'. Currently if the record was ever an 's' even though now its an 'r' it still pulls it up. Heres my SQL:

SELECT PS_TKT_HIST.EVENT_NO, Convert(decimal(12,2), PS_TKT_HIST_LIN.QTY_SOLD) as QTY_SOLD, AR_CUST.NAM, PS_TKT_HIST.CUST_NO, PS_TKT_HIST_LIN.ITEM_NO, PS_TKT_HIST_LIN.TKT_NO, PS_TKT_HIST.TKT_DAT, PS_TKT_HIST_LIN.LIN_TYP FROM PS_TKT_HIST AS PS_TKT_HIST INNER JOIN PS_TKT_HIST_LIN AS PS_TKT_HIST_LIN ON PS_TKT_HIST.EVENT_NO = PS_TKT_HIST_LIN.EVENT_NO AND PS_TKT_HIST.STR_ID = PS_TKT_HIST_LIN.STR_ID AND PS_TKT_HIST.STA_ID = PS_TKT_HIST_LIN.STA_ID AND PS_TKT_HIST.TKT_NO = PS_TKT_HIST_LIN.TKT_NO LEFT OUTER JOIN AR_CUST AS AR_CUST ON PS_TKT_HIST.CUST_NO = AR_CUST.CUST_NO LEFT OUTER JOIN IM_ITEM AS IM_ITEM ON PS_TKT_HIST_LIN.ITEM_NO = IM_ITEM.ITEM_NO WHERE

(PS_TKT_HIST_LIN.LIN_TYP = 's') and (PS_TKT_HIST_LIN.LIN_TYP != 'r')
******THIS IS WHERE IM THROWN OFF I ONLY WANT THE ITEM IF IT IS AN S TO BE PULLED UP***** IF ITS CURRENTLY AN R THEN DOESNT NEED TO BE DISPLAYED*****


and (PS_TKT_HIST_LIN.LIN_TYP <> 'U') and (PS_TKT_HIST.TKT_DAT <= dateadd(day, datediff(day, 0, GETDATE())-7, 0))ORDER BY PS_TKT_HIST.CUST_NO, PS_TKT_HIST_LIN.ITEM_NO, PS_TKT_HIST_LIN.TKT_NO


Thanks

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-15 : 15:02:55
can you post some sample results with that line commented out?

SELECT PS_TKT_HIST.EVENT_NO, Convert(decimal(12,2), PS_TKT_HIST_LIN.QTY_SOLD) as QTY_SOLD, AR_CUST.NAM, PS_TKT_HIST.CUST_NO,
PS_TKT_HIST_LIN.ITEM_NO, PS_TKT_HIST_LIN.TKT_NO, PS_TKT_HIST.TKT_DAT, PS_TKT_HIST_LIN.LIN_TYP
FROM PS_TKT_HIST AS PS_TKT_HIST
INNER JOIN PS_TKT_HIST_LIN AS PS_TKT_HIST_LIN ON PS_TKT_HIST.EVENT_NO = PS_TKT_HIST_LIN.EVENT_NO AND PS_TKT_HIST.STR_ID = PS_TKT_HIST_LIN.STR_ID
AND PS_TKT_HIST.STA_ID = PS_TKT_HIST_LIN.STA_ID AND PS_TKT_HIST.TKT_NO = PS_TKT_HIST_LIN.TKT_NO
LEFT OUTER JOIN AR_CUST AS AR_CUST ON PS_TKT_HIST.CUST_NO = AR_CUST.CUST_NO
LEFT OUTER JOIN IM_ITEM AS IM_ITEM ON PS_TKT_HIST_LIN.ITEM_NO = IM_ITEM.ITEM_NO
WHERE
--(PS_TKT_HIST_LIN.LIN_TYP = 's') and (PS_TKT_HIST_LIN.LIN_TYP != 'r')
(PS_TKT_HIST_LIN.LIN_TYP <> 'U')
and (PS_TKT_HIST.TKT_DAT <= dateadd(day, datediff(day, 0, GETDATE())-7, 0))
ORDER BY PS_TKT_HIST.CUST_NO, PS_TKT_HIST_LIN.ITEM_NO, PS_TKT_HIST_LIN.TKT_NO


Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 15:06:07
The results are all the records.
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-15 : 15:08:51
Just add only this condition to the WHERE clause and check if the results you get are same as what you were expecting:
 (PS_TKT_HIST_LIN.LIN_TYP = 's') 



Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 15:08:58
Actually even tho I know the item says R now.. the history still has a 's' but if the same item number now has 'r' the 's' still comes up in query.
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 15:11:58
The item even tho currently an 'r' still shows up in report. I want to be able to cross reference if same item if an 's' has an 'r' Its like a library system.. check in check out.. s is out r is in.. if item is out.. it creates a -1 in inventory.. and history file.. I want to be able to check if item is -1 and an s and more than 7 days old and not currently an 'r' then it shows up in dashboard.



quote:
Originally posted by dinakar

Just add only this condition to the WHERE clause and check if the results you get are same as what you were expecting:
 (PS_TKT_HIST_LIN.LIN_TYP = 's') 



Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/

Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 15:42:05
The item even tho currently an 'r' still shows up in report. I want to be able to cross reference if same item if an 's' has an 'r' Its like a library system.. check in check out.. s is out r is in.. if item is out.. it creates a -1 in inventory.. and history file.. I want to be able to check if item is -1 and an s and more than 7 days old and not currently an 'r' then it shows up in dashboard.
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-15 : 23:38:20
Any answers yet? or am I in wrong forum?
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-05-15 : 23:48:49
Sorry I must've clicked the "mark all as read" and your post probably got missed..
It will be nice if I can see some data from each of the tables so I can create some tables with your data and run your queries against them. This helps me better understand what your issue is.

Dinakar Nethi
SQL Server MVP
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-16 : 02:20:17
look at my other post.. may find it better put together with just a question and looking for solutions there.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 11:30:27
Most probably a CASE SENSITIVE COLLATION

WHERE UCASE(PS_TKT_HIST_LIN.LIN_TYP) = UCASE('s')


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 11:38:53
OR...
Is this a history table? Where several records with the same PK can be stored?

Example: Order #1 is

1) Created
2) Backordered
3) Sent
4) Paid

This would create 4 records with same order number.

Is this what you mean?

SELECT		PS_TKT_HIST.EVENT_NO,
CONVERT(decimal(12, 2), PS_TKT_HIST_LIN.QTY_SOLD) AS QTY_SOLD,
AR_CUST.NAM,
PS_TKT_HIST.CUST_NO,
PS_TKT_HIST_LIN.ITEM_NO,
PS_TKT_HIST_LIN.TKT_NO,
PS_TKT_HIST.TKT_DAT,
PS_TKT_HIST_LIN.LIN_TYP
FROM PS_TKT_HIST
INNER JOIN PS_TKT_HIST_LIN ON PS_TKT_HIST.EVENT_NO = PS_TKT_HIST_LIN.EVENT_NO AND PS_TKT_HIST.STR_ID = PS_TKT_HIST_LIN.STR_ID AND PS_TKT_HIST.STA_ID = PS_TKT_HIST_LIN.STA_ID AND PS_TKT_HIST.TKT_NO = PS_TKT_HIST_LIN.TKT_NO
INNER JOIN (
SELECT ITEM_NO
FROM PS_TKT_HIST_LIN
GROUP BY ITEM_NO
HAVING MAX(LIN_TYP) = 's'
AND MIN(LIN_TYP) = 's'
) AS d ON d.ITEM_NO = PS_TKT_HIST_LIN.ITEM_NO
LEFT JOIN AR_CUST ON PS_TKT_HIST.CUST_NO = AR_CUST.CUST_NO
LEFT JOIN IM_ITEM ON PS_TKT_HIST_LIN.ITEM_NO = IM_ITEM.ITEM_NO
WHERE PS_TKT_HIST_LIN.LIN_TYP <> 'U'
AND PS_TKT_HIST.TKT_DAT <= DATEADD(day, DATEDIFF(day, 7, CURRENT_TIMESTAMP), 0)
ORDER BY PS_TKT_HIST.CUST_NO,
PS_TKT_HIST_LIN.ITEM_NO,
PS_TKT_HIST_LIN.TKT_NO


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-16 : 11:44:19
Ok Heres what I have now and my result.. .I have 20 records.. with only a few items right now for testing.
SQL:
SELECT MAX(VI_PS_TKT_HIST_LIN.TKT_NO) AS Expr3, VI_PS_TKT_HIST_LIN.ITEM_NO AS Expr1, VI_PS_TKT_HIST_LIN.ITEM_NO AS Expr2,
VI_TKT_HIST_CUST.CUST_NO, MIN(DISTINCT VI_PS_TKT_HIST_LIN.TKT_DAT) AS TKT_DAT, VI_AR_CUST_WITH_ADDRESS.NAM,
VI_PS_TKT_HIST_LIN.LIN_TYP, VI_IM_ITEM_WITH_INV.ITEM_NO, VI_IM_ITEM_WITH_INV.QTY_ON_HND
FROM VI_PS_TKT_HIST_LIN INNER JOIN
VI_TKT_HIST_CUST ON VI_PS_TKT_HIST_LIN.TKT_NO = VI_TKT_HIST_CUST.TKT_NO INNER JOIN
VI_AR_CUST_WITH_ADDRESS ON VI_TKT_HIST_CUST.CUST_NO = VI_AR_CUST_WITH_ADDRESS.CUST_NO INNER JOIN
VI_IM_ITEM_WITH_INV ON VI_PS_TKT_HIST_LIN.ITEM_NO = VI_IM_ITEM_WITH_INV.ITEM_NO
WHERE (VI_PS_TKT_HIST_LIN.LIN_TYP = 's')
GROUP BY VI_PS_TKT_HIST_LIN.ITEM_NO, VI_TKT_HIST_CUST.CUST_NO, VI_AR_CUST_WITH_ADDRESS.NAM, VI_PS_TKT_HIST_LIN.LIN_TYP,
VI_IM_ITEM_WITH_INV.ITEM_NO, VI_IM_ITEM_WITH_INV.QTY_ON_HND, VI_PS_TKT_HIST_LIN.TKT_DAT
HAVING (VI_PS_TKT_HIST_LIN.TKT_DAT <= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 7, 0))



Results:
1-100008 CM-1999-00001 CM-1999-00001 081969592031219 5/2/2007 12:00:00 AM Sean S CM-1999-00001 -1.0000
1-100014 CM-1999-00001 CM-1999-00001 081969592031219 5/3/2007 12:00:00 AM Sean S CM-1999-00001 -1.0000
1-100016 CM-1999-00001 CM-1999-00001 081969592031219 5/8/2007 12:00:00 AM Sean S CM-1999-00001 -1.0000
1-100019 CM-1999-00002 CM-1999-00002 081969592031219 5/1/2007 12:00:00 AM Sean S CM-1999-00002 -1.0000

Any Better Solution?

Originally trying to get last record that is 's' that it looks to see if its an 'r' at a later time.. Heck I dont know.. up til 4 last night.. and still not feeling close to it.
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-16 : 11:48:59

I ran this and got no results..



quote:
Originally posted by Peso

OR...
Is this a history table? Where several records with the same PK can be stored?

Example: Order #1 is

1) Created
2) Backordered
3) Sent
4) Paid

This would create 4 records with same order number.

Is this what you mean?

SELECT		PS_TKT_HIST.EVENT_NO,
CONVERT(decimal(12, 2), PS_TKT_HIST_LIN.QTY_SOLD) AS QTY_SOLD,
AR_CUST.NAM,
PS_TKT_HIST.CUST_NO,
PS_TKT_HIST_LIN.ITEM_NO,
PS_TKT_HIST_LIN.TKT_NO,
PS_TKT_HIST.TKT_DAT,
PS_TKT_HIST_LIN.LIN_TYP
FROM PS_TKT_HIST
INNER JOIN PS_TKT_HIST_LIN ON PS_TKT_HIST.EVENT_NO = PS_TKT_HIST_LIN.EVENT_NO AND PS_TKT_HIST.STR_ID = PS_TKT_HIST_LIN.STR_ID AND PS_TKT_HIST.STA_ID = PS_TKT_HIST_LIN.STA_ID AND PS_TKT_HIST.TKT_NO = PS_TKT_HIST_LIN.TKT_NO
INNER JOIN (
SELECT ITEM_NO
FROM PS_TKT_HIST_LIN
GROUP BY ITEM_NO
HAVING MAX(LIN_TYP) = 's'
AND MIN(LIN_TYP) = 's'
) AS d ON d.ITEM_NO = PS_TKT_HIST_LIN.ITEM_NO
LEFT JOIN AR_CUST ON PS_TKT_HIST.CUST_NO = AR_CUST.CUST_NO
LEFT JOIN IM_ITEM ON PS_TKT_HIST_LIN.ITEM_NO = IM_ITEM.ITEM_NO
WHERE PS_TKT_HIST_LIN.LIN_TYP <> 'U'
AND PS_TKT_HIST.TKT_DAT <= DATEADD(day, DATEDIFF(day, 7, CURRENT_TIMESTAMP), 0)
ORDER BY PS_TKT_HIST.CUST_NO,
PS_TKT_HIST_LIN.ITEM_NO,
PS_TKT_HIST_LIN.TKT_NO


Peter Larsson
Helsingborg, Sweden

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 11:53:24
YOU WANT LAST 'S' RECORD BASED ON WHAT?
WE CAN'T GUESS...

Please post proper sample data and expected output.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-16 : 11:54:33
Based on the item number and the last ticket number.. does that help?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 12:01:13
[code]SELECT h.EVENT_NO,
CONVERT(decimal(12, 2), l.QTY_SOLD) AS QTY_SOLD,
AR_CUST.NAM,
h.CUST_NO,
l.ITEM_NO,
l.TKT_NO,
h.TKT_DAT,
l.LIN_TYP
FROM PS_TKT_HIST AS h
INNER JOIN PS_TKT_HIST_LIN AS l ON l.EVENT_NO = h.EVENT_NO AND l.STR_ID = h.STR_ID AND l.STA_ID = h.STA_ID AND l.TKT_NO = h.TKT_NO
INNER JOIN (
SELECT ITEM_NO,
MAX(TKT_NO) AS TKT_NO
FROM PS_TKT_HIST_LIN
GROUP BY ITEM_NO
) AS d ON d.ITEM_NO = l.ITEM_NO AND d.TKT_NO = l.TKT_NO
LEFT JOIN AR_CUST AS c ON c.CUST_NO = h.CUST_NO
LEFT JOIN IM_ITEM AS i ON i.ITEM_NO = l.ITEM_NO
WHERE l.LIN_TYP <> 'U'
AND h.TKT_DAT <= DATEADD(day, DATEDIFF(day, 7, CURRENT_TIMESTAMP), 0)
ORDER BY h.CUST_NO,
l.ITEM_NO,
l.TKT_NO[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-05-16 : 12:51:36
That worked.. under the DATEDIFF If i wanted to say more than 3 days but less than the 7 would it be:
h.TKT_DAT >= DATEADD(day, DATEDIFF(day, 3, CURRENT_TIMESTAMP), 0),
h.TKT_DAT <= DATEADD(day, DATEDIFF(day, 7, CURRENT_TIMESTAMP), 0)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-17 : 01:12:12
h.TKT_DAT > DATEADD(day, DATEDIFF(day, 3, CURRENT_TIMESTAMP), 0),
h.TKT_DAT < DATEADD(day, DATEDIFF(day, 7, CURRENT_TIMESTAMP), 0)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

soonerX
Starting Member

16 Posts

Posted - 2007-06-06 : 13:52:08
ok this worked.. now how do I add a count to it.. Say if I wanted to add what up all the records that this equaled instead of displaying columns.. jsut the number that items fell under.
Go to Top of Page
   

- Advertisement -