Showing posts with label customers. Show all posts
Showing posts with label customers. Show all posts

Sunday, March 25, 2012

Distinct on single column?

Hi,

This is a query that joins a vouple of tables to display all the products purchased by a group of customers and the price they paid for it.

SELECT DISTINCT (p.code),p.descript_1 + ' ' + p.descript_2 + ' ' + p.descript_3 as description,sol.p_sales as price,sol.q_ordered as quantity,(sol.p_sales * sol.q_ordered) as total,so.date_in as dateFROM EfasLive..debtor AS d

INNER JOIN Informatica..so AS so ON so.deb_code = d.code AND so.co_code = d.co_code

INNER JOIN Informatica..so_line AS sol ON sol.code = so.code AND sol.co_code = so.co_code AND sol.acc_year = so.acc_year AND sol.efas = so.efas

INNER JOIN EfasLive..part AS p ON p.code = sol.part

WHERE d.[grp{003}] = 'GROUP' AND p.co_code = 1 AND p.code NOT LIKE '&%' AND so.date_in > DATEADD(m,-3,GETDATE()) AND sol.q_ordered > 0

ORDER BY (p.code), datum DESC

The problem with this is that it returns multiple lines for every product (p.code). Like so:

code description price quantity total date

603244 description_1 17.950000 150.000000 2692.500000000000 2007-08-01 00:00:00

603244 description_1 17.950000 150.000000 2692.500000000000 2007-07-10 00:00:00

603245 description_2 17.950000 40.000000 718.000000000000 2007-07-24 00:00:00

603245 description_2 17.950000 25.000000 448.750000000000 2007-07-16 00:00:00

603663 description_3 16.890000 27.000000 456.030000000000 2007-07-20 00:00:00

603663 description_3 16.890000 150.000000 2533.500000000000 2007-07-10 00:00:00

603663 description_3 16.890000 30.000000 506.700000000000 2007-07-03 00:00:00

I'd like there to be only 1 line for every different code with it's description. The idea is that the other rows are dropped and that only the first one remains. The one with the most recent purchase. I tried with GROUP BY but that's probably wrong since you'd have to add all the other columns as well and you end up with the same one. And even with adding a HAVING at the end I can't see how this could be solved Tongue Tied

edit: There aren't any actual relationships in the tables (it's ancient you see ...) I'm using SQL 2005 though.

Hello

Just a few ideas, do not have an MSSQL instace nearby to test:

1) use cursor, which I'd prefer to avoid

- declare cursor for "Select Distinct (code) From EfasLive..part"

- for each cursor value do the select on joined tables where date = MAx(date) to get the most recent value

2) do something like

Select ....
From (Select Distinct (code) From EfasLive..part) as p Inner Join... (the rest of the tables)...
Where date = Max(date)

The idea is to join distinct "code" values with other tables and filter only the most recent one for each table (that's what "Max(date)" is for)

3) try to use CTEs (Common Table Expressions)

Post the solution after you find one! Tnx
|||

Hmz I'll try out some of this stuff. Thx! But the multiple instances of code don't come from Efaslive..part. They are actually from so_line. An so_line is actually an orderline. For every order there could be multiple lines each containing a different product. But since it's over a timespan of 3 months it will include multiple orders and thus multiple so_lines containing the same product (once for every order it was in). So doing a distinct on code in Efaslive..part probably won't work. Or at least it doesn't make sense to me Smile I'll most definitely look into CTEs and post my findings or a solution.

edit: actually this can be simplified ... just pretend that the result I get is a simple select query from a single table. As if it was a CTE Smile Even then I'd have no clue how to drop the older records Tongue Tied The only technique I know is to group them but then you'd have to use MAX or COUNT or AVG or whatever .. and then I wouldn't have the correct price and/or date. So you wouldn't realy be dropping them.

I'll look into the pointer thing.

|||

Here the query,

Code Snippet

;With CTE

as

(

SELECT DISTINCT

p.code

, p.descript_1 + ' ' + p.descript_2 + ' ' + p.descript_3 as description

, sol.p_sales as price

, sol.q_ordered as quantity

, sol.p_sales * sol.q_ordered as total

, so.date_in as date

, max(so.date_in) over(partition by p.code) as maxdate

--, Row_Number() over(partition by p.code order by so.date_in desc) rid

FROM

EfasLive..debtor AS d

INNER JOIN Informatica..so AS so

ON so.deb_code = d.code

AND so.co_code = d.co_code

INNER JOIN Informatica..so_line AS sol

ON sol.code = so.code

AND sol.co_code = so.co_code

AND sol.acc_year = so.acc_year

AND sol.efas = so.efas

INNER JOIN EfasLive..part AS p

ON p.code = sol.part

WHERE

d.[grp{003}] = 'GROUP'

AND p.co_code = 1

AND p.code NOT LIKE '&%'

AND so.date_in > DATEADD(m,-3,GETDATE())

AND sol.q_ordered > 0

)

Select

code

, description

, price

, quantity

, total

, date

From

CTE

Where

date = maxdate

--rid=1

distinct mvp

hi all,
i use a query that returns a set of projects and customers for my
report. i am using a multi value parameter to filter the projects in
the data. because the grouping is done in the report itself, when i
try to set the mvp from my query each project apears multiple times on
the dropdown list. is there a way to get the distinct projects from
the query? - to get each project to apear once?.
i dont want to create another dataset to get the distinct projects
becuase the query is quite heavy.
or maybe is there a way to create another dataset to query from my
main dataset?
thanks in advance
offaYou can't do a query on an existing dataset. For my parameter lists I have
dedicated datasets. Perhaps if all the query is doing is getting is the
distinct projects it won't be a compute intensive query. Also, you might
want to see what sort of index you have on the table.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
<offa23@.hotmail.com> wrote in message
news:1188265077.769587.273140@.k79g2000hse.googlegroups.com...
> hi all,
> i use a query that returns a set of projects and customers for my
> report. i am using a multi value parameter to filter the projects in
> the data. because the grouping is done in the report itself, when i
> try to set the mvp from my query each project apears multiple times on
> the dropdown list. is there a way to get the distinct projects from
> the query? - to get each project to apear once?.
> i dont want to create another dataset to get the distinct projects
> becuase the query is quite heavy.
> or maybe is there a way to create another dataset to query from my
> main dataset?
> thanks in advance
> offa
>

Thursday, March 22, 2012

Distinct Count of Customers

Hi All,

We are following the code given below

triptype customerid
van 24
van 25
bus 24
van 24
van 25

if triptype='van' then
customerid
else
0

We r making distinct count of above formula
now we getting distinct count as 3(Include 24,25 ,0)

but we need distinct count as 2 So there is any solution plz suggest meGroup the report by triptype and Right click on the customerid column; Insert Summary;choose distinct count;

DISTINCT Count and NULL Processiong

In short, I have a measure that counts the distinct number of customers.

I recently learned that it's counting a NULL value as 1 and I don't want this. Are there some properties that can stop the NULL value from being counted? I tried playing with a few of the properties that I found, but no luck.

The only solution that I have thought of so far is creating another table in the DSV that explicitly filters out the NULL values in the WHERE clause and then using it as the source for the DISTINCT COUNT. However, I'd like to avoid this solution if possible.

Maybe some MDX in a Calculated Member is needed? If so, please provide the pseudo-MDX as my MDX is definitely weak.

Any thoughts?

By the way, it's a AS2005 cube using SQL 2005 Standard Edition.

How about creating a new, hidden dimension (or attribute on your customer dimension) with two members: one member represents rows in the fact table where your Customer is null, and one for where it's not null. What you could then do is keep your distinct count measure as it is, but then overwrite it in the MDX Script so it only returns the value for the not-null member - something like this:

(Measures.[Customer Count])=(Measures.[Customer Count], [Hidden Dimension].[Hidden Attribute].[Not Null Customers]);

HTH,

Chris

|||Or, even better, make [Not Null Customers] member to be a default member in that dimension, and then you won't need to do anything in MDX Script at all, and it will be better for performance. My preference, however, is to do the DSV change that you want to avoid, I think that is the best solution.

DISTINCT COUNT - unhelpful error message

Hi,
I need to return a distinct count of customers who have ordered goods.
To do this I created a measure of type DISTINCT COUNT, over the CustomerID field in the orders fact table. Being a foreign key for the customers dimension table, the customerID is integer, not-null, and therefore ideal for the purpose.
The measue is created in a new measure group OK, and the cube processes through OK. However when I come to view the data by dragging the new measure onto the columns in the VS browser I get the wonderful message:

"
The query could not be processed: o Internal error: An unexpected exception occured.

"
Doing exactly the same creation process with the ProductID field works fine, and gives the expected results. I've looked at the tables and can see nothing obviously wrong with the data. The only difference is that there are about 3,800 products, and about 1,000,000 customers. However I did the same thing on the prototypes with no problem at all. Version is SQL 2005 Enterprise Ed.
Any thoughts?
Thanks as always,

Richard

Suggest you contact Analysis Services customer support for this situation.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

Sunday, March 11, 2012

Displaying Picture In Crystal Report

I want to display picture of customers in crystal reports.

For that purpose I have created table customer.

When new custiomer comes to me I take their picture using PC camera and save that picture in one of the folders.

At the same time I am sending path of that picture to database.

Now when I display record of customer in Crystal report. I want to diaplay their pictures tooo.

Can you help me??Place the OLE Object -> Bitmap Image on the report and right click the OLE Object ->Format Graphics -> Picture Tab and Graphic Location enter the path which is stored in the database in the formula. This will show the image in runtime.

Keep this in mind this functionality is available after Crystal Reports 10.

In the earlier versions you have to store the image as BLOB in the database and then show that field on the report.