i want the Output of the Following Query in One row
i want to merge Below SQL Query
Help me please
select Provider_ID,Circel_ID,count(distinct td_all.ID),t_det.BillNoTemp from TAPINOUT_DIFFERENCES_ALL td_all
inner join TransferDetails t_det on td_all.bill_no=t_det.Bill_No
where td_all.bill_no not in (select bill_no from TAPINOUT_DIFFERENCES_ALL where Status='Open') and sourcename='TransferDetails'
group by td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp
order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp ,count(distinct td_all.ID)as count from TAPINOUT_DIFFERENCES_ALL td_all
INNER JOIN TAPIN_Details TAP_DET ON td_all.FILENAME=TAP_DET.FLNAME
where td_all.SOURCENAME='TransferDetails' and td_all.Status='Open'
group by td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp
order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp,count(distinct td_all.ID)AS COUNT from TAPINOUT_DIFFERENCES_ALL td_all
inner join TAPIN_Details TAP_DET on td_all.FILENAME=TAP_DET.FLNAME
where td_all.anb_comments='Invoice Not Found'
group by td_all.Provider_ID,td_all.Circel_ID,TAP_DET.BillNoTemp order by td_all.Provider_ID,td_all.Circel_ID
select td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp,count(distinct td_all.ID) from TAPINOUT_DIFFERENCES_ALL td_all
inner join TransferDetails t_det on td_all.bill_no=t_det.Bill_No
where td_all.anb_comments='IT File not found'
group by td_all.Provider_ID,td_all.Circel_ID,t_det.BillNoTemp order by td_all.Provider_ID,td_all.Circel_ID
I think you're looking for the UNION operator, which lets you append the results of multiple queries into a single result set.
It works like so:
SELECT columns FROM tbl1 WHERE criteria
UNION
SELECT columns FROM tbl2 WHERE criteria
Use UNION keyword between statements.
SELECT bla, bla2 FROM table1
UNION ALL
SELECT bla3, bla4 FROM table2
Related
I want to distinct columns after ORDER BY points.pPoint.
this is points table diagram:
I want something as following image on the right side but getting result as the left side:
and this is my code:
SELECT TOP(6) MedicalExpertise.meid
FROM physician INNER JOIN
MedicalExpertise ON physician.meid = MedicalExpertise.meid INNER JOIN
points ON physician.phId = points.phID
ORDER BY points.pPoint DESC
Perhaps something like this?
SELECT DISTINCT meid
FROM ( SELECT TOP ( 6 ) MedicalExpertise.meid
FROM physician
INNER JOIN MedicalExpertise ON physician.meid = MedicalExpertise.meid
INNER JOIN points ON physician.phId = points.phID
ORDER BY points.pPoint DESC ) d
ORDER BY 1 DESC;
Simply use distinct keyword,
Ex:
SELECT DISTINCT column1, column2, ...
FROM table_name;
Can you edit the original question what you really want? Does it have to be grouped by any column before getting distinct? Please update the question.
in the select statement below I have 707 row
select detail_serial, Price from apa_invoice_detail
and I have another select statement which is
Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
and this one gives me 197 row
I need to get the price from the first statement for the detail_serial in the second statement (for only 197 row)
I tried this:
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
,asc_item.item_name_2
,asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
but it gives me "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
How can I solve this ???
try removing the columns in the inner select statement
select detail_serial, Price from apa_invoice_detail where detail_serial in
(Select max(detail_serial)
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2)
Use EXISTS instead of IN.
;WITH SecondQueryResults AS
(
Select max(detail_serial) detail_serial
, asc_item.item_name_2
, asc_group.group_name_2
From apa_invoice_detail
inner join asc_item on asc_item.item_id=apa_invoice_detail.item_id
inner join asc_group on asc_group.group_id=asc_item.group_id
Group by asc_item.item_name_2, asc_group.group_name_2
)
select
detail_serial,
Price
from
apa_invoice_detail T
where
EXISTS (SELECT 'serial exists on query result' FROM SecondQueryResults AS S WHERE S.detail_serial = T.detail_serial)
... or just select 1 column (the detail_serial) inside your IN subquery.
SELECT DISTINCT(t1.Ticker),t2.SecurityID,t2.ClosePrice,t2.QuoteDateTime FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
WHERE t2.QuoteDateTime IN (SELECT max(QuoteDateTime) FROM [Hub].[SecurityMaster].[SecurityPrices]) AND t1.SecurityTypeName = 'REIT'
I get an output with no data. The subquery doesn't run along with the other filter in the WHERE clause. I am not sure what I am doing wrong. Can somebody please help!
If you are trying to get the lastest row from SecurityPrices for each Ticker, one option is to use cross apply():
select --distinct /* distinct not needed if `Ticker` is unique on `smd`
smd.Ticker
, sp.SecurityID
, sp.ClosePrice
, sp.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityMasterDetails] as smd
cross apply (
select top 1
i.SecurityID
, i.ClosePrice
, i.QuoteDateTime
from [Hub].[SecurityMaster].[SecurityPrices] i
where i.SecurityID = smd.SecurityID
order by i.QuoteDateTime desc
) as sp
where SecurityTypeName = 'REIT' /* which table does this column belong to? */
I think your query would be
SELECT DISTINCT TOP 1 WITH TIES
t1.Ticker,
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2 ON t2.SecurityID =t1.SecurityID
WHERE SecurityTypeName = 'REIT'
ORDER BY t2.QuoteDateTime DESC
You aren't getting results because the max(QuoteDateTime) record doesn't have SecurityTypeName = 'REIT'. I think you want the max(QuoteDateTime) for this SecurityTypeName, so this can be done with an INNER JOIN.
SELECT DISTINCT
(t1.Ticker),
t2.SecurityID,
t2.ClosePrice,
t2.QuoteDateTime
FROM [Hub].[SecurityMaster].[SecurityMasterDetails] as t1
INNER JOIN [Hub].[SecurityMaster].[SecurityPrices] as t2
ON t2.SecurityID =t1.SecurityID
INNER JOIN
(SELECT max(QuoteDateTime) DT FROM [Hub].[SecurityMaster].[SecurityPrices]) P on P.DT = t2.QuoteDateTime
WHERE SecurityTypeName = 'REIT'
EDIT
Your data doesn't have what you think it does, I suspect. Here is how you can check...
--Find the SecurityID that matches the max date
SELECT
SecurityID ,
max(QuoteDateTime) DT
FROM [Hub].[SecurityMaster].[SecurityPrices]
GROUP BY SecurityID
--I'm betting this ID isn't in your SecurityMasterDetails where the Type is REIT
SELECT DISTINCT
SecurityID
FROM SecurityMasterDetails
WHERE SecurityTypeName = 'REIT'
Since the SecurityID returned in the first query isn't in the second query result set, you are going to get NULL results.
I have a SQL query that looks like this
Select a.*
From table1 a
where a.ColumnName in
(Select MAX(b.ColumnName)
from table2 b
where b.ColumnName2 in
(
Select MAX(c.columnName)
from table3 c
Group by c.ColumnName2
)
Group by b.ColumnName2
)
I am trying to write this in a join statement. I am positive inner join is what I need to get the right information. If someone could translate this to a join statement, I would be really glad.
Thank you.
EDIT 1:
I tried the typical Join statement that a rookie would.
Select a.*
from table1 a
inner join table2 b
on a.columnname = (Select max(b.columnName) from table2)
inner join table3 c
on b.columnName = (select max(c.columnName) from table3)
Obviously, that didn't work because I get 100,000+ results when I should be getting 800. I tried using an alias for table2 and table3 INSIDE the subselect statements and selecting the columnname using THAT alias like this:
Select max(bPart.columnName from table2 bPart)
Select max(cPart.columnName from table3 cPart)
Still the same result.
PERHAPS....
Though I'm not sure why a join is needed. Performance wise exists would likely be fastest, and since you're not returning values from table2 or 3 it seems like it would be the best approach.
SELECT a.*
FROM table1 a
INNER JOIN (SELECT MAX(ColumnName) MColumnName, columnname2
FROM table2
GROUP BY columnName2) B
ON A.columnName = B.mColumnName
INNER JOIN (SELECT MAX(columnName) mColumnName
FROM table3
GROUP BY ColumnName2) C
ON B.columname2 = C.MColumnName
I always look for the solution but today I surrender so please I need help.
I am using two 'inner join' because I need to get the sum of balances from two tables, but the command 'inner join ' doubles the valor when I use sum(column 1, column 2), while that when I use sum(Column 1) everything is ok.
For example:
this query work very well, but I also have to query from other table called casilleros.
select datos_usuarios.id_cliente,
datos_usuarios.nombre ,
sum(membresia.saldo)
from datos_usuarios
inner join membresiaondatos_usuarios.id_cliente=membresia.id_socio
where saldo>0
group by datos_usuarios.id_cliente, datos_usuarios.nombre
I had tried many thing for example
select datos_usuarios.id_cliente,
datos_usuarios.nombre,
sum(membresia.saldo+casilleros.saldo)
from datos_usuarios
inner join membresia on datos_usuarios.id_cliente=membresia.id_socio
inner join casilleros on datos_usuarios.id_cliente=casilleros.id_socio
group by datos_usuarios.id_cliente, datos_usuarios.nombre
But the result is wrong.
I tried with 'union all' but honestly It is not that I need.Because I need only one result of balance per client
select t0.id_cliente,
t0.nombre,
cast(sum(t1.saldo) + sum(t2.saldo) as decimal (10,2)),
from datos_usuarios t0
inner join membresia t1 on t0.id_cliente=t1.id_socio
inner join casilleros t2 on t0.id_cliente=t2.id_socio
group by t0.id_cliente, t0.nombre
UNION ALL is probably what you really want. But you want it as a derived table. Then do the sum in the outer query.
Select a.id_cliente, a.nombre, sum(a.saldo)
From
(select datos_usuarios.id_cliente, datos_usuarios.nombre, membresia.saldo from
datos_usuarios
inner join membresia on datos_usuarios.id_cliente=membresia.id_socio
inner join casilleros on datos_usuarios.id_cliente=casilleros.id_socio
UNion all
select datos_usuarios.id_cliente, datos_usuarios.nombre, casilleros.saldo from
datos_usuarios
inner join casilleros on datos_usuarios.id_cliente=casilleros.id_socio
) a
group by a.id_cliente, a.nombre
I think you want to get this query
;WITH mc AS (
SELECT saldo, id_socio
FROM membresia
UNION
SELECT saldo, id_socio
FROM casilleros
)
SELECT
datos_usuarios.id_cliente,
datos_usuarios.nombre,
sum(mc.saldo)
FROM datos_usuarios
JOIN mc ON mc.id_socio = datos_usuarios.id_cliente
GROUP BY datos_usuarios.id_cliente, datos_usuarios.nombre
We union two tables with the same structure in one pseudo table mc (using CTE) and then join our new table with table datos_usuarios.