I am trying to do a SELECT with UNION in Microsoft SQL Server where SQL statement has static string columns on which I would like to do a Group By. I am getting this error:
[Code: 207, SQL State: S0001] Invalid column name 'chrg_tx'. [Script position: 221 - 237]
The same SQL works in other RDBMS like INFORMIX.
Here is my SQL
SELECT
'' AS chrg_tx,
SUM(gross_after_discount) gross,
SUM(tax) tax,
SUM(net) net
FROM
invce_line_item
WHERE
ctry_cd = 'US'
AND lcl_btch_id = '2021-11-1214:49:20.838.EN'
AND invce_no = 'INV000001'
AND tax != 0
AND insr = 0
GROUP BY
chrg_tx
UNION
SELECT
'ASSURANCE' AS chrg_tx,
SUM(insr + insr_dsct) gross,
SUM(insr_tax) tax,
SUM(net_insr) net
FROM
invce_line_item
WHERE
ctry_cd = 'US'
AND lcl_btch_id = '2021-11-1214:49:20.838.EN'
AND invce_no = 'INV000001'
AND insr != 0
GROUP BY
chrg_tx
What can I try to resolve this?
Related
I migrated my MySQL database to SQL Server 2019.
Working fine with MySQL :
**$now = Carbon::now()->format('Y-m-d 23:59:00');
->leftJoin('abonnements', function ($join) use ($now) {
$join->on('abonnements.client_id', '=', 'clients.id')
->whereRaw(DB::raw("NOT EXISTS (
SELECT 1 FROM abonnements a1
where a1.date_fin_tolerance >= \"{$now}\" and a1.date_debut <= \"{$now}\"
and a1.client_id = clients.id
AND a1.created_at > abonnements.created_at
)"**
SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name: '2022-12-20 23:59:00'. (SQL: select count(*) as aggregate from [customers] left join [subscriptions] on [subscriptions].[customer_id] = [customers].[id] and NOT EXISTS (\r\n SELECT 1 FROM subscriptions a1\r\n where a1.date_end_tolerance >= cast( "2022-12-20 23:59:00" as datetime) and a1.start_date <= cast("2022-12-20 23:59:00" as datetime)\r\n and a1.client_id = customers.id\r\n AND a1.created_at > subscriptions.created_at\r\n ) and [subscriptions].[end_tolerance_date] >= 2022-12-20 23:59:00 and [subscriptions].[start_date] < = 2022-12-20 23:59:00 left join [subscription_type] on [subscription_type].[subscription_type_id] = [subscription_type].[id] where [customers].[deleted_at] is null)"
Why it confuses the value with the column
Double quotes are used for an identifier (e.g.: column or table name) within SQL Server. Use single quotes to pass the datetime value as a string.
My application uses a query that is working fine using a MySQL/MariaDB-Database.
I did modify my application to be more flexible and work with Microsoft SQL Server too.
Unfortunately the following query does NOT work using a SQL Server database:
select
p.PrinterGUID,
(exists (select 1
from computerdefaultprinter cdp
where cdp.PrinterGUID = p.PrinterGUID and
cdp.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f')
) as is_computer_default,
(exists (select 1
from userdefaultprinter udp
where udp.PrinterGUID = p.PrinterGUID and
udp.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054')
) as is_user_default
from
((select cm.PrinterGUID
from computermapping cm
where cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
) union -- to remove duplicates
(select PrinterGUID
from usermapping um
where um.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054')) p;
Running this query throws an error
Incorrect syntax near the keyword 'exists'
Microsoft SQL Server Management Studio returns the following in German:
I have created a SQL Fiddle with some example data: SQL-Fiddle
If necessary, more background-information is available here: UNION 2 Select-queries with computed columns
Is it possible to modify this query to work in both MySQL and SQL Server?
Thank you very much!
The literal translation of your query would be the following:
select
p.PrinterGUID,
CASE WHEN exists (select 1
from computerdefaultprinter cdp
where cdp.PrinterGUID = p.PrinterGUID and
cdp.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f')
THEN 1 ELSE 0 END as is_computer_default,
CASE WHEN exists (select 1
from userdefaultprinter udp
where udp.PrinterGUID = p.PrinterGUID and
udp.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054')
THEN 1 ELSE 0 END as is_user_default
from (select cm.PrinterGUID
from computermapping cm
where cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
union -- to remove duplicates
select PrinterGUID
from usermapping um
where um.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054') p;
Notice the use of a CASE expressions to determine the value of the column for when the EXISTS evaluates to true or not.
Just try out the following query. You don't need to use exists like this in sql server. Instead filter out the rows at the end using is_computer_default or is_user_default
select p.PrinterGUID,
(select 1
from computerdefaultprinter cdp
where cdp.PrinterGUID = p.PrinterGUID and
cdp.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f')
as is_computer_default,
(select 1
from userdefaultprinter udp
where udp.PrinterGUID = p.PrinterGUID AND
udp.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
) as is_user_default
from ((select cm.PrinterGUID
from computermapping cm
where cm.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
) union -- to remove duplicates
(select PrinterGUID
from usermapping um
where um.UserGUID = 'd3cf699b-8d71-4dbc-92f3-402950042054'
)
) p;
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'
)
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
I need to know if the used INTO SQL Server and equivalent ROWNUM in SQL Server
SELECT
SERIE, CORRELATIVO
INTO
vSerie, vCorrelativo
FROM
SIG.SAF_SERIES_DOCUMENTOS_DET
WHERE
COMPANIA = pCompania
AND MONTO = pMonto
AND ESTADO = 'P'
AND ROWNUM = 1;
This should do it, although you're missing an order by:
SELECT top 1
#vSerit = SERIE,
#vCorrelativo = CORRELATIVO
FROM SIG.SAF_SERIES_DOCUMENTOS_DET
WHERE COMPANIA = #pCompania
AND MONTO = #pMonto
AND ESTADO = 'P'
If you need something else than the first row, You can also do a row_number() window function as a column into your select and use that to limit the data or use offset / fetch if you're in SQL Server 2012 or use top twice with asc / desc order by