Showing posts with label struggling. Show all posts
Showing posts with label struggling. Show all posts

Friday, March 30, 2012

Help with SQL Query

I'm really struggling with this and hoping someone can help me AND I need to restate that I am a SQL noob!

First attempt:
This query is returning all the data I need and then some! There are two fields I can use to try to filter on InventoryDate and RefCodeID. As you will see in the next example I've tried without success to get the info I need.

Code Snippet

SELECT B.BlockName AS Block,

I.Lot,

R.RefCodeName AS LotType,

I.SaleableFrontFootage As Frontage,

ISNULL(IA.Address + ', ','') + IA.CityName + ' ' + IA.ProvinceCode + ' ' + ISNULL(IA.PostalCode,'') AS Address,

AI.HouseStyle AS Style,

RC.RefCodeName AS Status,

PUR.PurchaserName as Builder

FROM Inventory AS I

JOIN Block AS B

INNER JOIN Phase AS P

INNER JOIN ProjectSub AS PS

INNER JOIN Project AS PJ

ON PS.ProjectID = PJ.ProjectID

ON P.ProjectSubID = PS.ProjectSubID

INNER JOIN PhaseSetup AS PSetup

ON P.PhaseID = PSetup.PhaseID

ON B.PhaseID = P.PhaseID

ON I.BlockID = B.BlockID

LEFT JOIN InventoryDate AS IStatus

ON I.InventoryID = IStatus.InventoryID

AND IStatus.RefCodeID IN (71, 73) -- Open, Spec, Sale

LEFT JOIN RefCode AS R

ON I.LotTypeRefCodeID = R.RefCodeID

LEFT JOIN dbo.BuilderSaleByInventory(NULL) AS BSale

ON I.InventoryID = BSale.InventoryID

LEFT JOIN dbo.InventoryAddressByInventoryID(NULL) IA

ON I.InventoryID = IA.InventoryID

LEFT JOIN dbo.PurchaserByInventory(NULL) AS PUR

ON I.InventoryID = PUR.InventoryID

LEFT JOIN ArchitectureInformation as AI

ON I.InventoryID = AI.InventoryID

LEFT JOIN RefCode AS RC

ON IStatus.RefCodeID = RC.RefCodeID

WHERE PJ.ProjectName = 'Copperfield'

AND P.PhaseID IN (114, 119, 120)

AND Pur.PurchaserName NOT LIKE '%HRC%'

Second attempt:
I've tried adding a select statement in the select statement but it's not working for me, if you have any questions please ask, I'm not sure what Info you will need to help me solve this puzzle.

Code Snippet

SELECT P.PhaseName,

B.BlockName AS Block,

I.Lot,

R.RefCodeName AS LotType,

I.SaleableFrontFootage As Frontage,

ISNULL(IA.Address + ', ','') + IA.CityName + ' ' + IA.ProvinceCode + ' ' + ISNULL(IA.PostalCode,'') AS Address,

AI.HouseStyle AS Style,

-- RC.RefCodeName AS Status,

(SELECT RC.RefCodeName AS Status

FROM InventoryDate AS IDate

JOIN RefCode AS RC

ON IDate.RefCodeID = RC.RefCodeID

WHERE IDate.InventoryID >= BSale.InventoryDate

AND IDate.InventoryDate = (SELECT MAX(InventoryDate)

FROM InventoryDate AS IDate2

WHERE IDate.InventoryID = IDate2.InventoryID)) AS 'Status',

PUR.PurchaserName as Builder

FROM Inventory AS I

JOIN Block AS B

INNER JOIN Phase AS P

INNER JOIN ProjectSub AS PS

INNER JOIN Project AS PJ

ON PS.ProjectID = PJ.ProjectID

ON P.ProjectSubID = PS.ProjectSubID

INNER JOIN PhaseSetup AS PSetup

ON P.PhaseID = PSetup.PhaseID

ON B.PhaseID = P.PhaseID

ON I.BlockID = B.BlockID

LEFT JOIN RefCode AS R

ON I.LotTypeRefCodeID = R.RefCodeID

LEFT JOIN dbo.InventoryAddressByInventoryID(NULL) IA

ON I.InventoryID = IA.InventoryID

LEFT JOIN dbo.PurchaserByInventory(NULL) AS PUR

ON I.InventoryID = PUR.InventoryID

LEFT JOIN ArchitectureInformation as AI

ON I.InventoryID = AI.InventoryID

LEFT JOIN InventoryDate AS BSale

ON I.InventoryID = BSale.InventoryID

AND BSale.RefCodeID IN (70, 71, 73)

WHERE PJ.ProjectName = 'Copperfield'

AND P.PhaseID IN (114, 119, 120)

AND Pur.PurchaserName NOT LIKE '%HRC%'

any and all help is appreciated

Thanks

Wade

Wade:

In addition to your queries what we also need is (1) sample source data and (2) what the target output should look like; It is rather like you have given us a gun without any munitions and without a target and said, "Shoot!"

You also need to give us the function definitions for the two table functions.

|||

Hi WadeG,

When I write code, I break it down to simpler levels. As I get the data I need, I add more of the code and until I have it the way I need it. The problem with this is the level of complication. Break it down, and either eliminate or add code that will better define your query. Not a solution, but a method that will lead to the solution.

dbmsql

|||

This query may fix your problem,

Code Snippet

SELECT B.BlockName AS Block,

I.Lot,

R.RefCodeName AS LotType,

I.SaleableFrontFootage As Frontage,

ISNULL(IA.Address + ', ','') + IA.CityName + ' ' + IA.ProvinceCode + ' ' + ISNULL(IA.PostalCode,'') AS Address,

AI.HouseStyle AS Style,

RC.RefCodeName AS Status,

PUR.PurchaserName as Builder

FROM Inventory AS I

JOIN Block AS B

INNER JOIN Phase AS P

INNER JOIN ProjectSub AS PS

INNER JOIN Project AS PJ

ON PS.ProjectID = PJ.ProjectID

ON P.ProjectSubID = PS.ProjectSubID

INNER JOIN PhaseSetup AS PSetup

ON P.PhaseID = PSetup.PhaseID

ON B.PhaseID = P.PhaseID

ON I.BlockID = B.BlockID

LEFT JOIN InventoryDate AS IStatus

ON I.InventoryID = IStatus.InventoryID

AND IStatus.RefCodeID IN (71, 73) -- Open, Spec, Sale

LEFT JOIN RefCode AS R

ON I.LotTypeRefCodeID = R.RefCodeID

LEFT JOIN dbo.BuilderSaleByInventory(NULL) AS BSale

ON I.InventoryID = BSale.InventoryID

LEFT JOIN dbo.InventoryAddressByInventoryID(NULL) IA

ON I.InventoryID = IA.InventoryID

LEFT JOIN dbo.PurchaserByInventory(NULL) AS PUR

ON I.InventoryID = PUR.InventoryID AND Pur.PurchaserName NOT LIKE '%HRC%'

LEFT JOIN ArchitectureInformation as AI

ON I.InventoryID = AI.InventoryID

LEFT JOIN RefCode AS RC

ON IStatus.RefCodeID = RC.RefCodeID

WHERE PJ.ProjectName = 'Copperfield' AND P.PhaseID IN (114, 119, 120)

|||

The problem (I think) is with this piece of code:

Code Snippet

(SELECT RC.RefCodeName AS Status

FROM InventoryDate as IDate

JOIN RefCode AS RC

ON IDate.RefCodeID = RC.RefCodeID AND IDate.RefCodeID IN (70, 71, 73)

WHERE IDate.InventoryID = I.InventoryID

AND IDate.InventoryDate = (SELECT MAX(InventoryDate)

FROM InventoryDate AS IDate2

WHERE IDate.InventoryID = IDate2.InventoryID)) AS 'Status',

What I need to do is be able to pick the latest (MAX) date transaction, I just can't seem to get it to work. There are several tables involved, I can try to post whatever info you need.

Bascally the query with out the above code (Just using RC.RafCodeName AS Status) works it's just returning too many results.

I hope that makes sense.

Friday, March 9, 2012

Help with Query

I am struggling to build the appropriate query for the following scenario. A
simplified version of my problem is... consider the following table
EEID EPID StartDate
1 11 1/1/2005
1 13 2/1/2005 *
2 13 1/1/2005 *
3 14 2/1/2005
3 11 4/1/2005 *
The star indicates the records that should be pulled, and I need all 3
fields. The condition is, for each EEID pull the record with the largest
date.
If i do something like
select EEID, max(StartDate) from table group by EEID
I get the appropriate row, but i am missing one field.
Please help. I have struggled long and hard on this one.
thank You.
PooravI am , what field are you missing?
Message posted via http://www.webservertalk.com|||Select * From Table T
Where StartDate = (Select Max(StartDate)
From Table
Where EEID = T.EEID)
"haiiyaa" wrote:

> I am struggling to build the appropriate query for the following scenario.
A
> simplified version of my problem is... consider the following table
> EEID EPID StartDate
> 1 11 1/1/2005
> 1 13 2/1/2005 *
> 2 13 1/1/2005 *
> 3 14 2/1/2005
> 3 11 4/1/2005 *
> The star indicates the records that should be pulled, and I need all 3
> fields. The condition is, for each EEID pull the record with the largest
> date.
> If i do something like
> select EEID, max(StartDate) from table group by EEID
> I get the appropriate row, but i am missing one field.
> Please help. I have struggled long and hard on this one.
> thank You.
> Poorav
>

Wednesday, March 7, 2012

Help with pivot table

Hi!

I`ve been struggling to find a solution to make a report from MS SQL, and I think pivot table is the key but I`m not sure on how to do this. Hope someone can help, here is the case :

I have one table "Products" and one table "Packages". Every product in the Products table has 1 - * Packages (unit which the product is sold by)

The Products table looks something like this :

Id | productText | productGroup | etc

And the Packages table like this :

Id | productId | packageType | weight | etc

What I want to do is to flat this out so I can get the Product and all it`s packages in one record. Something like this :

productText | productGroup | packageType | weight | packageType | weight | etc.

I would really appriciate if someone could help me out with this.

Regards,
KnutYou need to use DECODE for the purpose.

Sunday, February 19, 2012

Help with joining records

Table A Table B

BOL# B_BOL#

Chargeback#
Hi All,
I have been struggling with this for the past few months. I have two
tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
the problem...When there are chargeback# fields associated with B_BOL#
they aren't being captured as additional records. None of my tables
have primary keys because at any given time any field can contain a
"null" value, so I am unable to assign a foreign key, I think. How can
I attach the subsequent records to the main record to bring back the
additonal data? Please help!!!Not sure this will help, because I'm not sure, did I fully understood
your problem.
If what you mean is like fallow:

B_BOL# from Table B can be a key either with BOL# or/and Chargeback#,
then I will try as fallow:

select * from TableB B
left join TableA A1
on B.B_BOL# = A1.BOL#
left join TableA A2
on B.B_BOL# = A2.Chargeback#

If you do not want to have doble records, you can ommite it with using
is not null (if it is always like this, that the BOL# is null when
chargeback# isn't null ... and opposite).

Or You can use case statement, by removing null records.

In the future, will be better, if you put the script, or some example
data, if the script will be to complicated.

Best regards

Matik

Rnt6872 napisal(a):

Quote:

Originally Posted by

Table A Table B
>
BOL# B_BOL#
>
Chargeback#
Hi All,
I have been struggling with this for the past few months. I have two
tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
the problem...When there are chargeback# fields associated with B_BOL#
they aren't being captured as additional records. None of my tables
have primary keys because at any given time any field can contain a
"null" value, so I am unable to assign a foreign key, I think. How can
I attach the subsequent records to the main record to bring back the
additonal data? Please help!!!

|||Rnt6872 (r_fordjr@.msn.com) writes:

Quote:

Originally Posted by

Table A Table B
>
BOL# B_BOL#
>
Chargeback#
Hi All,
I have been struggling with this for the past few months. I have two
tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
the problem...When there are chargeback# fields associated with B_BOL#
they aren't being captured as additional records. None of my tables
have primary keys because at any given time any field can contain a
"null" value, so I am unable to assign a foreign key, I think. How can
I attach the subsequent records to the main record to bring back the
additonal data? Please help!!!


1) The standard recommendations for this type of questions, is that
you post a) CREATE TABLE statements for the tables involed. b) INSERT
statements with sample data. c) the desired result given the sample.
This sort of information helps tremendously to understand what you
want to achieve. Also it makes it very easy to develop a tested
solution.

2) If all your columns can be nullable at any time, you have a very
difficult data model to work with. Most people would probably say
that your data model is flat wrong. Maybe there is a good reason
to have all columns nullable, but it sounds very suspicious.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:

Quote:

Originally Posted by

Rnt6872 (r_fordjr@.msn.com) writes:
>

Quote:

Originally Posted by

>Table A Table B
>>
>>BOL# B_BOL#
>>
>>Chargeback#
>>Hi All,
>>I have been struggling with this for the past few months. I have two
>>tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
>>the problem...When there are chargeback# fields associated with B_BOL#
>>they aren't being captured as additional records. None of my tables
>>have primary keys because at any given time any field can contain a
>>"null" value, so I am unable to assign a foreign key, I think. How can
>>I attach the subsequent records to the main record to bring back the
>>additonal data? Please help!!!


>
>
1) The standard recommendations for this type of questions, is that
you post a) CREATE TABLE statements for the tables involed. b) INSERT
statements with sample data. c) the desired result given the sample.
This sort of information helps tremendously to understand what you
want to achieve. Also it makes it very easy to develop a tested
solution.
>
2) If all your columns can be nullable at any time, you have a very
difficult data model to work with. Most people would probably say
that your data model is flat wrong. Maybe there is a good reason
to have all columns nullable, but it sounds very suspicious.
>
>
>


that was so diplomatic...

Given:
"None of my tables have primary keys because at any given time any field can
contain a null"

Then I would say that whoever designed this needed to pass a few more classes in
database design. Your data model needs professional help. Period.

--
Michael Austin.
Database Consultant|||I think you should provide some real examples from your data also, what
you are trying to accomplish by 'bring back additional data'.

Rnt6872 wrote:

Quote:

Originally Posted by

Table A Table B
>
BOL# B_BOL#
>
Chargeback#
Hi All,
I have been struggling with this for the past few months. I have two
tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for
the problem...When there are chargeback# fields associated with B_BOL#
they aren't being captured as additional records. None of my tables
have primary keys because at any given time any field can contain a
"null" value, so I am unable to assign a foreign key, I think. How can
I attach the subsequent records to the main record to bring back the
additonal data? Please help!!!