Showing posts with label duplicate. Show all posts
Showing posts with label duplicate. Show all posts

Tuesday, March 27, 2012

Corelated Sub query

Hi,

I'm using this query, It works fine, except that i want to show PayType. When i tried to show it, I'll get duplicate records

What can i do?

Code Snippet

SELECT tbl_TransPayLnk.TransPayID, tbl_TransPayLnk.TransDate, tbl_TransPayLnk.Operator, tbl_TransPayLnk.Flagged, tbl_TransPayLnk.Remarks,

tbl_TransPayLnk.RemarksDate, tbl_Transaction.RefNo, tbl_Transaction.TransAmount, tbl_TransPayLnk.TransNo AS Expr1, tbl_TransPayLnk.PayID,

tbl_FundCode.FundDescription

FROM tbl_TransPayLnk LEFTOUTERJOIN

tbl_FundCode INNERJOIN

tbl_Transaction ON tbl_FundCode.FundCodeID = tbl_Transaction.FundCodeID ON tbl_TransPayLnk.TransNo = tbl_Transaction.TransNo

WHEREEXISTS

(SELECT PaymentID, PayID, PayType, Amount

FROM tbl_Payment AS tbl_Payment_1

WHERE(PayID = tbl_TransPayLnk.PayID))

Thanks

Prince:

I assume by your post that to add "PayType" that you need to also join to the tbl_Payment table. I would normally expect that the amounts would vary from record to record in that table. Also, I question the use of showing the "payType" without also showing the amount. If all you truely want is the "PayType" then changing your "SELECT" to a "SELECT DISTINCT" might be in order. Also, experiment with adding the AMOUNT and possibly the "PaymentID" to the data returned and see if the data makes more "sense" that way.

Kent

|||

Prince

Try this:

Code Snippet

SELECT tbl_TransPayLnk.TransPayID, tbl_TransPayLnk.TransDate, tbl_TransPayLnk.Operator,

tbl_TransPayLnk.Flagged, tbl_TransPayLnk.Remarks,

tbl_TransPayLnk.RemarksDate, tbl_Transaction.RefNo, tbl_Transaction.TransAmount,

tbl_TransPayLnk.TransNo AS Expr1, tbl_TransPayLnk.PayID,

tbl_FundCode.FundDescription,

tbl_Payment_1.PayType

FROM tbl_TransPayLnk

LEFTOUTERJOIN tbl_FundCode

INNERJOIN tbl_Transaction

ON tbl_TransPayLnk.TransNo = tbl_Transaction.TransNo

INNERJOIN

(

SELECTDISTINCT PayID, PayType

FROM tbl_Payment

)AS tbl_Payment_1

ON tbl_TransPayLnk.PayID = tbl_Payment_1.PayID

P.S. Some sample DDL and sample data would sure make it easier to assist you....

|||

Hi,

Right i tried your solution and got an error :

Code Snippet

Msg 102, Level 15, State 1, Line 31

Incorrect syntax near 'PayID'.

As requested here are table structure and data:

Tbl_Transaction

Code Snippet

TransactionID int Unchecked
TransNo varchar(50) Checked
RefNo nvarchar(50) Checked
FundCodeID smallint Checked
TransAmount decimal(18, 2) Checked
Description varchar(100) Checked

Data in Tbl_Transaction

Code Snippet

TransactionID TransNo RefNo FundCodeID TransAmount Description 13 1001 20013808 1 28.55 14 1001 34983249 2 9.31


Tbl_TransPayLnk

Code Snippet

TransPayID int Unchecked
PayID int Checked
TransNo varchar(50) Checked
TransDate datetime Checked
Operator char(5) Checked
TerminalID tinyint Checked
Flagged char(1) Unchecked
Remarks text Checked
RemarksDate datetime Checked
TPayment decimal(18, 2) Checked

Data Tbl_TransPayLnk

Code Snippet

TransPayID PayID TransNo TransDate Operator TerminalID Flagged Remarks RemarksDate TPayment 5 1 1001 09/05/2007 02 2 V hi there 13/06/2007 37.86 6 1003 1002 09/05/2007 02 2 N 73.33


Tbl_Payment

Code Snippet

PaymentID int Unchecked
PayID int Checked
PayType varchar(50) Checked
Amount decimal(18, 2) Checked

Data Tbl_Payment

Code Snippet


PaymentID PayID PayType Amount 17 1 Cheque 30 18 1 Cash 7.86 19 1003 Cheque 73.33

And this is what i would need in one ROW

TransPayID TransDate Operator Flagged Remarks RemarksDate RefNo TransAmount TransNo PayID FundDescription 5 09/05/2007 02 V hi there 13/06/2007 20013808 28.55 1001 1 Rent 5 09/05/2007 02 V hi there 13/06/2007 34983249 9.31 1001 1 Sundry Debtor 6 09/05/2007 02 N 20013808 73.33 1002 1003 TV Licence

the only thing i want more in there is PAY TYPE without duplicating the rows. I mean as it is.

If i would add Payment tbl regardless of the joins i would be duplicating ROW 5 2 more times. so .......

Which would be cheque or cash.


Any ideas.

Thanks

|||

Which row out of tbl_Payment do you want to be in the result?

How do you determine that that row should be it?

Do you just want one, you don't care which?

Or the highest amount?

|||

Hi,

Well

TransPayID TransDate Operator Flagged Remarks RemarksDate RefNo TransAmount TransNo PayID FundDescription 5 09/05/2007 02 V hi there 13/06/2007 20013808 28.55 1001 1 Rent 5 09/05/2007 02 V hi there 13/06/2007 34983249 9.31 1001 1 Sundry Debtor 6 09/05/2007 02 N 20013808 73.33 1002 1003 TV Licence

If you look at this i'm getting PayID which is one. Against one in tbl_payment there is PayType. I just want PayType so cheque or cash would be against record PayID = 1

Thanks

|||

Prince:

What is the complete key to the tbl_payment table? Also, doesn't the fact that the LEFT JOIN table participate in a subsequent inner join:

Code Snippet

INNERJOIN

tbl_Transaction ON tbl_FundCode.FundCodeID = tbl_Transaction.FundCodeID ON tbl_TransPayLnk.TransNo = tbl_Transaction.TransNo

turn the LEFT JOIN into a defacto INNER JOIN?

|||

Hello Kent,

The primary key for tbl_Payment is PaymentID but that is not how i join. I joined it to tbl_transPayLnk using PayID which is copied in both these tables.

I don't know but what else can i do?

Read my long post and you get the idea of what i want?

Thanks

|||

Prince

I commented out the reference to the FundCode table since you didn't provide the data.

Just uncomment it and add your ON clause to include that data.

Code Snippet

createtable tbl_Transaction (

TransactionID int,

TransNo varchar(50),

RefNo nvarchar(50),

FundCodeID smallint,

TransAmount decimal(18, 2),

Description varchar(100)

)

createtable tbl_TransPayLnk (

TransPayID int,

PayID int,

TransNo varchar(50),

TransDate datetime,

Operator char(5),

TerminalID tinyint,

Flagged char(1),

Remarks text,

RemarksDate datetime,

TPayment decimal(18, 2)

)

createtable tbl_Payment (

PaymentID int,

PayID int,

PayType varchar(50),

Amount decimal(18, 2)

)

setdateformat dmy

insertinto tbl_Transaction values(13,'1001','20013808', 1, 28.55,null)

insertinto tbl_Transaction values(14,'1001','34983249', 2, 9.31,null)

insertinto tbl_TransPayLnk values(5, 1,'1001','09/05/2007','02', 2,'V','hi there','13/06/2007', 37.86)

insertinto tbl_TransPayLnk values(6, 1003,'1002','09/05/2007','02', 2,'N',null,null, 73.33)

insertinto tbl_Payment values(17, 1,'Cheque', 30)

insertinto tbl_Payment values(18, 1,'Cash', 7.86)

insertinto tbl_Payment values(19, 1003,'Cheque', 73.33)

SELECT tbl_TransPayLnk.TransPayID, tbl_TransPayLnk.TransDate, tbl_TransPayLnk.Operator,

tbl_TransPayLnk.Flagged, tbl_TransPayLnk.Remarks,

tbl_TransPayLnk.RemarksDate, tbl_Transaction.RefNo, tbl_Transaction.TransAmount,

tbl_TransPayLnk.TransNo AS Expr1, tbl_TransPayLnk.PayID,

--tbl_FundCode.FundDescription,

tbl_Payment_1.PayType

FROM tbl_TransPayLnk

--LEFT OUTER JOIN tbl_FundCode

INNERJOIN tbl_Transaction

ON tbl_TransPayLnk.TransNo = tbl_Transaction.TransNo

INNERJOIN

(

SELECTDISTINCT PayID, PayType

FROM tbl_Payment

)AS tbl_Payment_1

ON tbl_TransPayLnk.PayID = tbl_Payment_1.PayID

and tbl_Payment_1.PayType =

(selecttop 1 paytype from tbl_Payment

where tbl_TransPayLnk.PayID = tbl_Payment_1.PayID

)

|||

Hello Dale,

Thank you very much. I checked the code and although it does bring up the records. It only brought back Cheque even for the records that have cash in tbl_payment.

If it works fine, it should have brought cash and cheque for PayID= 1 which it didn't


Any ideas why?

Thanks I really appreciate your help.

|||OK; that helps. Now, are you expecting two lines to be displayed when there are two different pay types or are you wanting something like, 'Cheque, Cash' where the results are strung together? (Please forgive me, Dale)|||

That's where I'm confused on what you're looking for.

How exactly do you want PayType to appear in the result?

|||

There are only 2 pay types so it should appear as it is:

so for PayID =1 both cheque and cash should be displayed but without repeating the record 4 times.

Because rightnow it would do this if you omitt your last query. 2 cash and 2 for cheque for PayID=1

|||

"... Now, are you expecting two lines to be displayed when there are two different pay types or are you wanting something like, 'Cheque, Cash' where the results are strung together? ..."

(Like I said: Please forgive me, Dale... *sigh* )

|||

Code Snippet

createfunction dbo.GetPayType ( @.PayID asint)

returnsvarchar(200)

as

begin

declare @.pt varchar(200)

select @.pt =coalesce( @.pt +',','')+ paytype

from tbl_Payment where payid = @.PayID

return @.pt

end

SELECT tbl_TransPayLnk.TransPayID, tbl_TransPayLnk.TransDate, tbl_TransPayLnk.Operator,

tbl_TransPayLnk.Flagged, tbl_TransPayLnk.Remarks,

tbl_TransPayLnk.RemarksDate, tbl_Transaction.RefNo, tbl_Transaction.TransAmount,

tbl_TransPayLnk.TransNo AS Expr1, tbl_TransPayLnk.PayID,

--tbl_FundCode.FundDescription,

dbo.GetPayType(tbl_TransPayLnk.PayID)as PayType

FROM tbl_TransPayLnk

--LEFT OUTER JOIN tbl_FundCode

INNER JOIN tbl_Transaction

ON tbl_TransPayLnk.TransNo = tbl_Transaction.TransNo

Sunday, March 11, 2012

Copying database to new name on same server

Dear Y'all -
I'm a newbie to this list. I have a database of about 2Gb that I need
to duplicate several times, giving each duplicate a new name, on to a
single server. Sounds simple is proving to not be so. The basic
export command seems to do what I want but it runs for longer than I'm
willing to wait, over 6 hours. It takes less than an hour to create
the database from scratch.
Is there something I'm missing? Is there a way to duplicate a
database at the file system level and them update the DBMS system
tables (or whatever) so the new files are recognized properly.
TIA.
Yours - Billy
============================================================ William Goedicke goedicke@.goedsole.com
http://www.goedsole.com:8080
============================================================
Lest we forget:
Don't force it; get a bigger hammer.
- Al DodgeDo a backup once, then do multiple restores (during a restore you can
specify a new name, and it will take care of data file names etc. as well).
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"William Goedicke" <wgoedick@.reveregroup.com> wrote in message
news:m38ylcofqg.fsf@.mail.goedsole.com...
> Dear Y'all -
> I'm a newbie to this list. I have a database of about 2Gb that I need
> to duplicate several times, giving each duplicate a new name, on to a
> single server. Sounds simple is proving to not be so. The basic
> export command seems to do what I want but it runs for longer than I'm
> willing to wait, over 6 hours. It takes less than an hour to create
> the database from scratch.
> Is there something I'm missing? Is there a way to duplicate a
> database at the file system level and them update the DBMS system
> tables (or whatever) so the new files are recognized properly.
> TIA.
> Yours - Billy
> ============================================================> William Goedicke goedicke@.goedsole.com
> http://www.goedsole.com:8080
> ============================================================> Lest we forget:
> Don't force it; get a bigger hammer.
> - Al Dodge|||When you do the restores you will need to specify new paths for the data nd
lof files as well. You can do this in Enterprise Manager or use the WITH
MOVE option in Query Analyzer.
Rand
This posting is provided "as is" with no warranties and confers no rights.|||In my last post it should have been:
When you do the restores you will need to specify new paths for the data
and
log files as well. You can do this in Enterprise Manager or use the WITH
MOVE option in Query Analyzer.
This posting is provided "as is" with no warranties and confers no rights.|||If you use EM for the restore, the filenames get changed wrt the new name
automatically, at least in my experience.
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Rand Boyd [MS]" <rboyd@.onlinemicrosoft.com> wrote in message
news:rD7WE2KxDHA.3660@.cpmsftngxa07.phx.gbl...
> When you do the restores you will need to specify new paths for the data
nd
> lof files as well. You can do this in Enterprise Manager or use the WITH
> MOVE option in Query Analyzer.
> Rand
> This posting is provided "as is" with no warranties and confers no rights.
>

Wednesday, March 7, 2012

Copying a report to another report in same solution

How can I copy a report and all its supporing queries and make a duplicate so that report1 and report2 are duplicates.

Report 2 will be little different than report1 but the basic queries and parameters will all the same. I can then edit the report layout to what the new report will look like. I do not want to have to cut/paste one object at a time.

Is there a better way?

You can do this directly from within Visual Studio.

Right click on the report you want to copy (in Solution Explorer) and select 'Copy' (or you can just highlight your report and hit Ctrl+C). Then press Ctrl+V. This will create a new duplicate report with 'Copy of ' prefixed on the report name.

Hope this helps.

Jarret

|||Copy does not work as there is no "Paste" function. The Ctl C/V works just fine. Thank you.

Friday, February 24, 2012

Copy users between databases

Hello All,
I used DTS to copy one database to a blank database for a backup of the
database. A duplicate database.
How do I copy over the user logins to the new database.
This did not happen with DTS.
The databases are on the same instance of SQL.
Any help appreciated.
Thanks,
Terry
tgwillett@.cox.netRun this on the database to autofix users
select 'exec sp_change_users_login ''Auto_Fix'', ''' + name + '''' from
sysusers
where name not in ('INFORMATION_SCHEMA', 'dbo', 'guest', 'public')
"Terry" wrote:

> Hello All,
> I used DTS to copy one database to a blank database for a backup of the
> database. A duplicate database.
> How do I copy over the user logins to the new database.
> This did not happen with DTS.
> The databases are on the same instance of SQL.
> Any help appreciated.
> Thanks,
> Terry
> tgwillett@.cox.net
>
>

Copy users between databases

Hello All,
I used DTS to copy one database to a blank database for a backup of the
database. A duplicate database.
How do I copy over the user logins to the new database.
This did not happen with DTS.
The databases are on the same instance of SQL.
Any help appreciated.
Thanks,
Terry
tgwillett@.cox.netThis is a pretty good article on it:
http://support.microsoft.com/defaul...kb;en-us;246133
Christian
"Terry" <tgwillett@.cox.net> wrote in message
news:r_62g.56456$gE.9859@.dukeread06...
> Hello All,
> I used DTS to copy one database to a blank database for a backup of the
> database. A duplicate database.
> How do I copy over the user logins to the new database.
> This did not happen with DTS.
> The databases are on the same instance of SQL.
> Any help appreciated.
> Thanks,
> Terry
> tgwillett@.cox.net
>

Copy users between databases

Hello All,
I used DTS to copy one database to a blank database for a backup of the
database. A duplicate database.
How do I copy over the user logins to the new database.
This did not happen with DTS.
The databases are on the same instance of SQL.
Any help appreciated.
Thanks,
Terry
tgwillett@.cox.netThis is a pretty good article on it:
http://support.microsoft.com/default.aspx?scid=kb;en-us;246133
Christian
"Terry" <tgwillett@.cox.net> wrote in message
news:r_62g.56456$gE.9859@.dukeread06...
> Hello All,
> I used DTS to copy one database to a blank database for a backup of the
> database. A duplicate database.
> How do I copy over the user logins to the new database.
> This did not happen with DTS.
> The databases are on the same instance of SQL.
> Any help appreciated.
> Thanks,
> Terry
> tgwillett@.cox.net
>