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.
| Author |
Topic |
|
Sunny155
Starting Member
15 Posts |
Posted - 2009-03-31 : 13:45:48
|
Hi guys, I am new here. I need help fixing a query or maybe need advise to write it diffrenetly.I have 5 tables in a database - Transactions, Product, Service, ServProvider and GlbServiceD. I want to query the transaction, product, service, servprovidet table based on unique id of service table. Service table has got 8 records so 8 ids. I want to insert the result of the qury into 5th table.The query is like this (The joins are self explanatory)-DECLARE @LoopID as intDECLARE @MaxLoopID as intSET @LoopId = 1SET @MaxLoopID = (SELECT Max(nid) FROM dbo.service WHERE nid <> 1000)WHILE @LoopID <= @MaxLoopIDBEGININSERT INTO dbo.GlbServiceDSELECT getdate()-1 as dDay, Min(@LoopID) as nServiceID, count(T.sOpType) as nTransaction, sum(T.nValue) as nCashinFlow , sum(T.nRevenue) as nRevenue1 FROM dbo.Transactions AS TINNER JOINdbo.Product AS P ON T.nProductID = P.nIDINNER JOINdbo.ServProvider AS SP ON P.nServProvID = SP.nIDINNER JOINdbo.Service AS S ON SP.nServiceID = S.nIDWHERE T.sOptype = 'transaction' and datediff(day, T.dInsertion, getdate()-1) = 0 AND T.nErrorCodeId = 0 AND S.nID = @LoopIDSET @LoopID = @LoopID+1END; The select results this value if no records match -dDay = 2009-03-30 23:08:26.700 - (sample)nServiceID = NULL ----- (PLEASE NOTE)nTransaction = 0nCashinFlow = NULLnRevenue1 = NULLactuallu i want to set the value of nServiceID to the id of service id which is currently in loop (like if id 1 is in loop than 1, 2 than 2) and insert 8 records (bcos service table has 8 records)So the fixed qury will do something like this -Record 1 (where data found and not null )dDay = 2009-03-30 23:08:26.700 - (sample)nServiceID = 1 nTransaction = 5nCashinFlow = 100nRevenue1 = 1000Record 2 (where data not found hence null)dDay = 2009-03-30 23:08:26.700 - (sample)nServiceID = 2 ----- (PLEASE NOTE)nTransaction = 0nCashinFlow = NULLnRevenue1 = NULLRecord 3 (where data not found hence null)dDay = 2009-03-30 23:08:26.700 - (sample)nServiceID = 3 ----- (PLEASE NOTE)nTransaction = 0nCashinFlow = NULLnRevenue1 = NULLRecord 4 (where data found and not null)dDay = 2009-03-30 23:08:26.700 - (sample)nServiceID = 4nTransaction = 3nCashinFlow = 600nRevenue1 = 2000and etc upto total 8 recordsplease help fix this qury or suggest a new one.PS - I intend to use this query in conjunction with Scheduler and SQL Server Job so making it as stored procedure a good idea? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-31 : 13:52:50
|
seems like this is what u wantINSERT INTO dbo.GlbServiceDSELECT getdate()-1 as dDay, S.nID as nServiceID, count(T.sOpType) as nTransaction, sum(T.nValue) as nCashinFlow , sum(T.nRevenue) as nRevenue1 FROM dbo.Transactions AS TINNER JOINdbo.Product AS P ON T.nProductID = P.nIDINNER JOINdbo.ServProvider AS SP ON P.nServProvID = SP.nIDINNER JOINdbo.Service AS S ON SP.nServiceID = S.nIDWHERE T.sOptype = 'transaction' and datediff(day, T.dInsertion, getdate()-1) = 0 AND T.nErrorCodeId = 0 group by S.nID |
 |
|
|
Sunny155
Starting Member
15 Posts |
Posted - 2009-03-31 : 14:03:09
|
| when i added group by S.nID and replaced Min(@LoopID) with S.nid the query didnt displayed a thing i..e empty row with no data. and i dont want that. |
 |
|
|
Sunny155
Starting Member
15 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-01 : 12:51:30
|
quote: Originally posted by Sunny155 A picture says thousand words -Before making changes using your code -http://img90.imageshack.us/my.php?image=captureaoq.jpgAfter using your code -http://img90.imageshack.us/my.php?image=capture2g.jpgPlease not i have removed INSERT INTO statement just to show u output of select.
you're not using the query i gave you. i can still see DECLARE @LoopID....see what i posted, it just contains only a single insert...selectwhat does below return?SELECT getdate()-1 as dDay, S.nID as nServiceID, count(T.sOpType) as nTransaction, sum(T.nValue) as nCashinFlow , sum(T.nRevenue) as nRevenue1 FROM dbo.Transactions AS TINNER JOINdbo.Product AS P ON T.nProductID = P.nIDINNER JOINdbo.ServProvider AS SP ON P.nServProvID = SP.nIDINNER JOINdbo.Service AS S ON SP.nServiceID = S.nIDWHERE T.sOptype = 'transaction' and datediff(day, T.dInsertion, getdate()-1) = 0 AND T.nErrorCodeId = 0 group by S.nID |
 |
|
|
|
|
|
|
|