SQL Server 2005 using identifier from previous join statment - sql-server-2005

I have a table with 2 columns, the first is PKey, the second is an identifier; BAN & IID respectively.
I am looking to pull out the top 2 IID's for every distinct BAN. I used a sub-query in the From to get the Max(IID) which works fine. However when I try to join the table to my 1st statement to get the next highest MAX(IID) I get:
"Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "ii.iid" could not be bound."
This is referring to the Where statement in my 2nd join "where iid < ii.iid"
select distinct ii.BAN, ii.IID, ii2.IID
from (select distinct BAN, IID = Max(IID) from Bill_Timeliness..Invoices with(nolock) group by BAN) II
join (select distinct BAN, IID = Max(IID) from Bill_Timeliness..Invoices with(nolock) where iid < ii.iid group by BAN) II2
on ii.ban = ii2.ban
I have tried re-creating the first part as a second subquery of the where statement.
I also tried where iid < max(ii.iid)
Neither of these worked....i would really appreciate if someone can help me out here.
-Ben

This should work for you:
select o.BAN,
x.MaxID as TopID,
MAX(case when o.IID != x.MaxID then o.IID else null end) as SecondID
from (
select BAN, MAX(IID) as MaxID
from Bill_Timeliness..Invoices group by BAN) x
inner join Bill_Timeliness..Invoices o
on x.BAN = o.BAN
group by o.BAN,
x.MaxID

That is not how aliases are scoped
You could try a CTE instead.
;with CTE_X as (
select BAN, IID, dense_rank() over (partition by BAN order by IID desc) as ranker from Bill_Timeliness..Invoices)
select I.BAN, I.IID, II.IID from CTE_X I inner join CTE_X II on
I.BAN = II.BAN and I.ranker = 1 and II.ranker = 2

Related

multiple joins to same sub query

I need to join to the same table repeatedly but this looks ugly. Any suggestions appreciated. Below is simplified SQL, I have 8 tables in the subquery and it produces many duplicate records of the same date, so I need to find only the newest record for each client. (I don't think DB and/or version should matter, but I am using DB2 11.1 LUW)
select c.client_num, a.eff_date, t.trx_date
from client c
join address a on(a.id = c.addr_id)
join transaction t on(t.addr_id = a.id)
{many other joins}
where {many conditions};
select SQ.*
from [ABOVE_SUBQUERY] SQ
join
(select client_num, max(eff_date) AS newest_date from [ABOVE_SUBQUERY] group by client_num) AA
ON(SQ.client_num = AA.client_num and SQ.eff_date = AA.newest_date)
join
(select client_num, max(trx_date) AS newest_date from [ABOVE_SUBQUERY] group by client_num) TT
ON(SQ.client_num = TT.client_num and SQ.trx_date = TT.newest_date)
I need to fine only the newest record for each client.
Can't you just use row_number()?
select t.*
from (select t.*,
row_number() over (partition by client_num order by eff_date desc, eff_time desc) as seqnum
from <whatever> t
) t
where seqnum = 1;

Using Pivot to change Rows into Columns

I am resposting this and adding additional information. I am working on a SQL Query and using Pivot and running into an issue. The columns which I have in the Pivot Table actually show as row data and is repeated for each server.
WITH agg AS
(
select NodeID,
count(distinct cpuindex) as number_of_cpu,
case
when count(distinct cpuindex) < 8 THEN 1
else count(distinct cpuindex) / 8
end AS number_of_cores
from CPUMultiLoad_Detail
where nodeid in (select nodeid from nodesdata)
group by NodeID
)
SELECT * FROM (
SELECT cp.Environment, n.Caption,
cs.ComponentName,cs.ComponentStatisticData, cs.ErrorMessage,
agg.NodeID, agg.number_of_cpu, agg.number_of_cores, n.description
FROM APM_CurrentStatistics cs
INNER JOIN APM_Application app
ON cs.ApplicationID = app.ID
AND app.Name IN ('Oracle Database Licensing')
INNER JOIN NodesData n
ON cs.NodeID = n.NodeID
AND n.description NOT LIKE '%Windows%'
INNER JOIN NodesCustomProperties cp
ON cp.NodeID = n.NodeID
INNER JOIN agg
ON cs.NodeID = agg.NodeID
) t
PIVOT(
max(cs.ErrorMessage) FOR cs.ComponentName IN (
[Oracle Version],
[Oracle Partitioning],
[Oracle Tuning Pack],
[Diagnostic Pack],
[Real Application Clusters (RAC)])
) AS pivot_table;
Desired Output
I am getting this Error
Msg 107, Level 15, State 1, Line 30
The column prefix 'cs' does not match with a table name or alias name used in the query.
Msg 107, Level 15, State 1, Line 30
The column prefix 'cs' does not match with a table name or alias name used in the query.
Any help will be greatly appreciated.
Just take out the two cs. in this line
max(cs.ErrorMessage) FOR cs.ComponentName IN (
It treats the prior select statement like it has already run so those column names don't need to be disambiguated

How to join table to table created from query

I query to get the top 5 results of IPs and then i want to get for each IP, the countries and others fields related to it by join.
select actual_ip, actual_country_code, actual_country_name, organization FROM "public"."bus_request" inner join (
select top 5 actual_ip, count(*) FROM "public"."bus_request"
where app_name = 'xxxxx' and request_score>0 and date >= '2019-06-07' and event_type <> 'browser_js'
group by actual_ip order by count desc ) as temp on actual_ip = temp.actual_ip
SQL Error [500310] [42702]: [Amazon](500310) Invalid operation: column reference "actual_ip" is ambiguous;
Not sure exactly which DBMS you're using, but try resolving the ambiguity by specifying the table, e.g.:
select busreq.actual_ip, actual_country_code, actual_country_name, organization FROM "public"."bus_request" as busreq inner join (
select top 5 actual_ip, count(*) FROM "public"."bus_request"
where app_name = 'xxxxx' and request_score>0 and date >= '2019-06-07' and event_type <> 'browser_js'
group by actual_ip order by count desc ) as temp on busreq.actual_ip = temp.actual_ip
(you may need to disambiguate the inner use of actual_ip - but hopefully not.)
You should always qualify all column references in a query that references more than one table. In addition, you should use table aliases so the query is easier to write and to read:
select br.actual_ip, br.actual_country_code, br.actual_country_name, br.organization
from "public"."bus_request" br inner join
(select top 5 br2.actual_ip, count(*)
from "public"."bus_request" br2
where br2.app_name = 'xxxxx' and
br2.request_score > 0 and
br2.date >= '2019-06-07' and
br2.event_type <> 'browser_js'
group by br2.actual_ip
order by count(*) desc
) br2
on br2.actual_ip = br.actual_ip;

Query error IN GROUP BY

I want only one F_LATPRIMI, F_LONPRIMI from any I_ID_NAVE
select I_ID_NAVE ,F_LATPRIMI ,F_LONPRIMI
from(
select
distinct imo,[N_ident_seguenziale]
from
navi
inner join
[ESTERNALIZZAZIONE_FASCICOLINAVE]
ON
[ESTERNALIZZAZIONE_FASCICOLINAVE].[I_ID_NAVE]=navi.[N_ident_seguenziale]
) as tabimo
inner join
posizioni
on
posizioni.[I_ID_NAVE]=tabimo.[N_ident_seguenziale]
where
DATEDIFF(minute, D_TS,GETDATE() )<30
group by I_ID_NAVE
The error is
Msg 8120, Level 16, State 1, Line 3 Column 'posizioni.F_LATPRIMI' is
invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
I think what you are looking for is not GROUP BY rather ROW_NUMBER() function like
select I_ID_NAVE,
F_LATPRIMI ,
F_LONPRIMI
from (
select
imo,[N_ident_seguenziale], I_ID_NAVE,
F_LATPRIMI ,F_LONPRIMI,
ROW_NUMBER() OVER(PARTITION BY I_ID_NAVE ORDER BY I_ID_NAVE) AS rn
from
navi inner join [ESTERNALIZZAZIONE_FASCICOLINAVE]
ON [ESTERNALIZZAZIONE_FASCICOLINAVE].[I_ID_NAVE] = navi.[N_ident_seguenziale]) tabimo
join posizioni p on p.[I_ID_NAVE] = tabimo.[N_ident_seguenziale]
where DATEDIFF(minute, D_TS,GETDATE() ) < 30
and rn = 1;
One should group by all the columns which is in the select clause...
select I_ID_NAVE ,F_LATPRIMI ,F_LONPRIMI
from(
select
distinct imo,[N_ident_seguenziale]
from
navi
inner join
[ESTERNALIZZAZIONE_FASCICOLINAVE]
ON
[ESTERNALIZZAZIONE_FASCICOLINAVE].[I_ID_NAVE]=navi.[N_ident_seguenziale]
) as tabimo
inner join
posizioni
on
posizioni.[I_ID_NAVE]=tabimo.[N_ident_seguenziale]
where
DATEDIFF(minute, D_TS,GETDATE() )<30
group by I_ID_NAVE,F_LATPRIMI ,F_LONPRIMI
in MsSql when using an Group By, all other columns need to be in an aggregate function (like MAX(), etc).
So maybe what you want is to delete the last row group by I_ID_NAVE.
What do you really want?
Please explain to us so we can help
If you "want the last F_LATPRIMI and F_LONPRIMI from anyI_ID_NAVE"
use the "Last"-Aggregate-function:
select I_ID_NAVE ,LAST(F_LATPRIMI) ,LAST(F_LONPRIMI)
from(
select
distinct imo,[N_ident_seguenziale]
from
navi
inner join
[ESTERNALIZZAZIONE_FASCICOLINAVE]
ON
[ESTERNALIZZAZIONE_FASCICOLINAVE].[I_ID_NAVE]=navi.[N_ident_seguenziale]
) as tabimo
inner join
posizioni
on
posizioni.[I_ID_NAVE]=tabimo.[N_ident_seguenziale]
where
DATEDIFF(minute, D_TS,GETDATE() )<30
group by I_ID_NAVE

getting "No column was specified for column 2 of 'd'" in sql server cte?

I have this query, but its not working as it should,
with c as (select
month(bookingdate) as duration,
count(*) as totalbookings
from
entbookings
group by month(bookingdate)
),
d as (SELECT
duration,
sum(totalitems)
FROM
[DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
group by duration
)
select
c.duration,
c.totalbookings,
d.bkdqty
from
c
inner join d
on c.duration = d.duration
when I run this, I am getting
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 2 of 'd'.
Can any one tell me what am I doing wrong?
Also, when I run this,
with c as (select
month(bookingdate) as duration,
count(*) as totalbookings
from
entbookings
group by month(bookingdate)
),
d as (select
month(clothdeliverydate),
SUM(CONVERT(INT, deliveredqty))
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
)
select
c.duration,
c.totalbookings,
d.bkdqty
from
c
inner join d
on c.duration = d.duration
I get
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 1 of 'd'.
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 2 of 'd'.
You just need to provide an alias for your aggregate columns in the CTE
d as (SELECT
duration,
sum(totalitems) as sumtotalitems
FROM
[DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
group by duration
)
[edit]
I tried to rewrite your query, but even yours will work once you associate aliases to the aggregate columns in the query that defines 'd'.
I think you are looking for the following:
First one:
select
c.duration,
c.totalbookings,
d.bkdqty
from
(select
month(bookingdate) as duration,
count(*) as totalbookings
from
entbookings
group by month(bookingdate)
) AS c
inner join
(SELECT
duration,
sum(totalitems) 'bkdqty'
FROM
[DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
group by duration
) AS d
on c.duration = d.duration
Second one:
select
c.duration,
c.totalbookings,
d.bkdqty
from
(select
month(bookingdate) as duration,
count(*) as totalbookings
from
entbookings
group by month(bookingdate)
) AS c
inner join
(select
month(clothdeliverydate) 'clothdeliverydatemonth',
SUM(CONVERT(INT, deliveredqty)) 'bkdqty'
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
) AS d
on c.duration = d.duration
I had a similar query and a similar issue.
SELECT
*
FROM
Users ru
LEFT OUTER JOIN
(
SELECT ru1.UserID, COUNT(*)
FROM Referral r
LEFT OUTER JOIN Users ru1 ON r.ReferredUserId = ru1.UserID
GROUP BY ru1.UserID
) ReferralTotalCount ON ru.UserID = ReferralTotalCount.UserID
I found that SQL Server was choking on the COUNT(*) column, and was giving me the error No column was specified for column 2.
Putting an alias on the COUNT(*) column fixed the issue.
SELECT
*
FROM
Users ru
LEFT OUTER JOIN
(
SELECT ru1.UserID, COUNT(*) AS -->MyCount<--
FROM Referral r
LEFT OUTER JOIN Users ru1 ON r.ReferredUserId = ru1.UserID
GROUP BY ru1.UserID
) ReferralTotalCount ON ru.UserID = ReferralTotalCount.UserID
A single with clause can introduce multiple query names by separating them with a comma but it's mandatory that every column has a name
In this case, the second query has a column without one:
as (SELECT
duration,
sum(totalitems) --**HERE IS A MISSING NAME**
FROM ...
That's all.
Because you are creatin a table expression, you have to specify the structure of that table, you can achive this on two way:
1: In the select you can use the original columnnames (as in your first example), but with aggregates you have to use an alias (also in conflicting names). Like
sum(totalitems) as bkdqty
2: You need to specify the column names rigth after the name of the talbe, and then you just have to take care that the count of the names should mach the number of coulms was selected in the query. Like:
d (duration, bkdqty)
AS (Select.... )
With the second solution both of your query will work!
Quite an intuitive error message - just need to give the columns in d names
Change to either this
d as
(
select
[duration] = month(clothdeliverydate),
[bkdqty] = SUM(CONVERT(INT, deliveredqty))
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
)
Or you can explicitly declare the fields in the definition of the cte:
d ([duration], [bkdqty]) as
(
select
month(clothdeliverydate),
SUM(CONVERT(INT, deliveredqty))
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
)
Just add an alias name as follows
sum(totalitems) as totalitems.
evidently, as stated in the parser response, a column name is needed for both cases. In either versions the columns of "d" are not named.
in case 1: your column 2 of d is sum(totalitems) which is not named. duration will retain the name "duration"
in case 2: both month(clothdeliverydate) and SUM(CONVERT(INT, deliveredqty)) have to be named
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 1 of 'd'.
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 2 of 'd'.
ANSWER:
ROUND(AVG(CAST(column_name AS FLOAT)), 2) as column_name