15,896,269 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by HarshadaBS (Top 33 by date)
HarshadaBS
9-Dec-17 9:52am
View
Deleted
Okay
HarshadaBS
9-Dec-17 9:52am
View
Deleted
Okay
HarshadaBS
9-Dec-17 9:47am
View
@santosh Kumar sir .. Why? I searched everywhere but everyone gives complicated answers which is difficult to understand. That's why I posted here
HarshadaBS
9-Dec-17 9:46am
View
Yes. I got it.
HarshadaBS
9-Dec-17 9:45am
View
Thanks Suvendu sir and laxmidhar sir. It helped me to understand the reason of my question.
HarshadaBS
9-Dec-17 9:41am
View
One more thing is pivot is used when we want to convert rows into columns. It was my mistake that I didn't mention that, my tables come from database. I didn't created it. But now I have changed my question. But it doesn't include tables now.
HarshadaBS
9-Dec-17 9:35am
View
Yes. You are R8. Your query helps in advance level. But I will try it and get back to you soon.
HarshadaBS
9-Dec-17 9:30am
View
It's really nice to have this query. I will try it too and reply you back soon. Empty cells are present because I put syntax like (else ''). And it was the requirement to display empty cells if (name) is not in ( customer, client, supplier). In my database some names put under these three relationships and some names are not in any relationship. That's why some cells are empty.
HarshadaBS
8-Dec-17 6:24am
View
This is not exactly true. Whatever I ask here is for dummy data. I already tried some solutions by my own. When I was unable to find exact syntax I ask here. In reality it's a small part of big query. But as I said I am new to sql, sometimes I find it difficult. As you suggest I have changed my question.
HarshadaBS
6-Dec-17 8:23am
View
Yes. Perfect
HarshadaBS
6-Dec-17 7:37am
View
I want distinct record of each unique name and column values of supplier, client and customer should be like output given by your query i.e. solution 1
HarshadaBS
6-Dec-17 7:29am
View
I have tried this 'group by con.companyname'. but it's not working.
HarshadaBS
6-Dec-17 7:16am
View
as you can see isclient , issupplier, iscustomer columns are not from database. Please try to understand above query. and let me know how to apply your answer
HarshadaBS
6-Dec-17 7:13am
View
yes its working.
but my source-table is coming from database. i am giving my query so that you can understand.
Your query is perfect but how to apply to below query.
SELECT DISTINCT Con.CompanyName AS Contact,
'Y' AS IsClient,
'' AS IsCustomer,
'' AS IsSupplier
FROM contacts con
INNER JOIN Countries Cont ON Con.CountryID=Cont.CountryID
INNER JOIN ContactRelations CRes ON Con.ContactID=CRes.ContactID
INNER JOIN ContactRelationTypes CRel ON CRes.ContactRelationTypeID=CRel.ContactRelationTypeID
WHERE CRel.ContactRelationTypeID=2 -- 2 Client
UNION
SELECT DISTINCT Con.CompanyName AS Contact,
'' AS IsClient,
'Y' AS IsCustomer,
'' AS IsSupplier
FROM contacts con
INNER JOIN Countries Cont ON Con.CountryID=Cont.CountryID
INNER JOIN ContactRelations CRes ON Con.ContactID=CRes.ContactID
INNER JOIN ContactRelationTypes CRel ON CRes.ContactRelationTypeID=CRel.ContactRelationTypeID
WHERE CRel.ContactRelationTypeID=3 -- 3 Customer
UNION
SELECT DISTINCT Con.CompanyName AS Contact,
'' AS IsClient,
'' AS IsCustomer,
'Y' AS IsSupplier
FROM contacts con
INNER JOIN Countries Cont ON Con.CountryID=Cont.CountryID
INNER JOIN ContactRelations CRes ON Con.ContactID=CRes.ContactID
INNER JOIN ContactRelationTypes CRel ON CRes.ContactRelationTypeID=CRel.ContactRelationTypeID
WHERE CRel.ContactRelationTypeID=8 -- 8 Supplier
HarshadaBS
6-Dec-17 6:26am
View
No. This query does not give R8 answer. I have already tried this. Need output in one row only. And all columns should display with their values as shown in my output
HarshadaBS
23-Nov-17 0:22am
View
Okay. I am new to CodeProject. But it is very nice experience to know more about coding
HarshadaBS
22-Nov-17 0:55am
View
Deleted
Modify the comment. Delete the comment.
As you say I have deleted my solution. But whatever comments are below also deleted. Is that any way to get it back. Actually when I thanked you after correct answer given by you I deleted your reply also by mistake. Can you write it again. Please
HarshadaBS
22-Nov-17 0:53am
View
Deleted
As you say I have deleted my solution. But whatever comments are below also deleted. Is that any way to get it back. Actually when I thanked you after correct answer given by you I deleted your reply also by mistake.and I didn't read it properly. Can you write it again. Please. Please write again comments. Do not write code.
HarshadaBS
21-Nov-17 6:01am
View
That's what I need. Thank you so much for helping.
HarshadaBS
21-Nov-17 4:37am
View
Now using above code only how can we convert null values with 0. Can you share syntax?
HarshadaBS
21-Nov-17 4:34am
View
this is your 1st code
------------------------
DROP TABLE IF EXISTS #Demo;
CREATE TABLE #Demo(Client VARCHAR(50), Country VARCHAR(50),
InvoiceAmount DECIMAL(5,2))
INSERT INTO #Demo
VALUES
('Raja','India',100.00),('Ajit','Begium',200.00),('Sukanya','China',300.00),
('Neha','India',600.00),('Raja','India',300.00),('Ajit','Begium',800.00),
('Sukanya','China',700.00),('Neha','India',600.00),('Raja','Belgium',50.00),
('Neha','France',130.00);
DECLARE @QUERY AS NVARCHAR(MAX);
DECLARE @Col1 AS NVARCHAR(50)='Country'; --'CLIENT'
DECLARE @Col2 AS NVARCHAR(50)='CLIENT'; --'Country';
DECLARE @Col3 AS NVARCHAR(50)='InvoiceAmount';
DECLARE @InputTable AS NVARCHAR(50)='#DEMO';
DECLARE @PivotRows AS NVARCHAR(MAX);
DECLARE @Pvtab TABLE(Cols varchar(max));
DECLARE @Pvtcol Nvarchar(max);
SET @PivotRows =N'SELECT STUFF((SELECT DISTINCT '',''+QUOTENAME('+@Col2+',''['')
FROM ' + @InputTable + ' AS C
FOR XML PATH(''''),Type).value(''.'',''NVARCHAR(MAX)''), 1, 1,'''');'
INSERT INTO @Pvtab(Cols) EXEC(@PivotRows);
SELECT @Pvtcol=Cols FROM @Pvtab;
---print @pvtcol
SET @QUERY = N'SELECT * FROM
(
SELECT DISTINCT '+@Col1+','+@Col2+','+@col3+' FROM ' + @InputTable + '
)AS Te
PIVOT (
SUM(['+@Col3+']) FOR '+@Col2+' IN ('+@Pvtcol+')
)AS PIV'
EXEC(@QUERY)
------------------------------------------------
Using This code only(means only use table variables and column variables) (do not use name of columns directly)(columns and table should be dynamic) I want the output like below
Country Ajit Neha Raja Sukanya
1 Begium 1,000.00 0.00 0.00 0.00
2 Belgium 0.00 0.00 50.00 0.00
3 China 0.00 0.00 0.00 1,000.00
4 France 0.00 130.00 0.00 0.00
5 India 0.00 600.00 400.00 0.00
---------------------------------------
HarshadaBS
21-Nov-17 4:31am
View
This query returns null as 0.
HarshadaBS
21-Nov-17 4:30am
View
Sorry this was the output of previous query.
HarshadaBS
21-Nov-17 4:29am
View
after running above code i am getting this output. Am I doing anything wrong? What should I do?
-----------------------------------------------------------------
(No column name)
cast([Begium] AS Varchar(20)) AS [Begium],cast([India] AS Varchar(20)) AS [India]
-----------------------------------
(No column name)
[Begium], [India]
HarshadaBS
21-Nov-17 2:58am
View
Deleted
Please go through it. You can understand my question more clearly.
HarshadaBS
21-Nov-17 2:57am
View
Deleted
No issues sir. You are helping me that is a big thing. I am giving my expectations in more detail way as follows
HarshadaBS
20-Nov-17 3:56am
View
I mean to say... In your query, how can I convert null values with 0? Please give me syntax.
HarshadaBS
20-Nov-17 3:51am
View
Your query gives correct output. I don't want to change the output. I just want to replace all null values with 0. That's what I need and want syntax to convert null values with 0. But use above code only. Do not write separate syntax for isnull.
HarshadaBS
20-Nov-17 1:36am
View
Can you share the syntax to avoid Null values?
HarshadaBS
18-Nov-17 1:20am
View
/*Something like this...But this query is not right*/
SET @PivotRows =N'SELECT STUFF((SELECT DISTINCT '',ISNULL('+QUOTENAME('+@Col2+',''['')+',0.0)AS'+QUOTENAME('+@Col2+',''['')+''
FROM ' + @InputTable + ' AS C
FOR XML PATH(''''),Type).value(''.'',''NVARCHAR(MAX)''), 1, 1,'''');'
HarshadaBS
18-Nov-17 1:14am
View
To avoid Null values and replace it with '0' then what will be the syntax? I am trying but it gives syntax error.
HarshadaBS
18-Nov-17 0:51am
View
This Query Works Properly
HarshadaBS
17-Nov-17 6:03am
View
Before trying this query, I want to ask you that, can you explain why did you use temporary table (#Demo), instead of table variable(ex. @Table)?
Show More