SQL Way To Suppress Repeated Values
Feb 26, 2007
This is a problem I usually solve with procedural code, but I am wondering how/if it could be done with SQL queries.
A simple one to many query like:
Select inv.invnbr, inv.freight, invline.quantity, invline.partnbr, invline.cost from inv inner join invline on inv.id = invline.InvID
Returns something like:
invnbr freight quantity partnbr cost
100 50 3 abc 50
100 50 6 def 65
100 50 10 ghi 70
Is there way I can rewrite the query, or add a subquery such that the result set would be:
invnbr freight quantity partnbr cost
100 50 3 abc 50
100 0 6 def 65
100 0 10 ghi 70
Eg, the freight value, which comes from the one/header table, would show up in only one of the lines for invnbr 100?
Many thanks
Mike Thomas
View 9 Replies
ADVERTISEMENT
May 21, 2007
The scenario is as follows:
I have rows coming from the db including:
Contract Number, Contract Name, owner and Actions associated with each contract.
SQL statement brings back:
Contract Number Contract Name Sales Owner Action
1234 123453 $50 Neil x
1234 123453 $50 Bob y
534232 5464634211 $30 Harry z
The problem is that each contract can have multiple actions associated with it...
There ideal output would be:
Contract Number Contract Name Sales Owner Action
1234 123453 $50 Neil x
Bob y
534232 5464634211 $30 Harry z
Total: $80
Basically I need to hide and not include repeated items based on a contract number... one idea I had was creating a group based on contract number and then display info in the header and then only owner and actions in the detail section.. The problem is Totals... how can I can it to avoid count the duplicated values..
Any help would be greatly appreciated.
Thanks,
Neil
View 4 Replies
View Related
Jul 2, 2015
The cube going into browser or Excel, shows the following.
Measure Measure Calculated TotalRevenue(measure) TotalCost(measure) GrossMargin(calculated) Gross Margin%(calculated)
$#,##0;($#,##0) $#,##0;($#,##0) "$#,##0;($#,##0)" "#,##0.0 %;(#,##0.0 %)"
The excel gives me..
A $552,198 ($437,190) $115,008 20.8%
B $0 $0 $0
How can I suppress/hide the row with $0 values and NULL. So the user does not need to filter, in Excel.
View 4 Replies
View Related
Aug 26, 2014
I'm trying to come up with a query for this data:
CREATE TABLE #Visits (OpportunityID int, ActivityID int, FirstVisit date, ScheduledEnd datetime, isFirstVisit bit, isRepeatVisit bit)
INSERT #Visits (OpportunityID, ActivityID, FirstVisit, ScheduledEnd)
SELECT 1, 1001, '2014-08-17', '2014-08-17 12:00:00.000' UNION ALL
SELECT 1, 1002, '2014-08-17', '2014-08-17 17:04:13.000' UNION ALL
SELECT 2, 1003, '2014-08-18', '2014-08-18 20:39:56.000' UNION ALL
[Code] ....
Here are the expected results:
OpportunityIDActivityIDFirstVisitScheduledEndisFirstVisitisRepeatVisit
110012014-08-172014-08-17 12:00:00.00010
110022014-08-172014-08-17 17:04:13.00001
210032014-08-182014-08-18 20:39:56.00001
210042014-08-182014-08-18 18:00:00.00010
[Code] ....
Basically I'd like to mark the first Activity for each OpportunityID as a First Visit if its ScheduledEnd falls on the same day as the FirstVisit, and otherwise mark it as a Repeat Visit.
I have this so far, but it doesn't pick up on that the ScheduledEnd needs to be on the same day as the FirstVisit date to count as a first visit:
SELECT*,
CASE MIN(ScheduledEnd) OVER (PARTITION BY FirstVisit)
WHEN ScheduledEnd THEN 1
ELSE 0
END AS isFirstVisit,
CASE MIN(ScheduledEnd) OVER (PARTITION BY FirstVisit)
WHEN ScheduledEnd THEN 0
ELSE 1
END AS isRepeatVisit
FROM#Visits
View 3 Replies
View Related
Jun 15, 2015
I have one table
Customer ID; Sort of Transactions;Value
1234;Kit;1
1235;Kit;1
1236;Revenue;30
1235;Kit;2
1238;Revenue;10
1235 ;Kit;1
Looking for a formula for calculated column, which gives me sum of "Kit" for Customers
1234;Kit;1;1
1235;Kit;1;4
1236;Revenue;30;0
1235;Kit;2;4
1238;Revenue;10;0
1235 ;Kit;1;4
View 2 Replies
View Related
Jan 30, 2008
I'm trying to suppress whitespace in a drilldown for textboxes that have suppress duplicates applied.
I have a matrix report that is showing whitespace in a drilldown because I am supressing duplicates. Based on what I read in other forums, if I set the ToggleItem to Len(FieldName)=0 that should supress the whitespace, right?
I can see that I have a field in the toggleitem called: Firstname. If I put the value Len(Firstname)=0 in the toggleitem property, then I get the error: The textbox 'textbox21' has Len(Firstname)=0' as a toggle item. Toggle items must be text boxes that share the same scope as the hidden item. I think the code 'Len' is throwing it off.
If I put the value "Firstname" in the toggleitem property, then it doesn't return the error, so I know that firstname is a valid value for toggleitem, but setting the value to firstname doesn't suppress anything.
If someone can tell me how to supress a textbox based on a value, then this may get rid of the whitespace I'm trying to suppress. Any ideas? Thanks...
View 3 Replies
View Related
Sep 4, 2007
I have problem, i wanted a query which will search the duplicate and then give suggestionmost repeated word
Table containing the records like below
ID
Movie Name
New Name
1
Spider Man
Spider Man
2
Spider Man 2
Spider Man
3
Spider Man 3
Spider Man
4
Spider Man UK
Spider Man
5
Spider Man USA
Spider Man
6
New Spider Man
Spider Man
7
Spider Man Black
Spider Man
8
Spider Man Part 1
Spider Man
9
Spider Man Part 2
Spider Man
10
Spider Man I
Spider Man
11
Spider Man III
Spider Man
12
Spider Man Part II
Spider Man
My manufacturer send me the data in this format and i have to allocate there new name
to do some comparison
I wanted to make this process automatic.
what i mean is i need a query which will give me a repeated records along with suggestion
as new name.
I am fully confident that you guys will help me out from this problem.
Looking forward
View 9 Replies
View Related
Aug 14, 2014
From what I've seen, the CheckSum_Agg function appears to returns 0 for even number of repeated values. If so, then what is the practical use of this function for implementing an aggregate checksum across a set of values?
For example, the following work as expected; it returns a non-zero checksum across (1) value or across (2) unequal values.
declare @t table ( ID int );
insert into @t ( ID ) values (-7077);
select checksum_agg( ID ) from @t;
-----------
-7077
declare @t table ( ID int );
insert into @t ( ID ) values (-7077), (-8112);
select checksum_agg( ID ) from @t;
-----------
1035
However, the function appears to returns 0 for an even number of repeated values.
declare @t table ( ID int );
insert into @t ( ID ) values (-7077), (-7077);
select checksum_agg( ID ) from @t;
-----------
0
It's not specific to -7077, for example:
declare @t table ( ID int );
insert into @t ( ID ) values (-997777), (-997777);
select checksum_agg( ID ) from @t;
-----------
0
What's curious is that (3) repeated equal values will return a checksum > 0.
declare @t table ( ID int );
insert into @t ( ID ) values (-997777), (-997777), (-997777);
select checksum_agg( ID ) from @t;
-----------
-997777
But a set of (4) repeated equal values will return 0 again.
declare @t table ( ID int );
insert into @t ( ID ) values (-997777), (-997777), (-997777), (-997777);
select checksum_agg( ID ) from @t;
-----------
0
Finally, a set of (2) uneuqal values repeated twice will return 0 again.
declare @t table ( ID int );
insert into @t ( ID ) values (-997777), (8112), (-997777), (8112);
select checksum_agg( ID ) from @t;
-----------
0
View 0 Replies
View Related
Mar 28, 2006
i'm retrieving addresses from a database and displaying them in my report. i have an addr line 2 for addition address data if needed. i have placed this addr line 2 on its own detail row. however i do not want that row to display if there is no data. the following is happening even though i have set the visibility on the row and text field to =iif(fields!addr2="",false,true)
the name prints on the first line, the main address on the second line, i have a space where addr line 2 would have been, finally i get the city, state, zip on the last line.
expected outcome i would like is that addr line 2 does not appear for those addresses that addr line 2 does not have any data. if addr line 2 does have data then print.
any help would be greatly appreciated.
Chuck
View 7 Replies
View Related
Feb 21, 2007
Why this SQL procedure gives contiguous repeated records ( 3 or 4 times ) ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ALTER PROCEDURE GetProductsOnPromotInDep
(@DepartmentID INT)
AS
SELECT Product.ProductID, Title
FROM Product INNER JOIN
(ProductCategory INNER JOIN
(Category INNER JOIN Department ON Department.DepartmentID = Category.DepartmentID)
ON ProductCategory.CategoryID = Category.CategoryID)
ON Product.ProductID = ProductCategory.ProductID
WHERE Category.DepartmentID = @DepartmentID
AND ProductCategory.CategoryID = Category.CategoryID
AND Product.ProductID = ProductCategory.ProductID
AND Product.OnPromotion = 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
View 2 Replies
View Related
Dec 21, 2004
If I have a student table with 3 columns ID, FirstName and LastName
ID- FirstName -LastName
-------------------------
1 Tom Hanks
2 Jerry Thomas
and I have a subject table with two columns StudentID and Subject. StudentID is a foreign key to the student table.
StudentID - Subject
------------------------
1 History
1 Biology
2 History
If my query is
select distinct student.id, student.fname, student.lname, student.subject from student
left outer join subject on student.id = subject.studentid
then I get
1 Tom Hanks History
1 Tom Hanks Biology.
Is there a way I could get
1 Tom Hanks History
null null null Biology
--> meaning I don't want to repeat data??
thanks
Teena
View 1 Replies
View Related
Nov 28, 2007
Is there a way to suppress a text box if there is no data to display. I've created a letter that may or may not have address2 address3. The format looks odd with a bunch of blanks lines in the company info section. I use to be able to do this in crystal don't know how to do this in SRS.
Thanks
View 11 Replies
View Related
Jun 19, 2006
How can find out the repeated character in each row value of spacific column
View 1 Replies
View Related
Dec 17, 2013
For the below mentioned query their is repetition of rows with the same data.
SELECT srs.prod_area as PA,
srs.art as ArtNo,
b.adsc_unicode as ArtName,
cast(case when a.unit <> '2' then c.avsx/10 else c.avsx/1000 end as integer(2)) as AwsMHS,
cast(srs.estimate/10 as integer(1)) as AwsSRSThisWeek,
[Code] ....
View 4 Replies
View Related
Mar 5, 2014
finding the solution for the below query?It displays repeated records.
select distinct ku.username,rro.role_name,rp.resource_type_code,kr.region_name,kc.currency_name,
fcr.cost_rule_id RULE,fcr.rate current_rate,pp.project_name,kou.org_unit_name ORG_UNIT
from
[code]....
View 2 Replies
View Related
Feb 5, 2008
Hi!
I am beginner on using SQL.
How can I select the repeated rows (five or more times) on a table which have the same ID's but different updated date. I do not need to group all the rows with the same ID,s but rows which are repeated many times according to the required reports needed.
Below are some information regarding tables and views:
VIEW name= dbo.tblCCInvoicesH
COLUMN ID= ConsumerID
COLUMN DATE= ReadingDate
Thank you in advance...
meti
View 1 Replies
View Related
Mar 10, 2008
Hi
I have created a Percent Log Used Alert with a threshold of 85% and am getting e-mailed with Database Mail, the problem is that I continue to get e-mailed repeatedly with the same e-mail until I disable the Alert. Is there any way to have it just e-mail it once?
Thanks
Pam
View 3 Replies
View Related
Apr 24, 2002
Hello, I have 3 servers sql 7 sp3, 1 editor, a distributor and an subscriber. the base on the editor is removed, how to make to remove all traces of replication on the distributor and the subscriber.
thank you. Pascal
View 1 Replies
View Related
Aug 19, 2001
I wrote a stored procedure that executes a dbcc sqlperf(logspace) statement many times. Therefore, I would like to suppress the output, "DBCC execution completed...". Can this be done? I have checked Books Online for a trace flag or set command to no avail.
View 1 Replies
View Related
Apr 24, 2006
I just scanned the OSQL articles in BOL and I am not sure if there is a way to do this but I want to suppress the empty black DOS windows when calling my batch files that contain osql commands. Any ideas?
View 2 Replies
View Related
Jan 12, 2004
When I call osql -S server -E -h-1 with a -i option, the feedback I get is prefaced with "1> 2> 3>".
The test script is simply:
set nocount on
use master
select name from sysdatabases
How can I suppress the "1> 2> 3>"?
Thanks
View 2 Replies
View Related
Sep 18, 2007
Is there any way to suppress the whole package from failing when a file is not found while doing an FTP get (other then setting the MaxErrorCount higher)?
My situation is that I know what a file is named but the file may not exist yet on the server. So, I use an FTP task to do a Get on that file. If the file does not exist then the FTP Task increments the error count even if I force the ExecutionResult and/or ExecutionValue.
This wouldn't be a very big deal but I am looping to getting and processing multiple files. It's acceptable that the file does not exist, but I want to be able to have the package actually error in other places. So far, the only thing I can do to stop the package from failing is setting the MaximumErrorCount for the Loop and the Package which isn't going to cut it.
I am hoping there is away around this without having to use a 3rd party FTP Task.
Thanks,
-Ryan
View 6 Replies
View Related
Nov 28, 2007
Hi,
I am running SSIS package using command line. However I want to suppress the messages that come and I want to display my own messages. How can I do that?
For eg: I am using File System task to transfer a file from source to destination. How will i tell this to the user who is seeing this executed at the command line?
thanks.
View 1 Replies
View Related
Apr 8, 2008
Hello All,
I have an SSIS package which uses a third party RMS driver. It is not licensed and so it throws a warning messagebox when the package is run and every time the connection is used. Is there a way to suppress these messages so that the package can be scheduled and run without user interaction?
Thanks
Arun
View 10 Replies
View Related
Jan 30, 2006
Since there is no way to pass value from subreport to the main report, how to suppress the subreport when it is blank?
Thanks.
View 29 Replies
View Related
Mar 31, 2008
Hi,
I have a sequence container with a bunch of tasks in it. Right now, it's set up so that if any of the tasks in the sequence container "errors out" it sends out an email. However, there are certain tasks in the container that I would prefer NOT to send an error message out if/when they fail. How do I turn off the error message for certain tasks?
Thanks
View 2 Replies
View Related
Feb 13, 2008
I have a Derived Column transformation that pads some string fields with spaces, out to the field's maximum length using an expression similar to
rt_tran_date_bill + REPLICATE(" ",20 - LEN(rt_tran_date_bill))
This gives me a warning
"
Warning: 0x800470D8 at Merge Assessments and Invoices, Derived Column 1 1 [11661]: The result string for expression "rt_tran_date_bill + REPLICATE(" ",20 - LEN(rt_tran_date_bill))" may be truncated if it exceeds the maximum length of 4000 characters. The expression could have a result value that exceeds the maximum size of a DT_WSTR.
Is there any way to suppress the warning, or construct the expression so that it doesn't generate the warning?
In this example rt_tran_date_bill will never exceed 20 characters
View 10 Replies
View Related
Apr 15, 2004
Hi there :)
I am developing a system for my uni course and I am stuck a little problem...
Basically its all about lecturers, students modules etc - A student has many modules, a module has manu students, a lecturer has many modules and a module has many lecturers.
I am trying to get a list of lecturers that run modules associated with a particular student. I am able to get a list of the appropriate lecturers, but some lecturers are repeated because they teach more than one module that the student is associated with.
How can I stop the repeats?
Heres my sql select code in my cs file:
string sqlDisplayLec = "SELECT * FROM student_module sm, lecturer_module lm, users u WHERE sm.user_id=" + myUserid + "" + " AND lm.module_id = sm.module_id " + " AND u.user_id = lm.user_id ";
SqlCommand sqlc2 = new SqlCommand(sqlDisplayLec,sqlConnection);
sqlConnection.Open();
lecturersDG.DataSource = sqlc2.ExecuteReader(CommandBehavior.CloseConnection);
lecturersDG.DataBind();
And here is a pic of my Data Model:
Data Model Screenshot
Any ideas? Many thanks :) !
View 1 Replies
View Related
Sep 7, 2005
I am running a query on multiple tables and the data I get back consists of several repeated rows but with one column different. I want to take out those repeated rows and for the column that is different join that data and separate it by a comma. Can this be done?
Ex.
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 717
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 610
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 310
So i would like this data to come up as:
Cindy Lair 111 Drury Circle Harrisburg Pennsylvania 717,610,310
View 7 Replies
View Related
Feb 4, 2007
In the web site that I am building ( in C# language ), a hypothetic customer who would buy something would be redirected to a Secure payments company where he would make the payment and then the company would send back to my web site, information about this transaction.
My program would then save this info in a Microsoft SQL database. The problem is that this company uses to send the same info several times repeatedly and I do not want to store the same info more than once.
So I want a SQL procedure where it takes the invoice number of the customer ( contained in its string of info ) and looks inside a table to see if it was already stored there. If it is there ( or not ), it would return a value, which could be false/true or 0/1 so my program could use this value to save a new info or not and then activate ( or not ) some related tasks.
I am still learning SQL and I tried the below procedure but it is not working. Which alternative procedure could solve the problem ?
~~~~~~~~~~~~~~~~~~~~~~~~~
CREATE PROCEDURE VerifyIfInvoiceExists
(@Invoice VARCHAR(50))
AS
SELECT COUNT(*) FROM IPN_received
WHERE Invoice = @Invoice
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
View 8 Replies
View Related
Jul 20, 2005
I'm using the following query to look in a log file and show somestatistics. It counts the total number of views and the total numberof unique users who have viewed a particular item (the "id" and"title" fields are associated with the item).SELECT title, COUNT(id) AS NumberViews, COUNT(DISTINCT UID) ASNumberUniqueUsers, Type, idFROM ActivityLogWHERE dtTime >= 'Nov 1 2004 12:00AM' AND DtTime <= 'Nov 1 200512:00AM'GROUP BY Title, Type, hdnIdORDER BY TopViewed descThis works fine on SQL Server 2000 (our development machine), but onSQL Server 7 (our production machine), the title column has the samevalue for every row. The other columns show the correct values.If I remove the "ORDER BY" clause, then it works fine. If I removethe "COUNT(DISTINCT UID)" column, it works fine. If I totally removethe WHERE clause, it works fine.Any ideas? It seems like a bug since it works fine on sql2k. I'vetried adding OPTION (MAXDOP 1) to the end of the query, but thatdidn't help.We're using SQL Server 7.0 sp1, and my boss doesn't want to riskupgrading to sp4 because it might screw up all of our otherapplications. I looked through all of the sp2 through sp4 bug fixes,and I didn't see anything specifically mentioning this.Thanks.
View 2 Replies
View Related
Sep 23, 2007
Hi Guys,
I am using the query below to retrieve these results:
You can see that the results are repeated, once for DATIF = 1 and then again for DATIF = 2.
In this case does not matter if the results appear close to DATIF 1 or DATIF 2.
Take in care that I can not know how may extradates or Extrasums are attached to each Account.
Is there any way to avoid these repeated rows?
Thanks in advance,
Aldo.
ACCOUNTKEY DATFID DATFNAME DATF SUFID SUFNAME SUF
--------------- ----------- -------------------------------------------------- ----------------------- ----------- -------------------------------------------------- ----------------------
123456 1 ExtraDates01 2005-01-01 00:00:00.000 1 ExtraSum01 4
123456 1 ExtraDates01 2005-01-01 00:00:00.000 2 ExtraSum02 3
123456 1 ExtraDates01 2005-01-01 00:00:00.000 3 ExtraSum03 1
123456 1 ExtraDates01 2005-01-01 00:00:00.000 4 ExtraSum04 2
123456 2 ExtraDates02 2004-01-01 00:00:00.000 1 ExtraSum01 4
123456 2 ExtraDates02 2004-01-01 00:00:00.000 2 ExtraSum02 3
123456 2 ExtraDates02 2004-01-01 00:00:00.000 3 ExtraSum03 1
123456 2 ExtraDates02 2004-01-01 00:00:00.000 4 ExtraSum04 2
Code Snippet
SELECT DISTINCT
Accounts.ACCOUNTKEY,
ExtraDates.DATFID,
ExtraDateNames.DATFNAME ,
ExtraDates.DATF ,
ExtraSums.SUFID ,
ExtraSumNames.SUFNAME ,
ExtraSums.SUF
FROM
EXTRADATES AS ExtraDates
LEFT OUTER JOIN EXTRADATENAMES AS ExtraDateNames ON ExtraDates.DATFID = ExtraDateNames.DATFID
RIGHT OUTER JOIN ACCOUNTS AS Accounts ON ExtraDates.KEF = Accounts.ACCOUNTKEY
LEFT OUTER JOIN EXTRASUMS AS ExtraSums
LEFT OUTER JOIN EXTRASUMNAMES AS ExtraSumNames ON ExtraSums.SUFID = ExtraSumNames.SUFID ON Accounts.ACCOUNTKEY = ExtraSums.KEF
LEFT OUTER JOIN EXTRANOTENAMES
RIGHT OUTER JOIN EXTRANOTES ON EXTRANOTENAMES.NOTEID = EXTRANOTES.NOTEID ON Accounts.ACCOUNTKEY = EXTRANOTES.KEF
WHERE
Accounts.SORTGROUP BETWEEN 0 AND 999999999
AND Accounts.ACCOUNTKEY BETWEEN '123456' AND '123456'
View 2 Replies
View Related
Mar 19, 2007
I have hundreds of these errors saying 'Login failed for user 'Reporting' The user is not associated with a trusted SQL Server connection [CLIENT: ip address]
The ip address is that of the server that sql server is installed on.
Looking in my log file, all looks good until I get to Service Broker manager has started, then I get Error: 18452, Severity: 14 State: 1 then these two lines repeat about every minute, for the last 3 days!
I think I must have just missed a tick box somewhere, but where?
I have been into one of the databases, and input and checked data, both via an application I wrote and SQL Server Management Studio.
I am also having trouble connecting using my application to connect to the database, I can only connect if I use a Windows administrator account (this SQL Server 2005 running on a Windows 2003 Server, with the app on PC running Windows 2000)
View 4 Replies
View Related