SQL查询转换表

发布于 2024-11-25 18:29:46 字数 874 浏览 0 评论 0原文

我在将一个看起来像这样的表转换

city    | distributor | phno
---------------------------------
new york   xxx           12345

new york   yyy           12312

new york   zzz           12313

london     aaa           12315

london     bbb           11111

hong konk  ccc           12311

city    | distributor1 | phno1 | distributor2 | phno2 | distributor3 | phno3
-----------------------------------------------------------------------------
new york   xxx           12345      yyy         12312     zzz           12313

london     aaa           12315      bbb         11111     0             0             

hong konk  ccc           12311       0            0       0             0
  • 考虑到任何城市都没有更多的表和 3 个分销商

时遇到问题,我已经尝试过自连接,但我得到了重复的记录,请告知。

请告诉我这是否在 Excel 中完成?因为这仅用于报告目的

I got a problem transforming a table that looks like this

city    | distributor | phno
---------------------------------
new york   xxx           12345

new york   yyy           12312

new york   zzz           12313

london     aaa           12315

london     bbb           11111

hong konk  ccc           12311

into

city    | distributor1 | phno1 | distributor2 | phno2 | distributor3 | phno3
-----------------------------------------------------------------------------
new york   xxx           12345      yyy         12312     zzz           12313

london     aaa           12315      bbb         11111     0             0             

hong konk  ccc           12311       0            0       0             0
  • given that any city does not more and 3 distributers

I have tried the self join but I get duplicate records, please advise.

Let me know if this be done in excel ?? as this for reporting purpose only

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-12-02 18:29:46

您也可以在 SQL 中对表进行数据透视。

http://msdn.microsoft.com/en-us/library/ms177410.aspx

You can PIVOT tables in SQL as well.

http://msdn.microsoft.com/en-us/library/ms177410.aspx

久隐师 2024-12-02 18:29:46

我认为这种转换最好在报告编写器中完成,而不是在数据库中完成。

I think this kind of transformation is best done in a report writer, not in the database.

澉约 2024-12-02 18:29:46

我不确定它在 Sybase SQL 中是否有效,但我仍然会在其他 SQL 服务器中从类似的内容开始:

SELECT * FROM 
    (SELECT city FROM table GROUP BY city) AS c
LEFT JOIN (SELECT TOP 1 distributor AS distributor1, phno AS phno1 
    FROM table c1 WHERE c1.city = c.city 
    ORDER BY distributor ASC) AS c1
    ON true
LEFT JOIN (SELECT TOP 2 distributor AS distributor2, phno AS phno2 
    FROM table c2 WHERE c2.city = c.city 
    ORDER BY distributor ASC) AS c2
    ON distributor1 != distributor2
LEFT JOIN (SELECT TOP 3 distributor AS distributor3, phno AS phno3 
    FROM table c3 WHERE c3.city = c.city 
    ORDER BY distributor ASC) AS c3
    ON distributor1 != distributor3 AND distributor2 != distributor3

I'm not sure if it's valid in Sybase SQL, still I would start with something like this in other SQL servers:

SELECT * FROM 
    (SELECT city FROM table GROUP BY city) AS c
LEFT JOIN (SELECT TOP 1 distributor AS distributor1, phno AS phno1 
    FROM table c1 WHERE c1.city = c.city 
    ORDER BY distributor ASC) AS c1
    ON true
LEFT JOIN (SELECT TOP 2 distributor AS distributor2, phno AS phno2 
    FROM table c2 WHERE c2.city = c.city 
    ORDER BY distributor ASC) AS c2
    ON distributor1 != distributor2
LEFT JOIN (SELECT TOP 3 distributor AS distributor3, phno AS phno3 
    FROM table c3 WHERE c3.city = c.city 
    ORDER BY distributor ASC) AS c3
    ON distributor1 != distributor3 AND distributor2 != distributor3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文