SELECT query using group by - sql

I don't know why the following query is not working:
SELECT whs_code, pdt_code,case_dt_yyyymmdd, fresh_frozen_status,
SUM(qty_cases_on_hand)-Qty, SUM(qty_weight_on_hand)-Wt, operation
FROM
(
SELECT whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation,SUM(qty_cases_on_hand) AS Qty, SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
)
WHERE operation='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation`
The error is :
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'WHERE'.
To make it simple to understand what i am trying to do here, please see the example
I need data as result of
(SELECT x FROM tbl_table Where column y='SU')-(SELECT x FROM tbl_table Where column y='RU')

SELECT ru.whs_code,
ru.pdt_code,
ru.case_dt_yyyymmdd,
ru.fresh_frozen_status,
ru.operation,
ru.Qty - su.Qty AS Qty_Diff,
ru.Wt - su.Wt AS Wt_Diff
FROM
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) ru,
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) su
WHERE ru.whs_code = su.whs_code
AND ru.pdt_code = su.pdt_code
AND ru.case_dt_yyyymmdd = su.case_dt_yyyymmdd
AND ru.fresh_frozen_status = su.fresh_frozen_status
AND ru.operation = su.operation;

Related

Select top n after data operations SQL

I want to
Filter a data set
Take distinct of a column
take out top 10 rows of that data set with the distinct column
The code I am using is
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
)
;
and the error I am getting is
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Where am I going wrong?
You have to give a name to the subquery:
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
) AS my_subquery
ORDER BY businessdata
;
make sure you also an ORDER BY to correctly set the order and thus make TOP meaningful

Oracle to T-SQL CTE conversion errors

Having trouble converting Oracle syntax to T-SQL. Trying to convert the following statement:
SELECT *
FROM (WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
MINUS
SELECT PP.USER_ID FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1)
Conversion attempt:
SELECT *
FROM (WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
EXCEPT
SELECT PP.USER_ID FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1)
SQL Server returned the following errors:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near ')'.
SQL Server does not allow for CTE in subquery:
WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
EXCEPT -- Oracle has MINUS
SELECT PP.USER_ID
FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1;
Second thing: SQL Server has EXCEPT keyword instead of MINUS.

Error in SQL Server query - inner join

I am trying to add Percentile value for each record as new column. But i am getting error in my SQL query. Can anyone please help to solve it.
Error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'inner'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b where [Country] = 'AUSTRALIA'
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
You have your where statement in the wrong place. Joins are formally part of the from statement and thus come before criteria. To have your criterion at the bottom check the correct table you use the alias.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
where b.[Country] = 'AUSTRALIA'

Trying to calculate cummulative sum in tsql

What am I doing wrong here?
The result is an error, saying:
Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'.
Msg 156, Level 15, State 1, Line 25 Incorrect syntax near the keyword
'as'.
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
) as data
order by tekst, transdate
Regards
Peter
It is obvious that you have inappropriate version of Sql Server. Cumulative sums with order by clause like:
SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
are only available from Sql Server 2012+.
Actually I can reproduce those errors on Sql Server 2008:
This is on Sql Server 2012:
Notice how the error message changes.
The way you are getting data from derived table is not correct..
EX:
create table
sales
(
id int
)
insert into sales
values
(1),
(2),
(3)
derived table should always have table alias and parent table should refer using from
----this is valid
select
* from
(
select id+1 as id1
from sales
) b
--this is not valid
select
*
(select
id from sales
)b
--Above is valid when you have a subquery say
select
id,(select t1.name from table t1 where t1.id=t2.id)as custname
from table t2
coming to your question..this is not valid.I see both table types are same
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
So you can write something like below
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select * from
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
and b.type='afholdt'
and b.tekst = 'fs'
) as Normtid
order by tekst, transdate

Works in SQLFiddle Not in SQL Server 2012

When I run this Query in SQL Fiddle it runs perfectly:
;with cte as
(SELECT
analysisvalue.analysisid,
heatname,
analysistime,
sampletype,
grade,
productid,
element,
value
FROM
dbo.AnalysisValue
INNER JOIN
dbo.CAnalysis
ON
dbo.AnalysisValue.AnalysisID = dbo.CAnalysis.AnalysisID
WHERE
heatname = 'A7M0066'
)
SELECT
*
FROM
S_analysis s
CROSS JOIN (SELECT TOP 1 analysistime
FROM cte
ORDER BY analysisid desc
) c
WHERE s.heat_no = 'A7M0066' OR
(s.analysis_datetime BETWEEN c.analysistime AND DATEADD(hh, 2, c.analysistime ))
However when I run it in SQL Server 2012, I receive this error with the leading semi-colon:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ';'.
And this error when without the semicolon:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'.
Instead of a CTE, you could use a #temp table and see if the query is valid:
SELECT
analysisvalue.analysisid,
heatname,
analysistime,
sampletype,
grade,
productid,
element,
value
INTO #temptable
FROM
dbo.AnalysisValue
INNER JOIN
dbo.CAnalysis
ON
dbo.AnalysisValue.AnalysisID = dbo.CAnalysis.AnalysisID
WHERE
heatname = 'A7M0066';
SELECT *
FROM
S_analysis s
CROSS JOIN (SELECT TOP 1 analysistime
FROM #temptable
ORDER BY analysisid desc
) c
WHERE s.heat_no = 'A7M0066' OR
(s.analysis_datetime BETWEEN c.analysistime AND DATEADD(hh, 2, c.analysistime ))