I have two SQL Server queries, and I need to display final output result in a C# gridview:
Select *
From PayBack
Where DATEDIFF(day, GetDate(), (Expirydate)) < 0
and DATEPART(yyyy, Expirydate) = '2018'
and DATEPART(Month,Expirydate) = '02'
Select Nomclient, Numero1, Numero2, Numero3, Email1, Email2
From Client
Where Nomclient In (Select Client from PayBack)
How to merge this into one query?
Try this -
Select PB.*,C.Nomclient,C.Numero1,C.Numero2,C.Numero3,C.Email1,C.Email2
From PayBack as PB
Inner Join Client as C on C.Nomclient = PB.Client
Where DATEDIFF(day,GetDate(),(PB.Expirydate))<0 and
DATEPART(yyyy,PB.Expirydate) = '2018' and DATEPART(Month,PB.Expirydate)='02'
Select
Nomclient,Numero1,Numero2,Numero3,Email1,Email2
from
Client
where
Nomclient in (
Select Client
From PayBack
where
DATEDIFF(day,GetDate(),(Expirydate))<0 and DATEPART(yyyy,Expirydate) = '2018'
and DATEPART(Month,Expirydate)='02'
)
Related
I tried to update a column in one table from another table using join condition. I tried all possible ways and its showing SQL command not properly ended.I am trying t his in sql server.
update a
set a.col4=b.col4
from dummy_jd a
join --select * from dummy_jd a,
(select col1,col2,col3,col4,col5,sum(reported_amount) reported_amount
from dummy_jd_2
where col5=3843
and col3='abc'
and col1=4
and col2=3002
group by col1,col2,col3,col4,col5
) b on (a.col2=b.col5 and a.col4=b.col3)
where a.col1=9;
This is which i tried
Try below -
update a set a.col4=b.col4 from dummy_jd a
join
(select col1,col2,col3,col4,col5,sum(reported_amount) reported_amount
from dummy_jd_2 where col5=3843 and col3='abc' and col1=4 and col2=3002
group by col1,col2,col3,col4,col5
)b on a.col2=b.col5 and a.col4=b.col3
where a.col1=9
Thanks to everyone i achieved it by using for loopand joins below is the query i used
for i in (select * from dummy_jd1)
loop
update dummy_jd a set a.col4 = (select distinct col4 from dummy_jd_2 b
where /*(jnl_ref_no, bill_ref_no) in (select
jnl_ref_no,bill_ref_no from dummy_jd1) and*/
a.col2 = b.col5
and a.col4 = b.col3
and b.col5 = i.col2
and b.col3 = i.col3
and id_type = 4
--and id_value in(3002,3019)
and reported_amount <> 0
group by col1,col2,col3,col4,col5)
where a.id_type = 9
and a.col2=i.col2
and a.col4=i.col3;
I need to join these two tables. I need to select occurrences where:
ex_head of_family_active = 1 AND tax_year = 2017
and also:
ex_head of_family_active = 0 AND tax_year = 2016
The first time I tried to join these two tables I got the warehouse data
dbo.tb_master_ascend AND warehouse_data.dbo.tb_master_ascend in the from clause have the same exposed names. As the query now shown below, I get a syntax error on the "where". What am I doing wrong? Thank you
use [warehouse_data]
select
parcel_number as Account,
pact_code as type,
owner_name as Owner,
case
when ex_head_of_family_active >= 1
then 'X'
else ''
end 'Head_Of_Fam'
from
warehouse_data.dbo.tb_master_ascend
inner join
warehouse_data.dbo.tb_master_ascend on parcel_number = parcel_number
where
warehouse_data.dbo.tb_master_ascend.tax_year = '2016'
and ex_head_of_family_active = 0
where
warehouse_data.dbo.tb_master_ascend.t2.tax_year = '2017'
and ex_head_of_family_active >= 1
and (eff_from_date <= getdate())
and (eff_to_date is null or eff_to_date >= getdate())
#marc_s I changed the where statements and updated my code however the filter is not working now:
use [warehouse_data]
select
wh2.parcel_number as Account
,wh2.pact_code as Class_Type
,wh2.owner_name as Owner_Name
,case when wh2.ex_head_of_family_active >= 1 then 'X'
else ''
end 'Head_Of_Fam_2017'
from warehouse_data.dbo.tb_master_ascend as WH2
left join warehouse_data.dbo.tb_master_ascend as WH1 on ((WH2.parcel_number = wh1.parcel_number)
and (WH1.tax_year = '2016')
and (WH1.ex_head_of_family_active is null))
where WH2.tax_year = '2017'
and wh2.ex_head_of_family_active >= 1
and (wh2.eff_from_date <= getdate())
and (wh2.eff_to_date is null or wh2.eff_to_date >= getdate())
I would use a CTE to get all your parcels that meet your 2016 rules.
Then join that against your 2017 rules on parcel ID.
I'm summarizing:
with cte as
(
select parcelID
from
where [2016 rules]
group by parcelID --If this isn't unique you will cartisian your results
)
select columns
from table
join cte on table.parcelid=cte.parcelID
where [2017 rules]
select a.dda_pk
from direct_table a
where a.dda_type = 'B'
and a.dda_status = 'D'
and a.dda_location = '01'
group by a.dda_emp_idno
having dda_pk < max(a.dda_pk)
and a.dda_status = 'D'
and a.dda_location = '01'
Column 'direct_deposit_audit.dda_pk' is invalid in the HAVING clause
because it is not contained in either an aggregate function or the
GROUP BY clause.
This is based on a assumption that the SQL returns rows for each dda_emp_idno
where dda_pk is smaller than the maximum dda_pk for that dda_emp_idno.
This should do the same thing in SQL Server:
select dda_pk
from (
select
dda_pk,
dense_rank() over (partition by dda_emp_idno order by dda_pk desc) as RN
from
direct_table a
where
a.dda_type = 'B'
and a.dda_status = 'D'
and a.dda_location = '01'
) X
where RN > 1
All the elements in group by must be in select clause so you can try this:
select a.dda_pk
from direct_table a
where a.dda_type = 'B'
and a.dda_status = 'D'
and a.dda_location = '01'
group by a.dda_pk
having a.dda_pk < max(a.dda_pk)
and a.dda_status = 'D'
and a.dda_location = '01'
Without having dug into this query, let me just note that Sybase ASE has much looser semantics around the GROUP BY than most other databases. This means that queries with GROUP BY that run iN ASE may raise an error in other databases.
For more info, see http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.help.ase.15.7/title.htm
What I'm basically trying to do is fire off multiple SQL statements in one go.
This works fine as long as they don't return results.
What I want to do is make a temporary table fill it and join it on my existing data:
CREATE TABLE #JaarMaandTable(jaarm int,maandm int)
INSERT INTO #JaarMaandTable (jaarm,maandm) VALUES (2013,9), (2013,10), (2013,11)
SELECT jaarm,maandm, kr.*
FROM #JaarMaandTable jm
LEFT JOIN (
SELECT DATEPART(Month, datum) as maand, DATEPART(Year, datum) as jaar ,count(*) as regels mytable
FROM agenda
WHERE datum >= '20130901'
AND datum <= '20131130'
GROUP BY DATEPART(Year, datum), DATEPART(Month, datum)
)kr ON jm.jaarm = kr.jaar AND jm.maandm = kr.maand ORDER BY jaarm, maandm
This is to make use of a temp table to split up results in months even when there's no data for those months.
It works fine in query analyser.
When I try to use "open" on this query, it tells me it doesn't return a cursor.
When I "execsql" it, it won't return results.
When I split it up, it immediately forgets the #temptable.
You can write the query using a with statement, to avoid the need for a temporary table:
with JaarMaandTable(jaarm int,maandm int) as (
select 2013, 9 union all
select 2013, 10 union all
select 2013, 11
)
SELECT jaarm,maandm, kr.*
FROM JaarMaandTable jm
LEFT JOIN (
SELECT DATEPART(Month, datum) as maand, DATEPART(Year, datum) as jaar ,count(*) as regels mytable
FROM agenda
WHERE datum >= '20130901'
AND datum <= '20131130'
GROUP BY DATEPART(Year, datum), DATEPART(Month, datum)
)kr ON jm.jaarm = kr.jaar AND jm.maandm = kr.maand ORDER BY jaarm, maandm
I have a SQL query Question, I am trying to nest two separate queries which run perfectly fine individually.
The first query is
Select DISTINCT Leads.EmailAddress,ClientCustomFields.CommonName , LeadSources.Name, CONVERT(VARCHAR(10), leads.LeadStatusDate,101) AS [MM/DD/YYYY],Leads.Age,Leads.State, Leads.ProgramOfInterest, Leads.HighestEducationLevel, Leads.LeadNumber from LeadSources inner join Leads on Leads.LeadSourceNumber=LeadSources.LeadSourceNumber join ClientCustomFieldPrograms on Leads.ClientCustomFieldProgramsNumber=ClientCustomFieldPrograms.ClientCustomFieldProgramsNumber join ClientCustomFields on ClientCustomFieldPrograms.ClientCustomFieldsNumber=ClientCustomFields.ClientCustomFieldsNumber where Leads.EmailAddress in ('beloved1965#charter.net') AND Year (Leads.LeadDate)=2011 and ClientCustomFields.CommonName LIKE '%Ashford%' and Leads.LeadStatus='sent' order by Leads.EmailAddress
The second query
/****** Script for SelectTopNRows command from SSMS ******/
SELECT
CASE
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mr.' THEN 'Male'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mrs.' THEN 'Female'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Ms.' THEN 'Female'
ELSE 'Unknown' END as Gender, dbo.ClientLeadDistribution.LeadNumber, dbo.ClientLeadDistribution.Postdate, dbo.ClientLeadDistribution.ClientCustomFieldsNumber, dbo.ClientCustomFields.CommonName, dbo.Leads.LeadSourceNumber, dbo.LeadSources.Name, dbo.Leads.ProgramOfInterest, dbo.Leads.EmailAddress from dbo.ClientLeadDistribution join dbo.Leads on dbo.ClientLeadDistribution.LeadNumber = dbo.Leads.LeadNumber join dbo.LeadSources on dbo.Leads.LeadSourceNumber = dbo.LeadSources.LeadSourceNumber join dbo.ClientCustomFields on dbo.ClientLeadDistribution.ClientCustomFieldsNumber = dbo.ClientCustomFields.ClientCustomFieldsNumber where CustomFieldsXML.exist('/Salutation') = 1 and YEAR (postdate) = 2012 and MONTH (postdate)=1 --and dbo.ClientLeadDistribution.ClientCustomFieldsNumber in (1810,1820,1830,2750,2760,2770) order by PostDate, ClientCustomFieldsNumber
The combination is
Select DISTINCT ClientLeadDistribution.ClientCustomFieldsNumber, Leads.EmailAddress,ClientCustomFields.CommonName , LeadSources.Name, CONVERT(VARCHAR(10), leads.LeadStatusDate,101) AS [MM/DD/YYYY],Leads.Age,Leads.State, Leads.ProgramOfInterest, Leads.HighestEducationLevel, Leads.LeadNumber from LeadSources inner join Leads on Leads.LeadSourceNumber=LeadSources.LeadSourceNumber join ClientCustomFieldPrograms on Leads.ClientCustomFieldProgramsNumber=ClientCustomFieldPrograms.ClientCustomFieldProgramsNumber join ClientCustomFields on ClientCustomFieldPrograms.ClientCustomFieldsNumber=ClientCustomFields.ClientCustomFieldsNumber join ClientLeadDistribution on ClientCustomFields.ClientCustomFieldsNumber=ClientLeadDistribution.ClientCustomFieldsNumber where Leads.EmailAddress in ('beloved1965#charter.net') AND Year (Leads.LeadDate)=2011 and ClientCustomFields.CommonName LIKE '%Fortis%' and Leads.LeadStatus='sent' and ClientLeadDistribution.ClientCustomFieldsNumber =(SELECT ClientLeadDistribution.CustomFieldsXML,CASE
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mr.' THEN 'Male'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Mrs.' THEN 'Female'
WHEN CustomFieldsXML.value('(/Salutation/node())[1]', 'varchar(max)') = 'Ms.' THEN 'Female'
ELSE 'Unknown' END as Gender from dbo.ClientLeadDistribution join dbo.Leads on dbo.ClientLeadDistribution.LeadNumber = dbo.Leads.LeadNumber join dbo.LeadSources on dbo.Leads.LeadSourceNumber = dbo.LeadSources.LeadSourceNumber join dbo.ClientCustomFields on dbo.ClientLeadDistribution.ClientCustomFieldsNumber = dbo.ClientCustomFields.ClientCustomFieldsNumber where CustomFieldsXML.exist('/Salutation') = 1 AND YEAR (postdate) = 2012 and MONTH (postdate)=1) --and dbo.ClientLeadDistribution.ClientCustomFieldsNumber in (1810,1820,1830,2750,2760,2770)--order by ClientCustomFields.CommonName--order by Leads.EmailAddress
And Finally I get this error I tried stuff but nothing seems to work. Any help or suggestions appreciated.
Your subquery is returning 2 fields: "CustomFieldsXML" and "gender".
The error is telling you that you cannot have it return 2 fields. I think you are only after the "gender" so remove the first.