Can anybody correct me with the below query where the query is going wrong ?
For the below Query i need to add where clause and get based on client names. When i execute the query i am able to view only first client data. And also i want the result to store in separate database.
select amount,[Account #]as cdn#,[CC ACCT]as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[VendorFiles] as a
inner join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),a.client)
Union all
Select [Amount],[CDN #] as cdn#,[Card_No] as CC#,[Card_Type] as CCType
from [PayPal_staging].[dbo].[VirtualFiles] as b
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
union all
select [SV10_Amt] as Amount,[Cdr_Id] as cdn#,[SV07_Last4] as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[IVRFiles] as c
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',' SWIS203',' SWIS600')
Can anybody help me with the above query where the query is going wrong and how to get the result for all clients in Where clause and how to store the result in separate database ?
Please write me a complete query for the above task....
It's hard to know for sure, but you have a space preceding each CLIENT ID except for the first one:
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',
' SWIS203',' SWIS600')
Just remove the spaces:
where PK_Client in ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600',
'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600',
'SWIS203','SWIS600')
The where clause only works for the last select. Maybe you want it this way.
SELECT *
FROM (SELECT [Amount]
, [Account #]as cdn#
, [CC ACCT]as CC#
, [CardType] as CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VendorFiles] AS a
INNER JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),a.client)
UNION ALL
SELECT [Amount]
, [CDN #] AS cdn#
, [Card_No] AS CC#
, [Card_Type] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VirtualFiles] AS b
LEFT JOIN [CUBS].[dbo].[Client] as d
ON d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
UNION ALL
SELECT [SV10_Amt] AS Amount
, [Cdr_Id] AS cdn#
, [SV07_Last4] AS CC#
, [CardType] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[IVRFiles] AS c
LEFT JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
) t
WHERE t.PK_Client IN ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600'
,'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600'
,'SWIS203','SWIS600');
Related
I am trying to make this request. I added the group by because I had duplicates. I am getting the error:
Invalid column name for the columns in the group by.
I have read a lot of posts regarding this type of error but I am still stuck. Also I would like to know if I should use sum(). Help please.
INSERT INTO tabA
([load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd])
select 1 [load_number]
,getdate() [load_date]
,'' [no_command]
,'' [no_document]
,coalesce(c.id_numb,-1) [id_transc]
,coalesce(b.elem_cd,-1) [division_cd]
,coalesce(d.budget_cd,-1) [activity_cd]
,'' [project_cd]
from tabB b
left join tabC c on c.credit = b.account
left join tabD d on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010'
group by [load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd]
If you are concerned about duplicates, you can just use select distinct:
select distinct 1 [load_number],
getdate() [load_date],
'' [no_command],
'' [no_document],
coalesce(c.id_numb,-1) [id_transc],
coalesce(b.elem_cd,-1) [division_cd],
coalesce(d.budget_cd,-1) [activity_cd],
'' [project_cd]
from tabB b left join
tabC c
on c.credit = b.account left join
tabD d
on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010';
You can't use column aliases defined in the select in the group by in SQL Server.
someone, please help to join the below queries.
I have tried my best but not able to join with the condition.
PLN_ID is the common column on both the tables.
Query 1-
SELECT PLN_ID
, ASSORTMENT_GROUP
, STORE
, PLANOGRAM
, STATUS
FROM ACN_PLANOGRAMS
WHERE PLANOGRAM not like '%<Untitled>%'
;
Query 2
SELECT distinct(PLN_ID)
, count(*)
, (sum(WIDTH)) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
;
Please changes join what you want you. Try this query:
SELECT
DISTINCT(a.PLN_ID),
(SUM(a.WIDTH)) AS width,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
FROM
ACN_FIXEL a
INNER JOIN
ACN_PLANOGRAMS b ON a.PLN_ID = b.PLN_ID
WHERE
a.type = '0'
AND b.PLANOGRAM NOT LIKE '%<Untitled>%'
GROUP BY
a.PLN_ID,
b.PLN_ID,
b.ASSORTMENT_GROUP,
b.STORE,
b.PLANOGRAM,
b.STATUS
HAVING
COUNT(*) > 1
There are several ways to solve this. Without understanding your data model or business logic I offer the simplest solution, a derived table (inline view):
SELECT p.PLN_ID
, p.ASSORTMENT_GROUP
, p.STORE
, p.PLANOGRAM
, p.STATUS
, f.fixel_count
, f.fixel_width
FROM ACN_PLANOGRAMS p
inner join (SELECT PLN_ID
, count(*) as fixel_count
, (sum(WIDTH)) AS fixel_width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1 ) f
on f.pln_id = p.pln_id
WHERE p.PLANOGRAM not like '%<Untitled>%'
;
This solution only returns results for PLN_ID in both result sets. If you have a different logic you may need to use LEFT OUTER JOIN instead.
Make Query 2 a subquery:
SELECT ap.PLN_ID
, ap.ASSORTMENT_GROUP
, ap.STORE
, ap.PLANOGRAM
, ap.STATUS
, sq.cnt
, sq.width
FROM ACN_PLANOGRAMS ap
JOIN (
SELECT PLN_ID
, count(*) AS cnt
, sum(WIDTH) AS width
FROM ACN_FIXEL
WHERE type='0'
GROUP BY PLN_ID
HAVING count(*) > 1
) sq
ON ( sq.PLN_ID = ap.PLN_ID )
WHERE ap.PLANOGRAM not like '%<Untitled>%'
;
So, I'm trying to get a interesting thing to happen, and haven't been able to really get it to work.
So, my query:
SELECT a.lead_id
,a.status
,a.[user]
, a.list_id
, a.last_local_call_time
, a.owner
FROM [LEADS].[dbo].[LDS_VICIDIAL_LIST] a
inner join leads.dbo.LDS_CONTACT_STATUS b on a.[status]=b.[status]
where list_id in ('2557','2562','2559') and Dialable='Y' and a.status<>'5NI' and a.called_since_last_reset = 'n'
order by a.last_local_call_time
and basically what I am trying to do is get it to display 1 record from list 2557, 1 record from list 2562, and 1 record from 2559 and repeating as such.
SELECT a.lead_id
,a.status
,a.[user]
, a.list_id
, a.last_local_call_time
, a.owner
FROM [LEADS].[dbo].[LDS_VICIDIAL_LIST] a
inner join leads.dbo.LDS_CONTACT_STATUS b on a.[status]=b.[status]
where list_id in ('2557','2562','2559') and Dialable='Y' and a.status<>'5NI' and a.called_since_last_reset = 'n'
order by ROW_NUMBER() OVER (PARTITION BY a.list_id order by a.list_id)
Got me what I wanted.
I have set of two queries. In first query, if is separate from second, I got good results.
First query
SELECT *
FROM
(
SELECT nks.[Id]
, nks.[IdNarudzbe]
, nks.[IdArtikla] as artikal
, nks.[IdUsluge]
, nks.[Naziv]
, nks.Kolicina
, p.Naziv as kupac
, p.Id as kupacId
, p.Adresa
, p.Telefon
, nkz.[BrojDokumenta] AS nalog
, nkz.[BrojDokumentaKroz] AS nalogKroz
, nkz.[RokIsporuke]
, nkz.[IdNastaloOdDokumenta]
, d.Naziv as drzava
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
--...
from first query nkz.[IdNastaloOdDokumenta] is important to second
SELECT BrojDokumenta
, BrojDokumentaKroz
FROM .[dbo].[NarudzbeKupacaZaglavlje]
where id = nkz.[IdNastaloOdDokumenta]
For ex. In first query I got nkz.[IdNastaloOdDokumenta] = 20. Number 20 I use in second query in where statement, and value I get from BrojDokumenta, I would like to join to first query.
I was wondering if is possible to make one query out of these two. I think I can not union operator because number of column from these two queries don't match.
The same table, and the same columns are already in the first query. Perhaps you want a self-join, like this:
FROM [dbo].[NarudzbaKupacaStavke] nks
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz
ON nkz.Id = nks.IdNarudzbe
LEFT JOIN [dbo].[NarudzbeKupacaZaglavlje] nkz2
ON nkz2.Id = nkz.[IdNastaloOdDokumenta]
LEFT JOIN dbo.Partneri p
ON nkz.IdKupac = p.Id
LEFT JOIN dbo.Drzave d
ON p.IdDrzava = d.Id
WHERE idArtikla IN ('FP80PUR-08', 'FP80PUR-09', 'FP80PUR-12')
AND nkz.[VrstaDokumenta] = 'PRO'
AND nkz.StatusArhive = 0
In the following SQL, it was looking at only those vouchers from location_ID = 5. How can I code that, I ONLY want from a Patient_ID who ONLY visited location_ID 5?
SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number",
"Practitioners"."Practitioner_ID", "Practitioners"."First_Name",
"Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name",
"vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB",
"vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID",
"Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID",
"Vouchers"."Location_ID"
FROM ("Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo"
INNER JOIN "Ntier_70751"."PM"."Vouchers" "Vouchers"
ON "vwGenPatInfo"."Account_ID"="Vouchers"."Account_ID")
INNER JOIN "Ntier_70751"."PM"."Practitioners" "Practitioners"
ON "Vouchers"."Actual_Prov_Practitioner_ID"="Practitioners"."Practitioner_ID"
--
WHERE "Vouchers"."Location_ID"=5
Here is one way to do this. I also got rid of all those unneeded double quotes and used proper aliases.
SELECT V.Patient_ID
, gpi.Patient_Number
, P.Practitioner_ID
, P.First_Name
, P.Last_Name
, gpi.Patient_First_Name
, gpi.Patient_Last_Name
, gpi.Patient_DOB
, gpi.Patient_Sex
, V.Carrier_ID
, V.Billing_Date
, V.Patient_Policy_ID
, V.Location_ID
FROM Ntier_70751.PM.vwGenPatInfo gpi
INNER JOIN Ntier_70751.PM.Vouchers V ON gpi.Account_ID = V.Account_ID
INNER JOIN Ntier_70751.PM.Practitioners P ON V.Actual_Prov_Practitioner_ID = P.Practitioner_ID
cross apply
(
select V2.Account_ID
from Ntier_70751.PM.Vouchers V2
where V2.Account_ID = V.Account_ID
group by V2.Account_ID
HAVING MAX(Location_ID) = 5
AND MIN(Location_ID) = 5
) x
Put a condition as say;
WHERE "Vouchers"."Location_ID" = 5
I would go with not exists
SELECT "Vouchers"."Patient_ID", "vwGenPatInfo"."Patient_Number",
"Practitioners"."Practitioner_ID", "Practitioners"."First_Name", "Practitioners"."Last_Name", "vwGenPatInfo"."Patient_First_Name", "vwGenPatInfo"."Patient_Last_Name", "vwGenPatInfo"."Patient_DOB", "vwGenPatInfo"."Patient_Sex", "Vouchers"."Carrier_ID", "Vouchers"."Billing_Date", "Vouchers"."Patient_Policy_ID", "Vouchers"."Location_ID"
FROM "Ntier_70751"."PM"."vwGenPatInfo" "vwGenPatInfo" INNER JOIN
"Ntier_70751"."PM"."Vouchers" "Vouchers"
ON "vwGenPatInfo"."Account_ID" = "Vouchers"."Account_ID" INNER JOIN
"Ntier_70751"."PM"."Practitioners" "Practitioners"
ON "Vouchers"."Actual_Prov_Practitioner_ID" = "Practitioners"."Practitioner_ID"
WHERE "Vouchers"."Location_ID"=5
and not exists (select 1
FROM "Ntier_70751"."PM"."Vouchers" "Vouchers2"
WHERE "Vouchers2"."Patient_ID" = "Vouchers2"."Patient_ID"
AND "Vouchers2"."Location_ID"<>5)
Just using the condition 'WHERE "Vouchers"."Location_ID" = 5'will return all Patient_IDs that visited the that location at least once but not exclusively. There are several ways to do it but the cleanest would be using having max (location_id) <5 and min (location_id) >5