how do I use sql DISTINCT,UNION with where clause - sql

Dear All,
how do I use sql DISTINCT,UNION with where clause?
column "GDN" is in the database table "GPG".
please recommend and solution.
Thanks
TABEL : GPD
PNM
GPPI12301001
GPPI12301002
GPPI82301001
GPPI82301002
TABEL : GPG
GDN
PNM
A.04.01.002.001
GPPI12301001
A.04.01.002.001
GPPI12301002
A.04.01.008.001
GPPI82301001
A.04.01.008.001
GPPI82301002
desired result
PNM
GPPI12301001
GPPI12301002
Dim query As String = "SELECT DISTINCT PNM FROM GPD UNION SELECT DISTINCT PNM FROM GPG ORDER BY PNM"
Below where clause I mean in the GPG database table
WHERE GDN = 'A.04.01.002.001'

Each SELECT has its own WHERE clause. And the UNION has an ORDER BY for its final result.
SELECT PNM
FROM GPD
UNION
SELECT PNM
FROM GPG
WHERE GDN = 'A.04.01.002.001' -- belongs to the UNIONS's 2:nd SELECT
ORDER BY PNM -- belongs to the whole UNION

SELECT DISTINCT PNMs.PNM
FROM (
SELECT PNM FROM GPD
UNION
SELECT PNM
FROM GPG
WHERE GDN = 'A.04.01.002.001'
) PNMs
ORDER BY PNM

You will never get your desired output so long as you UNION all the PNM values from the GPD table. You will always get all the PNM values instead of the subset related to the given GDN value.
All you need is this:
SELECT DISTINCT PNM
FROM GPG
WHERE GDN = 'A.04.01.002.001'
ORDER BY PNM

Related

BQ SQL join with a table with a name that is derived from a query

I have some fields that are a date. That date then is then used to look up a table with a name corresponding to the date of that field. I'm doing a join to get other fields, but the question is how to treat the field with the date as a variable that can be used then to perform the join.
Here is the example query:
with tab1 as (
select
product_id,
start_date,
from `project.user.table`
)
select * from tab1 inner join `project2.table2.{start_date}` as B on tab1.product_id = B.p_id
After suggestions I have tried the following query to tighten things up, but it is sadly not working.
with tab1 as (
select
cast(product_id as INT64) as product_id_64,
cast(FORMAT_DATE('%Y%m%d', CAST(start_date AS DATE)) as STRING) as start_date_string
from `project.user.table`
)
select * from `user2.dataset.*` b
inner join tab1
on b._TABLE_SUFFIX = tab1.start_date_string
led to the following error:
Error running query. Cannot read field of type STRING as INT64 Field: GTIN
If your table is a native BigQuery one you can try to test and use a wildcard:
WITH tab1 as (
select
product_id,
start_date,
from `project.user.table`
)
SELECT *
FROM `<yourproject>.<yourdataset>.*` b
INNER JOIN tab1
ON b._TABLE_PREFIX = tab1.start_date
AND b.p_id = tab1.product_id
I can't test unfortunately

How to get records with distinct column value from result set in oracle?

could you please help me on the below query?
Using the query
SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, BaseAddOns.Id
from LineGernal Inner Join
BaseAddOns
on LineGernal.Id=BaseAddOns.ParentLineGernalID
Output-
Result Needed-
Thanks
Rajendra
You can use group by and min as follows:
SELECT LineGernal.Id, LineGernal.Description, LineGernal.SSMS, min(BaseAddOns.Id) as id
from LineGernal Inner Join
BaseAddOns
on LineGernal.Id=BaseAddOns.ParentLineGernalID
GROUO BY LineGernal.Id, LineGernal.Description, LineGernal.SSMS
I would recommend pre-aggregation in a subquery:
select li.id as lineGeneralId, lg.description, lg.ssms, bao.id as BaseAddOnsId
from LineGernal lg
inner join (
select ParentLineGernalID, min(id) as id
from BaseAddOns
group by ParentLineGernalID,
) bao on lg.id = bao.ParentLineGernalID

How to use multiple count and where condition sql server 2008?

I have this two query
1.
select CL_Clients.cl_id,CL_Clients].cl_name,COUNT(*) AS number_of_orders
from CL_Clients,CLOI_ClientOrderItems
where CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id
2.
select CL_Clients.cl_id,count(cloi_current_status) as dis
from CLOI_ClientOrderItems,CL_Clients
where cloi_current_status]='12'
and CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id,CLOI_ClientOrderItems.cloi_current_status
i have this column i need to put count function and where condition
[cloi_current_status]
166
30
30
30
150
150
150
150
150
150
150
Quite simple, you just encapsulate the queries and give their result sets an alias and then do a JOIN between their aliases on the column that is common. (In the query below I assume you'll be joining by client id)
SELECT *
FROM (
SELECT CL_Clients.cl_id,
CL_Clients].cl_name,
COUNT(*) AS number_of_orders
FROM CL_Clients,
CLOI_ClientOrderItems
WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
GROUP BY CL_Clients.cl_name,
CL_Clients.cl_id
) A
INNER JOIN (
SELECT CL_Clients.cl_id,
count(cloi_current_status) AS dis
FROM CLOI_ClientOrderItems,
CL_Clients
WHERE cloi_current_status] = '12'
AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
GROUP BY CL_Clients.cl_name,
CL_Clients.cl_id,
CLOI_ClientOrderItems.cloi_current_status
) B
ON A.cl_id = B.cl_id
WHERE ...
GROUP BY ...
This will be treated as a separate result set, so you can also filter results with a WHERE or just a GROUP BY, just like in a normal SELECT.
UPDATE:
To answer the question in your comments, when you join two tables that have a column with the same value and use
SELECT * FROM A INNER JOIN B the * will show all columns returned by the join, meaning all columns from A and all columns from B, this is why you have duplicate columns.
If you want to filter the columns returned you can specifiy which columns you want returned. So, in your case, the top SELECT * can be replaced with
SELECT A.cl_id, A.cl_name, A.number_of_orders, B.dis so, your query becomes:
SELECT A.cl_id, A.cl_name, A.number_of_orders, B.dis
FROM (
SELECT CL_Clients.cl_id,
CL_Clients].cl_name,
COUNT(*) AS number_of_orders
FROM CL_Clients,
CLOI_ClientOrderItems
WHERE CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
GROUP BY CL_Clients.cl_name,
CL_Clients.cl_id
) A
INNER JOIN (
SELECT CL_Clients.cl_id,
count(cloi_current_status) AS dis
FROM CLOI_ClientOrderItems,
CL_Clients
WHERE cloi_current_status] = '12'
AND CL_Clients.cl_id = CLOI_ClientOrderItems.cl_id
GROUP BY CL_Clients.cl_name,
CL_Clients.cl_id,
CLOI_ClientOrderItems.cloi_current_status
) B
ON A.cl_id = B.cl_id
UPDATE #2:
For your last question, you need to GROUP BY at the end of the big query and use a HAVING condtion, like this:
GROUP BY A.cl_id, A.cl_name, A.number_of_orders, B.dis
HAVING COUNT(cloi_current_status) > 100
All depends on what data you are trying to get, but you can go about it like this.
SELECT Column_x, Column_y, etc..
FROM ClL_Clients a
JOIN (select CL_Clients.cl_id,CL_Clients].cl_name,COUNT(*) AS number_of_orders
from CL_Clients,CLOI_ClientOrderItems
where CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id) b
on a.cl_id = b.cl_id
JOIN (select CL_Clients.cl_id,count(cloi_current_status) as dis
from CLOI_ClientOrderItems,CL_Clients
where cloi_current_status]='12'
and CL_Clients.cl_id=CLOI_ClientOrderItems.cl_id
group by CL_Clients.cl_name,CL_Clients.cl_id,CLOI_ClientOrderItems.cloi_current_status) c
on a.cl_id = c.cl_id
Group by BLAH BLAH
Hope this gets you in the right direction.

Inner query in like statement

I have a query like:
SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data like '%2012177%'
here rather than hard coding dataId value '2012177', i need to get this value from query like:
SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01'.
How can i do that?
you could probably try :
SELECT data FROM MSG_1 WHERE created>'2014-02-24' and data in
(SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01')
SELECT m.data
FROM MSG_1 m
JOIN (SELECT distinct dataId as d
FROM Item
where Src='MKT' and ValueDt>'2014-02-01'
) t
ON m.data like '%'+t.d+'%'
WHERE m.created>'2014-02-24'
edited to suit sql server.
Hope this helps:
SELECT
data
FROM
MSG_1 M1
JOIN (SELECT distinct dataId FROM Item where Src='MKT' and ValueDt>'2014-02-01') M2
ON M1.Data = concat('%', M2.dataId ,'%')
WHERE
M1.created>'2014-02-24'

sql query to get distinct records based on created date

I have some records as in the image. I want to write a sql query to fetch the latest unique records so that I only have two records on the basis of UserAlertTicketID and latest Created Date. In this case the output would be like:
"CB23C56F-B067-415E-AD62-59DF4DA4F26D" "2" "2011-03-04 09:49:59.440" "9EDB3DBC-4685-414D-A48B-04CA8285A2D1"
"9FA4D72B-8BB3-4CE9-BCA2-C334AF47EB30" "3" "2011-03-04 09:05:46.817" "94C67A9C-3818-4AB5-A6F6-CD7BD69FAEC7"
Kindly Help!!!
Thanks
SELECT TOP 2 * FROM table GROUP BY UserAlertTicketID ORDER BY CreatedDate DESC
EDIT: this may not do what you want, however try this:
SELECT TOP 2 T1.* FROM Table T1 LEFT JOIN Table T2
ON (T1.AlertTicketEventID = T2.AlertTicketEventID AND T1.CreatedDate < T2.CreatedDate)
WHERE T2.AlertTicketEventID IS NULL ORDER BY T1.CreatedDate DESC
This method will avoid using aggregates and should be much faster.
Something like this?
SELECT
a.AlertTicketEventId,
a.AlertTicketStatusID,
a.CreatedDate,
a.UserAlertTicketID
FROM <Table> a
INNER JOIN (
SELECT
AlertTicketStatusID
,MAX(CreatedDate) AS LastDate
FROM <Table>
GROUP BY AlertTicketStatusID
) b ON a.AlertTicketStatusID = b.AlertTicketStatusID AND a.CreatedDate = b.CreatedDate