473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Same Column, Same Table - Query

9 New Member
good day.


sample database:

table1

col1 col2 col3
A item1 100
A item2 200
A item3 300
B item1 400
B item2 500
B item3 600


#################################

query result:

col1 col2 col3 col4 col5
item1 A 100 B 400
item2 A 200 B 500
item3 A 300 B 600


is this possible to do a query with this result.

thank you.
Oct 7 '14 #1

✓ answered by Rabbit

Your first attempt was closer. Now you only reference the table once. The first time you referenced the table 3 times. I said you needed to reference the table 2 times. Is there a reason you decided to reference it only once? You need to do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.    t1.field1,
  3.    t2.field1,
  4.    t1.field2,
  5.    t2.field2
  6.  
  7. FROM
  8.    table1 AS t1
  9.  
  10.    INNER JOIN table1 AS t2
  11.    ON t1.field2 = t2.field2
  12.  
  13. WHERE
  14.    t1.field1 = valueA AND
  15.    t2.field2 = valueB
You need to reference the table twice, not once.

You need to join on the fields in the two references by their alias so it knows which reference you're referring to. Otherwise it has no idea which one you want to use.

And you need to reference the values you're looking for using the alias, you can't use the same reference for 2 different values because that's impossible.

15 1912
Frinavale
9,735 Recognized Expert Moderator Expert
Glennyboy,

Is this a SQL Server question or an HTML question?
What technology are you using to implement your solution?

What are you trying to do?
Oct 7 '14 #2
Bala Kumaran
30 New Member
Yes It is possible getting from Sql. But, you need have use one of the script below to get result from sql.

PHP, ASP.NET, JSP
Oct 7 '14 #3
glennyboy
9 New Member
Frinavale,

thank you for the reply.

- This is for MS SQL Server.
- Im using SQL Server Management Studio Express to test the sql query, then i will use it in MS Excel to export data from sql server to MS Excel.
Oct 8 '14 #4
ck9663
2,878 Recognized Expert Specialist
Yes it's possible. Just JOIN the two tables by COL2

Happy Coding!!!


~~ CK
Oct 8 '14 #5
glennyboy
9 New Member
ck9663,

thanks for the reply.

its only 1 table.
Oct 8 '14 #6
Rabbit
12,516 Recognized Expert Moderator MVP
You can join a table to itself
Oct 8 '14 #7
glennyboy
9 New Member
here is my query code
Expand|Select|Wrap|Line Numbers
  1. USE [SSI-HQ]
  2.  
  3. SELECT
  4.     [SSI$Sales Price].[Sales Code]
  5.     ,[SSI$Sales Price].[Item No_] AS [Item No]
  6.     ,[SSI$Sales Price].[Variant Code]
  7.     ,[SSI$Sales Price].[Unit of Measure Code] AS [Unit of Measure]
  8.     ,[SSI$Sales Price].[Unit Price] AS SALES1
  9.     ,[SSI$Sales Price].[Unit Price] AS SALES2
  10.     ,SSI$Item.[Unit Cost]
  11.     ,[SSI$Sales Price].[Starting Date]
  12.     ,[SSI$Sales Price].[Ending Date]
  13.  
  14.  
  15. FROM [SSI$Sales Price]
  16.  
  17. LEFT JOIN SSI$Item
  18.     ON [SSI$Sales Price].[Item No_] = SSI$Item.No_
  19.  
  20. JOIN [SSI$Sales Price]
  21.     ON [SSI$Sales Price].[Item No_] = [SSI$Sales Price].[Item No_] AND [SSI$Sales Price].[Variant Code] = [SSI$Sales Price].[Variant Code] 
  22.  
  23. WHERE
  24.     [SSI$Sales Price].[Sales Code] = 'C00020' AND
  25.     [SSI$Sales Price].[Sales Code] = 'SRP' AND
  26.     [SSI$Sales Price].[Sales Type] = '0' 
  27.  
  28.  
Oct 8 '14 #8
glennyboy
9 New Member
error message

Msg 1013, Level 16, State 1, Line 3
The objects "SSI$Sales Price" and "SSI$Sales Price" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
Oct 8 '14 #9
glennyboy
9 New Member
here is revised query:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [SSI$Sales Price].[Sales Code] AS CUSTOMER
  3.     ,[SSI$Sales Price].[Sales Code] AS SRP
  4.     ,[SSI$Sales Price].[Item No_] AS [Item No]
  5.     ,[SSI$Sales Price].[Variant Code]
  6.     ,[SSI$Sales Price].[Unit of Measure Code] AS [Unit of Measure]
  7.     ,[SSI$Sales Price].[Unit Price] AS SALES1
  8.     ,[SSI$Sales Price].[Unit Price] AS SALES2
  9.     ,SSI$Item.[Unit Cost]
  10.     ,[SSI$Sales Price].[Starting Date]
  11.     ,[SSI$Sales Price].[Ending Date]
  12.  
  13.  
  14. FROM [SSI$Sales Price]
  15.  
  16. LEFT JOIN SSI$Item
  17.     ON [SSI$Sales Price].[Item No_] = SSI$Item.No_
  18.  
  19. JOIN [SSI$Sales Price] AS CUSTOMER
  20.     ON [SSI$Sales Price].[Item No_] = [SSI$Sales Price].[Item No_] AND [SSI$Sales Price].[Variant Code] = [SSI$Sales Price].[Variant Code] 
  21.  
  22. JOIN [SSI$Sales Price] AS SRP
  23.     ON [SSI$Sales Price].[Item No_] = [SSI$Sales Price].[Item No_] AND [SSI$Sales Price].[Variant Code] = [SSI$Sales Price].[Variant Code] 
  24.  
  25. WHERE
  26.     [SSI$Sales Price].[Sales Code] = 'C00020' AND
  27.     [SSI$Sales Price].[Sales Code] = 'SRP' AND
  28.     [SSI$Sales Price].[Sales Type] = '0'
  29.  
after i run the query nothing happen, no error message
Oct 8 '14 #10
Rabbit
12,516 Recognized Expert Moderator MVP
I'm not sure why you join to the same table 3 times. Your example only shows that you need to join it twice.

Also, you incorrectly reference the second instance of the table, you need to use the alias. When you use [SSI$Sales Price], that is referring to the first instance of the table. Therefore, your WHERE clause is impossible because Sales Code from the first table can never be equal to 2 different values at the same time.
Oct 8 '14 #11
glennyboy
9 New Member
Rabbit,

thanks for the reply.

i want to separate the column of [SSI$Sales Price].[Sales Code] into C00020 and SRP in the same table.

thanks.
Oct 9 '14 #12
Rabbit
12,516 Recognized Expert Moderator MVP
I know, that's why you have to use the alias you gave to the second table.
Oct 9 '14 #13
glennyboy
9 New Member
here is my revised code, but im unable to separate column of sales code and unit price

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     [SSI$Sales Price].[Sales Code]
  3.     ,[SSI$Sales Price].[Item No_]
  4.     ,[SSI$Sales Price].[Variant Code]
  5.     ,[SSI$Sales Price].[Unit of Measure Code]
  6.     ,[SSI$Sales Price].[Unit Price]
  7.     ,SSI$Item.[Unit Cost]
  8.     ,[SSI$Sales Price].[Starting Date]
  9.     ,[SSI$Sales Price].[Ending Date]
  10.  
  11.  
  12. FROM [SSI$Sales Price]
  13.  
  14. LEFT JOIN SSI$Item
  15.     ON [SSI$Sales Price].[Item No_] = SSI$Item.No_
  16.  
  17. WHERE
  18.     [SSI$Sales Price].[Sales Code] IN ('C00020' , 'SRP') AND
  19.     [SSI$Sales Price].[Sales Type] IN ('0', '1')
  20.  
  21.  
Oct 16 '14 #14
Rabbit
12,516 Recognized Expert Moderator MVP
Your first attempt was closer. Now you only reference the table once. The first time you referenced the table 3 times. I said you needed to reference the table 2 times. Is there a reason you decided to reference it only once? You need to do something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.    t1.field1,
  3.    t2.field1,
  4.    t1.field2,
  5.    t2.field2
  6.  
  7. FROM
  8.    table1 AS t1
  9.  
  10.    INNER JOIN table1 AS t2
  11.    ON t1.field2 = t2.field2
  12.  
  13. WHERE
  14.    t1.field1 = valueA AND
  15.    t2.field2 = valueB
You need to reference the table twice, not once.

You need to join on the fields in the two references by their alias so it knows which reference you're referring to. Otherwise it has no idea which one you want to use.

And you need to reference the values you're looking for using the alias, you can't use the same reference for 2 different values because that's impossible.
Oct 16 '14 #15
glennyboy
9 New Member
thanks a lot Rabbit.
Oct 20 '14 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Heist | last post by:
Hi, I just want to know to turn this: CREATE TABLE . ( NOT NULL , (50) COLLATE French_CI_AS NULL , NOT NULL , (50) COLLATE French_CI_AS NOT NULL , NULL , NULL ) ON into this:
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
10
by: ste | last post by:
Hi there, I'm trying to query a MySQL database (containing image data) and to output the results in a HTML table of 3 columns wide (and however many rows it takes) in order to create a basic...
2
by: Volition | last post by:
I have looked around and can't find any help for my SQL problem. I have a Table which lists people peopledb. _____________ id | Name 1 | Fred 2 | Bill 3 | Bob
3
by: Robertf987 | last post by:
Hi, I'm a bit stuck with an access database. I'm using access 2000 if that's any help. Right, it's 3:40am right now and I'm rather tired, but I *hope* this makes sense. I have a table which...
1
by: pl1 | last post by:
Hi everyone, I'm using MS-Access 2003, OS: Window XP Professional sp3. In Northwind database (sample Database),the Suppliers table have column "SupplierID" which is also present in Products...
4
by: Captain Jack Sparrow | last post by:
I have a column in a table named "Hospice". I have a make table query that pulls in this column but renames the column in the result to "Hospice (Yes/No)". The column name in the resulting table is...
1
by: Mihail | last post by:
Hi all ! What I have: In a BE database I use a temporary table to store some data. Of course, for different filters that data will be different. Keep in mind, please, that the table structure is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.