Solved

SQl Query help for reports

Posted on 2011-03-09
8
181 Views
Last Modified: 2012-05-11

 i have a drop down list of customers in the report from where i can select one customer or all customers.If i select one
 customer then the report should be populated for that customer only.If i seelct all from the drop down list then the
report should be poulated for all the customers

Like it should be as follows

Customer name : harry id:1---------For one customer

Customer Name:All Customers-------------For all customers

For this i created a dataset called customer name and wrote the following queries for that data set


select c.customername
from tbl_customer c
union
select 'all'

where all= select customername from tbl_customer-------showed error here

0
Comment
Question by:sqlcurious
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35088489
Try like this to retrieve the data from database when the user selects a customer_name
select *
  from tbl_customer
 where CustomerName = case when @customer_name = 'ALL' then CustomerName else @customer_name end

Open in new window

To display the Customer names in the drop down, you can try like this.
select distinct customername from tbl_customer union all select 'all'

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35088586


yes i tried that one too but i am designing the report based on that .I have a dataset called customer name and wrote the same query as
      

select distinct customername from tbl_customer union all select 'all----to get customer from dropdown list
' it is not selecting all customers but only giving me the report based on random one customer

however my stored proc is showing data for multiple customers
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35088604
Can you post the SP code? Want to see how are you handling the 'ALL' parameter. If you have logic something like which I have mentioned, that should work.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:sqlcurious
ID: 35088617
following is the stored proc


ALTER  PROCEDURE [dbo].[DBSP_Report_GetLettingDate1]
(      
      @LettingDateFrom datetime,  
      @LettingDateTo datetime,  
      @ProjectTxDot int,  
      @CustomerName varchar(50)='',  
      @DivisionName varchar(250)=''
 )

AS

BEGIN
--declare @SQL varchar(8000)
--DECLARE @country varchar(255)

   
SELECT      CU.CustomerID,  
            CU.CustomerName,  
            P.ProjectID ,  
            P.Projectname,  
            p.ProjectNumber,  
            P.ProjectCountyID,  
            convert(varchar(10),P.ProjectStartDate,110) AS ProjectStartDate,  
            P.projectTxdot,  
            D.DivisionID,
            D.Divisionname As DivisionName,  
            --P.projectCountyID As CountyID,  
            C.CountyName As CountyName,  
            --CU.CustomerName As CustomerName,  
            --CU.customerID,  
            QI.QuoteItemQuantity as Quantity,  
            QI.QuoteItem_ItemNumber AS Item#,  
            QI.QuoteItemDescription As ItemDescription,  
            QI.QuoteItemQuantity As ItemQuantity,  
            QI.QuoteItemUnit As ItemUnit,  
            QI.QuoteItemUnitprice As UnitCost,  
            QI.QuoteItem_ItemTotalPrice As UnitPrice,  
            (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) AS TotalCost,        
            (QI.QuoteItemQuantity*QI.QuoteItem_ItemTotalPrice) AS TotalPrice,  
            (QI.QuoteItemWeight*QI.QuoteItemQuantity) AS TotalWeight,  
            QI.QuoteItemWeight AS ItemWeight,  
            QI.QuoteItemMarkupPercent AS MarkupPercent,  
            Q.QuoteFreightRate as FreightRate,  
            Q.QuoteNumberOfTrucks,  
            Q.QuoteSurcharge1,  
            Q.QuoteSurcharge2,  
            Q.QuoteSurcharge3,  
            ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) +(Q.QuoteSurcharge1+Q.QuoteSurcharge2+Q.QuoteSurcharge3)) AS TotalFreightforQuote,
            (((QI.QuoteItemQuantity*QI.QuoteItem_ItemTotalPrice)-(QI.QuoteItemQuantity * QI.QuoteItemUnitPrice)-((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) +(Q.QuoteSurcharge1+Q.QuoteSurcharge2+Q.QuoteSurcharge3)))/((QI.QuoteItemQuantity*QI.QuoteItem_ItemTotalPrice))*100) AS MARGIN
into #temptable
FROM      TBL_Project P INNER JOIN  TBL_Quotes1 Q
            ON P.ProjectID = Q.ProjectID  inner   JOIN  TBL_County C
            ON C.CountyID = P.ProjectCountyID inner JOIN  TBL_QuoteItems QI
            ON Q.QuoteID = QI.QuoteID INNER JOIN  TBL_Division D
            ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN  TBL_CustomerQuotes CU
            -- ON CU.QuoteID = Q.QuoteID  
            inner join tbl_customer cu on cu.customercreatedby=Q.QuoteCreatedby
WHERE            p.projectstartdate >= ISNULL(@LettingDateFrom,p.projectstartdate)
            and p.projectstartdate <= ISNULL(@LettingDateTo,p.projectstartdate)
            and D.DivisionName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE WHEN @DivisionName = '' OR @DivisionName IS NULL Then D.DivisionName Else @DivisionName END, ','))
            and p.ProjectTxDot = CASE WHEN @projectTxDot = '' Then p.ProjectTxDot Else @projectTxDot END
            and CU.CustomerName IN (SELECT Item FROM dbo.fneBid_SplitValue(CASE WHEN @CustomerName = 'all' Then CU.CustomerName Else @CustomerName END, ','))
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35088727
Can you check this?
ALTER PROCEDURE [dbo].[DBSP_REPORT_GETLETTINGDATE1]( 
               @LettingDateFrom DATETIME, 
                 @LettingDateTo DATETIME, 
                  @ProjectTxDot INT, 
                  @CustomerName VARCHAR(50)  = '', 
                  @DivisionName VARCHAR(250)  = '') 
AS 
  BEGIN 
    --declare @SQL varchar(8000)  
    --DECLARE @country varchar(255) 
    SELECT CU.CustomerID, 
           CU.CustomerName, 
           P.ProjectID, 
           P.Projectname, 
           p.ProjectNumber, 
           P.ProjectCountyID, 
           CONVERT(VARCHAR(10),P.ProjectStartDate,110)                                                                                                                                                                                                                                             AS ProjectStartDate,
           P.projectTxdot, 
           D.DivisionID, 
           D.Divisionname                                                                                                                                                                                                                                                                          AS DivisionName,
           --P.projectCountyID As CountyID, 
           C.CountyName                                                                                                                                                                                                                                          AS CountyName,
           --CU.CustomerName As CustomerName,    
           --CU.customerID, 
           QI.QuoteItemQuantity                                                                                                                                                                                                           AS Quantity,
           QI.QuoteItem_ItemNumber                                                                                                                                                                                                                                                                 AS Item#,
           QI.QuoteItemDescription                                                                                                                                                                                                                                                                 AS ItemDescription,
           QI.QuoteItemQuantity                                                                                                                                                                                                                                                                    AS ItemQuantity,
           QI.QuoteItemUnit                                                                                                                                                                                                                                                                        AS ItemUnit,
           QI.QuoteItemUnitprice                                                                                                                                                                                                                                                                   AS UnitCost,
           QI.QuoteItem_ItemTotalPrice                                                                                                                                                                                                                                                             AS UnitPrice,
           (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice)                                                                                                                                                                                                                                          AS TotalCost,
           (QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice)                                                                                                                                                                                                                                    AS TotalPrice,
           (QI.QuoteItemWeight * QI.QuoteItemQuantity)                                                                                                                                                                                                                                             AS TotalWeight,
           QI.QuoteItemWeight                                                                                                                                                                                                                                                                      AS ItemWeight,
           QI.QuoteItemMarkupPercent                                                                                                                                                                                                                                                               AS MarkupPercent,
           Q.QuoteFreightRate                                                                                                                                                                                                                                                                      AS FreightRate,
           Q.QuoteNumberOfTrucks, 
           Q.QuoteSurcharge1, 
           Q.QuoteSurcharge2, 
           Q.QuoteSurcharge3, 
           ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3))                                                                                                                                                                            AS TotalFreightforQuote,
           (((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice) - (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) - ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3))) / ((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice)) * 100) AS MARGIN
      INTO #temptable 
      FROM TBL_Project P 
           INNER JOIN TBL_Quotes1 Q 
             ON P.ProjectID = Q.ProjectID 
           INNER JOIN TBL_County C 
             ON C.CountyID = P.ProjectCountyID 
           INNER JOIN TBL_QuoteItems QI 
             ON Q.QuoteID = QI.QuoteID 
           INNER JOIN TBL_Division D 
             ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN  TBL_CustomerQuotes CU  
           -- ON CU.QuoteID = Q.QuoteID 
           INNER JOIN tbl_customer cu 
             ON cu.customercreatedby = Q.QuoteCreatedby 
     WHERE p.projectstartdate >= ISNULL(@LettingDateFrom,p.projectstartdate) 
           AND p.projectstartdate <= ISNULL(@LettingDateTo,p.projectstartdate) 
           AND D.DivisionName IN (SELECT Item 
                                    FROM dbo.FNEBID_SPLITVALUE(CASE 
                                                                 WHEN @DivisionName = '' 
                                                                       OR @DivisionName IS NULL THEN D.DivisionName
                                                                 ELSE @DivisionName 
                                                               END,',')) 
           AND p.ProjectTxDot = CASE 
                                  WHEN @projectTxDot = '' THEN p.ProjectTxDot 
                                  ELSE @projectTxDot 
                                END 
           AND ((@CustomerName <> 'all' 
                 AND CU.CustomerName IN (SELECT Item 
                                           FROM dbo.FNEBID_SPLITVALUE(@CustomerName,','))) 
                 OR (@CustomerName = 'all')) 
  END

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35088892
yes i seems working
but i have a small issue in the report.If i select all the report should display for all customers but it showing the data in report by one customer name.However the calculation are made by selecting all customers .I have attatched the screenshot
Output1.docx
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35088955
check this.
ALTER PROCEDURE [dbo].[DBSP_REPORT_GETLETTINGDATE1]( 
               @LettingDateFrom DATETIME, 
                 @LettingDateTo DATETIME, 
                  @ProjectTxDot INT, 
                  @CustomerName VARCHAR(50)  = '', 
                  @DivisionName VARCHAR(250)  = '') 
AS 
  BEGIN 
    --declare @SQL varchar(8000)  
    --DECLARE @country varchar(255) 
    SELECT CU.CustomerID, 
           case when @CustomerName = 'all' then 'All Customers' else CU.CustomerName end CustomerName, 
           P.ProjectID, 
           P.Projectname, 
           p.ProjectNumber, 
           P.ProjectCountyID, 
           CONVERT(VARCHAR(10),P.ProjectStartDate,110)                                                                                                                                                                                                                                             AS ProjectStartDate,
           P.projectTxdot, 
           D.DivisionID, 
           D.Divisionname                                                                                                                                                                                                                                                                          AS DivisionName,
           --P.projectCountyID As CountyID, 
           C.CountyName                                                                                                                                                                                                                                          AS CountyName,
           --CU.CustomerName As CustomerName,    
           --CU.customerID, 
           QI.QuoteItemQuantity                                                                                                                                                                                                           AS Quantity,
           QI.QuoteItem_ItemNumber                                                                                                                                                                                                                                                                 AS Item#,
           QI.QuoteItemDescription                                                                                                                                                                                                                                                                 AS ItemDescription,
           QI.QuoteItemQuantity                                                                                                                                                                                                                                                                    AS ItemQuantity,
           QI.QuoteItemUnit                                                                                                                                                                                                                                                                        AS ItemUnit,
           QI.QuoteItemUnitprice                                                                                                                                                                                                                                                                   AS UnitCost,
           QI.QuoteItem_ItemTotalPrice                                                                                                                                                                                                                                                             AS UnitPrice,
           (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice)                                                                                                                                                                                                                                          AS TotalCost,
           (QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice)                                                                                                                                                                                                                                    AS TotalPrice,
           (QI.QuoteItemWeight * QI.QuoteItemQuantity)                                                                                                                                                                                                                                             AS TotalWeight,
           QI.QuoteItemWeight                                                                                                                                                                                                                                                                      AS ItemWeight,
           QI.QuoteItemMarkupPercent                                                                                                                                                                                                                                                               AS MarkupPercent,
           Q.QuoteFreightRate                                                                                                                                                                                                                                                                      AS FreightRate,
           Q.QuoteNumberOfTrucks, 
           Q.QuoteSurcharge1, 
           Q.QuoteSurcharge2, 
           Q.QuoteSurcharge3, 
           ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3))                                                                                                                                                                            AS TotalFreightforQuote,
           (((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice) - (QI.QuoteItemQuantity * QI.QuoteItemUnitPrice) - ((Q.QuoteFreightRate * Q.QuoteNumberOfTrucks) + (Q.QuoteSurcharge1 + Q.QuoteSurcharge2 + Q.QuoteSurcharge3))) / ((QI.QuoteItemQuantity * QI.QuoteItem_ItemTotalPrice)) * 100) AS MARGIN
      INTO #temptable 
      FROM TBL_Project P 
           INNER JOIN TBL_Quotes1 Q 
             ON P.ProjectID = Q.ProjectID 
           INNER JOIN TBL_County C 
             ON C.CountyID = P.ProjectCountyID 
           INNER JOIN TBL_QuoteItems QI 
             ON Q.QuoteID = QI.QuoteID 
           INNER JOIN TBL_Division D 
             ON D.Createdby = Q.QuoteCreatedBy --INNER JOIN  TBL_CustomerQuotes CU  
           -- ON CU.QuoteID = Q.QuoteID 
           INNER JOIN tbl_customer cu 
             ON cu.customercreatedby = Q.QuoteCreatedby 
     WHERE p.projectstartdate >= ISNULL(@LettingDateFrom,p.projectstartdate) 
           AND p.projectstartdate <= ISNULL(@LettingDateTo,p.projectstartdate) 
           AND D.DivisionName IN (SELECT Item 
                                    FROM dbo.FNEBID_SPLITVALUE(CASE 
                                                                 WHEN @DivisionName = '' 
                                                                       OR @DivisionName IS NULL THEN D.DivisionName
                                                                 ELSE @DivisionName 
                                                               END,',')) 
           AND p.ProjectTxDot = CASE 
                                  WHEN @projectTxDot = '' THEN p.ProjectTxDot 
                                  ELSE @projectTxDot 
                                END 
           AND ((@CustomerName <> 'all' 
                 AND CU.CustomerName IN (SELECT Item 
                                           FROM dbo.FNEBID_SPLITVALUE(@CustomerName,','))) 
                 OR (@CustomerName = 'all')) 
  END

Open in new window

0
 

Author Comment

by:sqlcurious
ID: 35089347

actually i want to display customer name :all in the report output if i select all from the drop down list now its taking one particular customer but showing data  for all  customers.
Please check the screenshot .I think some changes has to be made in ssrs

i used the following query in the dataset called customer.Do i have to chnage anything there?please let me know

SELECT   distinct   CustomerName
FROM         TBL_Customer
UNION all
SELECT     'all'
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question