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 2008 Forums
 Transact-SQL (2008)
 Query help

Author  Topic 

neditheg
Starting Member

3 Posts

Posted - 2014-01-30 : 05:32:24
I have 3 tables
CREATE TABLE [dbo].[tblCRMA]
(
[ClientId] [int] IDENTITY(1,1) NOT NULL,
[MCC_AgentId] [int] NULL,
[MCC_ClientId] [int] NULL,
[ImportSessionId] [int] NULL,
[DateInserted] [datetime] NULL,
[DateModified] [datetime] NOT NULL,
[MCC_LastCall] [datetime] NULL,
[MCC_LastCallCode] [int] NULL,
[PdV_transanti_nel_periodo] [nvarchar](255) NULL,
[TSR] [nvarchar](255) NULL,
[Sales_Yes_No] [nvarchar](255) NULL,
[Cluster] [nvarchar](255) NULL,
[Next_Call] [nvarchar](255) NULL,
[ATTIVI_AL_27_01] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_5] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_10] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_20] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_5e10] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_10e20] [nvarchar](255) NULL,
[da_chiamare_per_taglio_da_5e20] [nvarchar](255) NULL,
[da_chiamare_per_tutti_i_tagli] [nvarchar](255) NULL,
[ESITO] [nvarchar](2000) NULL,
[MOTIVAZIONI_non_ordina_prod_carenti] [nvarchar](2000) NULL,
[Note] [nvarchar](255) NULL
)
--------------------------------------------------------------

CREATE TABLE [dbo].[tblCRMA_History]
(
[tblHistoryId] [int] IDENTITY(1,1) NOT NULL,
[ClientId] [int] NOT NULL,
[FieldId] [int] NOT NULL,
[FieldName] [varchar](128) NULL,
[FieldCrmColumn] [nvarchar](255) NULL,
[ValueOld] [nvarchar](max) NULL,
[ValueNew] [nvarchar](max) NULL,
[DateUpdated] [datetime] NOT NULL,
[UpdatedByUserId] [int] NULL
)
------------------
CREATE TABLE [dbo].[agents]
(
[AGENTID] [int] NOT NULL DEFAULT ('-1'),
[AGENTNAME] [varchar](255) NOT NULL DEFAULT (''),
[AGENTPIN] [varchar](30) NOT NULL DEFAULT (''),
[AGENTGROUP] [varchar](20) NULL DEFAULT (NULL),
[NUMBERPATTERN] [varchar](120) NULL DEFAULT (NULL),
[ISDELETED] [int] NOT NULL DEFAULT ('0'),
[SUPERVISORID] [int] NOT NULL DEFAULT ('-1'),
[RIGHTS] [int] NOT NULL DEFAULT ('0'),
[SUPERVISORRIGHTS] [int] NOT NULL DEFAULT ('0'),
[LOGGEDSERVER] [int] NOT NULL DEFAULT ('-1'),
[DefaultSkill] [int] NOT NULL DEFAULT ((5)),
[Details] [varchar](250) NULL DEFAULT (''),
[SIP_Realm] [varchar](50) NULL DEFAULT (NULL),
[SIP_HA1] [varchar](32) NULL DEFAULT (NULL),
[SIP_Auth] [int] NULL DEFAULT ((0)),
[PBX_Extension] [varchar](50) NULL DEFAULT (NULL)
)

The link between this tables are:

tblCRMA.clientid=tblCRMA_History.clientid
tblCRMA_History.updatedbyuserid=agents.agentid


I need to select all the fileds from tblCRMA

where the column "esito" is not blank or null and to add to this result "agentname" column from the agents table and the "dateupdated"
from the tblCRMA_History table.


My big problem is that in the tblCRMA_History are more rows for a tblCRMA.clientid.

so

select * from tblCRMA where clientid= 1
returns one row

but
select * from tblCRMA_History where clientid=1 returns more rows,
one for every field modified in tblCRMA

I don't know how to add on my first select only the info I need.

If you guys need more informations I'm ready to write it!

Thanks!!


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-30 : 06:54:22
so how do you want to show those multiple dateupdated values for a given clientid? you can show only a single value if you want one row per clientid. So you've determine what value to choose ie whether its earliest or latest dateupdated value for a given clientid. Alternatively you can show all associated values in delimited format

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

neditheg
Starting Member

3 Posts

Posted - 2014-01-30 : 06:58:42
The value of "dateupdated" is the same for a specified clientid so, I need to take the first record from the tblCRMA_History table for each clientid.

Thanks!
Go to Top of Page

neditheg
Starting Member

3 Posts

Posted - 2014-02-03 : 05:05:51
I've found another way... please close this topic!

Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-02-03 : 06:28:23
quote:
Originally posted by neditheg

I've found another way... please close this topic!

Thanks!


You can edit the title and append [SOLVED] at the end

Madhivanan

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

- Advertisement -