Showing posts with label schema. Show all posts
Showing posts with label schema. Show all posts

Friday, March 30, 2012

Help with SQL Query

I need help building the following query..

My table has the following schema: eventID, typeID

Sample Rows:

1,1
1,2
1,3
2,1
3,2
3,2
4,3
4,4
5,2

I want to be able to query for all eventID's such that type = 2 and
type <> 1. So the result should be

3,2
4,2

The result should NOT include 1,2 because eventID 1 is also "related"
to typeID 1 and 3.Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications. Your personal pseudo-code is wrong at many levels; did
you mean this?

CREATE TABLE EventSchedules
(event_id INTEGER NOT NULL
REFERENCES Events (event_id),
event_type INTEGER NOT NULL
CHECK (event_type > 0), --assumption
PRIMARY KEY (event_id, event_type)); --requirement!

INSERT INTO EventSchedules VALUES (1,1);
INSERT INTO EventSchedules VALUES (1,2);
INSERT INTO EventSchedules VALUES (1,3);
INSERT INTO EventSchedules VALUES (2,1);
INSERT INTO EventSchedules VALUES (3,2);
INSERT INTO EventSchedules VALUES (3,2);-- removed dup row!!
INSERT INTO EventSchedules VALUES (4,3);
INSERT INTO EventSchedules VALUES (4,4);
INSERT INTO EventSchedules VALUES (5,2);

A data element name like "type_id" makes no sense. Either it is an
identifier for a particular kind of entity or it is some kind of code
for an attribute. It cannot be both an attribute and an entity. You
might want to get a book on data modeling and the ISO-11179 Standards.

>> I want to be able to query for all event_id's such that event_type = 2 and event_type <> 1. <<

Here is one way.

SELECT event_id
FROM EventSchedules
GROUP BY event_id
HAVING MIN(event_type) > 1
AND MAX (CASE WHEN event_type <> 2 THEN 0 ELSE 2 END) = 2;

And I am sure that someone will come up with a self-join solution, too.|||b_naick@.yahoo.ca wrote:
> I need help building the following query..
> My table has the following schema: eventID, typeID
> Sample Rows:
> 1,1
> 1,2
> 1,3
> 2,1
> 3,2
> 3,2
> 4,3
> 4,4
> 5,2
> I want to be able to query for all eventID's such that type = 2 and
> type <> 1. So the result should be
> 3,2
> 4,2
> The result should NOT include 1,2 because eventID 1 is also "related"
> to typeID 1 and 3.

--BEGIN PGP SIGNED MESSAGE--
Hash: SHA1

Shouldn't the last pair be 5,2, since there isn't any 4,2.

Try,

SELECT DISTINCT eventID, typeID
FROM t as t1
WHERE typeID = 2
AND eventID NOT IN (SELECT eventID FROM t WHERE typeID != 2)

Change table name "t" to your table's true name.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

--BEGIN PGP SIGNATURE--
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQpTZ5IechKqOuFEgEQKIrgCfeW81ytgRIXUnl//jAA0RU8zZwLQAoOPN
UBuYtqvs/JqhLjVuFIYYTzqF
=aqWw
--END PGP SIGNATURE--|||(b_naick@.yahoo.ca) writes:
> I need help building the following query..
> My table has the following schema: eventID, typeID
> Sample Rows:
> 1,1
> 1,2
> 1,3
> 2,1
> 3,2
> 3,2
> 4,3
> 4,4
> 5,2
> I want to be able to query for all eventID's such that type = 2 and
> type <> 1. So the result should be
> 3,2
> 4,2
> The result should NOT include 1,2 because eventID 1 is also "related"
> to typeID 1 and 3.

I assume that desired result is

3,2
5,2

Else there is something I don't understand at all.

This could be a good query:

SELECT *
FROM tbl a
WHERE a.type = 2
AND NOT EXISTS (SELECT *
FROM tbl b
WHERE a.eventID = b.eventID
AND EXISTS (SELECT *
FROM tbl c
WHERE c.eventID = b.eventID
ABD c.type = 1))

Since you did not include CREATE TABLE and INSERT statements, I
have not tested this.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Friday, February 24, 2012

Help with linked server query

I have two tables in a linked server that I am trying to accomplish the following:

Server is called Medic, schema is informix, catalog is v001
table names are findet and findhdr

1) join indices (patkey and invno)
2) distinct count (finhdr.patid)
3) sum currency(findet.amount)
4) within a date range(findet.ar_date)
5) having specific criteria (findet.proccode = "99201 thru 99205")

Any help is greatly appreciated!

JTremember,in SQL Server,Microsoft using four-section to describe the database object.It's [server].[database].[owner].[object_name].
example,linkserver Access,table name is MyTable,u must write [server_access]...[MyTable].
link server oracle,user is scott,table is employee,u must write [server_oracle].[].[scott].[employee].

I have no knowledge about informix.But I think u can know how to describe ur table according my introduction,Right?|||Originally posted by enhydraboy
remember,in SQL Server,Microsoft using four-section to describe the database object.It's [server].[database].[owner].[object_name].
example,linkserver Access,table name is MyTable,u must write [server_access]...[MyTable].
link server oracle,user is scott,table is employee,u must write [server_oracle].[].[scott].[employee].

I have no knowledge about informix.But I think u can know how to describe ur table according my introduction,Right?

Thank you for the response. It is not what I was looking for. If anyone else can help, I would appreciate it.

Sunday, February 19, 2012

Help with Join Query Please

Hi All,
I need help figuring out a query.
I have two tables:
REQ and PO
The schema here is that there can be multiple PO rows per a single REQ row.
example:
REQ PO
--
Requistion1
PO1
PO2
PO3
I am creating a query that counts the number of REQ ROWS per Person
and the number of NULL Columns in the Row, including the PO Columns.
My problem is that the [Total Requests] SUM includes the PO Records and I
don't wan that.
Below is my query. Any help would be greatly appreciated.
Thanks,
John.
/*--*/
SELECT Assignedto,
COUNT(*) AS [Total Requests],
PRStatus AS [Status],
SUM(CASE WHEN (Region Is Null) Then 1 Else 0 End) AS [Region],
SUM(CASE WHEN (OrderList Is Null) Then 1 Else 0 End) AS [OrderList],
SUM(CASE WHEN (ClientName Is Null) Then 1 Else 0 End) AS [Client Name],
SUM(CASE WHEN (PRStatus Is Null) Then 1 Else 0 End) AS [PRStatus ],
/*---*/
/*PO Data*/
SUM(CASE WHEN (P.Supplier Is Null) Then 1 Else 0 End) AS [Supplier],
SUM(CASE WHEN (P.PONumber Is Null) Then 1 Else 0 End) AS [PO Number],
SUM(CASE WHEN (P.DatePOCreated Is Null) Then 1 Else 0 End) AS [Date PO
Created],
SUM(CASE WHEN (P.DatePOAnticipatedShip Is Null) Then 1 Else 0 End) AS [Date
PO Anticipated Ship],
/*---*/
FROM INTReq R
LEFT OUTER JOIN INTReq_PO P ON R.RID = P.RID
WHERE (PRStatus <> 'Cancelled')
GROUP BY Assignedto, PRStatus
Order By Assignedto, PRStatusJohn wrote:
> Hi All,
> I need help figuring out a query.
> I have two tables:
> REQ and PO
> The schema here is that there can be multiple PO rows per a single
> REQ row. example:
> REQ PO
> --
> Requistion1
> PO1
> PO2
> PO3
I think I understand what you mean, but you should eliminate all ambiguity
by following the recommendations here: www.aspfaq.com/5006

> I am creating a query that counts the number of REQ ROWS per Person
> and the number of NULL Columns in the Row, including the PO Columns.
> My problem is that the [Total Requests] SUM includes the PO Records
> and I don't wan that.
> Below is my query. Any help would be greatly appreciated.
>
Now I'm really lost. Please provide DDL, sample data and desired results (in
tabular format)
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||TRY
count(distinct REQ) as 'TotalRequest'
"John" <jrugo@.patmedia.net> wrote in message
news:uvCTGe2zFHA.3408@.TK2MSFTNGP09.phx.gbl...
> Hi All,
> I need help figuring out a query.
> I have two tables:
> REQ and PO
> The schema here is that there can be multiple PO rows per a single REQ
> row.
> example:
> REQ PO
> --
> Requistion1
> PO1
> PO2
> PO3
> I am creating a query that counts the number of REQ ROWS per Person
> and the number of NULL Columns in the Row, including the PO Columns.
> My problem is that the [Total Requests] SUM includes the PO Records and I
> don't wan that.
> Below is my query. Any help would be greatly appreciated.
> Thanks,
> John.
> /*--*/
> SELECT Assignedto,
> COUNT(*) AS [Total Requests],
> PRStatus AS [Status],
> SUM(CASE WHEN (Region Is Null) Then 1 Else 0 End) AS [Region],
> SUM(CASE WHEN (OrderList Is Null) Then 1 Else 0 End) AS [OrderList],
> SUM(CASE WHEN (ClientName Is Null) Then 1 Else 0 End) AS [Client Name],
> SUM(CASE WHEN (PRStatus Is Null) Then 1 Else 0 End) AS [PRStatus ],
> /*---*/
> /*PO Data*/
> SUM(CASE WHEN (P.Supplier Is Null) Then 1 Else 0 End) AS [Supplier],
> SUM(CASE WHEN (P.PONumber Is Null) Then 1 Else 0 End) AS [PO Number],
> SUM(CASE WHEN (P.DatePOCreated Is Null) Then 1 Else 0 End) AS [Date PO
> Created],
> SUM(CASE WHEN (P.DatePOAnticipatedShip Is Null) Then 1 Else 0 End) AS
> [Date PO Anticipated Ship],
> /*---*/
> FROM INTReq R
> LEFT OUTER JOIN INTReq_PO P ON R.RID = P.RID
> WHERE (PRStatus <> 'Cancelled')
> GROUP BY Assignedto, PRStatus
> Order By Assignedto, PRStatus
>
>|||Yup, that did the trick :)
Thank you very much for you help.
John.
"Kevin" <pearl_77@.hotmail.com> wrote in message
news:e3zsPK3zFHA.2912@.TK2MSFTNGP10.phx.gbl...
> TRY
> count(distinct REQ) as 'TotalRequest'
>
> "John" <jrugo@.patmedia.net> wrote in message
> news:uvCTGe2zFHA.3408@.TK2MSFTNGP09.phx.gbl...
>